Version Control in Practice: Commit Conventions, Remote Sync, and Branch Management

    This guide walks through the practical side of version control, organized around the four key locations (working directory, staging area, local repository, and remote repository), commit conventions that make history explainable, the first upload to a remote repository, remote synchronization (fetch, integrate, push), branch management, conflict resolution and recovery from mistakes, and a reusable daily checklist. Readers will learn how to structure meaningful commits, avoid common push failures, manage branches safely, and build reliable history for collaboration, debugging, and recovery.

    1. First, Understand the Four Key Locations in Version Control

    Version control is not just about “saving code.” It is about organizing changes into a history that can be compared, reverted, and collaborated on. To commit and push correctly, you first need to distinguish four locations: the working directory, the staging area, the local repository, and the remote repository.

    The working directory is where you actually edit files. Adding files, modifying code, and deleting configuration all happen here first. Changes in the working directory are not yet part of the formal history, and they are not uploaded automatically when you push to the remote.

    The staging area is used to select which changes the next commit should include. It is not a simple temporary backup, but an intermediate layer for organizing commit content. For example, when the same working directory contains both feature development and documentation updates, you can stage and commit them separately, avoiding mixing changes with different purposes.

    The local repository stores committed history. A commit typically records a snapshot of the project state, along with the commit message, author information, and parent commit relationships. Completing a local commit does not mean the remote repository has these changes.

    The remote repository is used to exchange commit history, share branches, and support team collaboration. Pushing uploads local commits, while fetching updates your local understanding of the remote state. The two go in different directions and cannot replace each other.

    You can understand their relationship through the conceptual flow below:

    1. Edit files
    2. ↓
    3. Check working directory changes
    4. ↓
    5. Select changes and place them in the staging area
    6. ↓
    7. Check staged content
    8. ↓
    9. Create a local commit
    10. ↓
    11. Push commits to the remote repository

    The flow blocks in this article are neutral illustrations of operations, not commands you can run directly. Actual operations should map to the features provided by the version control system you use.

    2. Commit Conventions: Making History Explainable

    2.1 A commit message should state “what was done”

    A commit message is not decoration. It is an important entry point for code review, issue localization, and history tracking. If a large number of records just say “update,” “fix a bit,” or “final version,” maintainers will struggle to understand the intent behind a given commit.

    A recommended structure is:

    1. Type (optional scope): short summary
    2. Background:
    3. Describe the existing behavior, problem, or requirement.
    4. Main changes:
    5. Describe the key adjustments and implementation boundaries.
    6. Verification:
    7. Describe the checks, tests, or manual validation performed.

    For example:

    1. Fix (login validation): prevent blank accounts from entering the verification flow
    2. Background:
    3. When only whitespace was entered, the original logic still sent a verification request.
    4. Main changes:
    5. Trim leading and trailing whitespace before field validation, and return early on empty values.
    6. Verification:
    7. Cover three input cases: empty string, whitespace-only string, and a normal account.

    The title is for quick identification; the body adds the reasoning and verification basis. Simple changes can have only a title. Changes that involve behavior, compatibility, or risk should include a body.

    2.2 Classify commits by the purpose of the change

    Common types can use the following neutral classification:

    Type Scope Key question
    Feature Adding new business capability or entry point Does it introduce behavior that did not exist before?
    Fix Correcting defects, exceptions, or wrong results Does it restore expected behavior?
    Style Formatting, indentation, and pure formatting changes Does it leave program semantics unchanged?
    Performance Reducing resource usage or improving efficiency Is there a reasonable analysis or verification basis?
    Refactor Adjusting structure, reducing coupling, improving maintainability Does it preserve expected external behavior?
    Revert Reverting a historical change Is the corresponding commit and reason clear?
    Test Adding tests or adjusting test logic Does it mainly affect the verification process?
    Docs Modifying documentation, usage guides, or explanatory comments Does it leave program execution behavior unchanged?

    Classification should serve collaboration, not just fill in labels. In particular, distinguish “refactor” from “fix”: if a change corrects wrong behavior, even if it also adjusts structure, the core purpose is usually still a fix.

    Comment changes cannot all be treated as documentation updates. Some comments or markers are read by build, check, or generation processes; in that case, classify them according to their actual impact.

    2.3 One commit should revolve around a single clear purpose

    If commit granularity is too large, it increases the difficulty of review and rollback; if it is too small, it produces many history records that lack independent meaning. A reasonable commit should have one explainable purpose and, as much as possible, form a complete, verifiable change.

    For example, when fixing an input validation issue, the corresponding code and tests can go in the same commit; unrelated page styling adjusted along the way should be handled separately.

    Before committing, check item by item:

    • Does it include files unrelated to the current goal?
    • Are build artifacts, runtime logs, or local caches added by mistake?
    • Does it contain passwords, access credentials, or private configuration?
    • Are there large-scale meaningless formatting changes?
    • Does the commit message accurately cover the staged content?

    Do not stage all changes by default. Facing mixed changes, selecting content by file or by change hunk is usually safer.

    3. First Upload: From a Local Project to a Remote Repository

    3.1 Organize project boundaries before initializing

    Before initializing a local repository, confirm the project root directory to avoid operating in a parent directory and accidentally including other files. At the same time, define ignore rules to exclude content that should not enter version history.

    Common exclusions include:

    • Build files that can be regenerated;
    • Temporary files and runtime logs;
    • Local caches;
    • Developer personal configuration;
    • Files containing sensitive information.

    Note that ignore rules are typically used to prevent untracked files from being added; they do not automatically remove files already in history. For sensitive information already committed, merely deleting the current file does not eliminate the risk of a history leak.

    3.2 Complete the first local commit

    The recommended flow for the first commit is:

    1. Confirm project root directory
    2. → Initialize local repository
    3. → Configure commit identity
    4. → Set ignore rules
    5. → Check files to be included
    6. → Stage necessary content
    7. → Check staged diff
    8. → Create the first commit

    The commit identity marks the author of history records and does not necessarily equal the login identity of the remote repository. Remote access permissions still need to be confirmed through the appropriate authentication mechanism.

    After the first commit, check the commit record and working directory status to confirm that the content actually included matches expectations, rather than judging the result only by a “success” message.

    Linking a remote is essentially saving a remote address and its alias locally. After linking, you still need to confirm that the address is correct, that access permissions are valid, and that the branch you are about to push matches the team convention.

    Do not assume the default branch name. Different repositories may have different default branches; verify first, then establish the tracking relationship between the local branch and the remote branch.

    A tracking relationship helps the system identify where the current branch usually receives updates from and where it pushes to, but it does not mean conflicts will be resolved automatically.

    3.4 Do not push blindly when the remote is not empty

    If the remote repository already has an initial description file or other commits, the local history and remote history may differ. A first push being rejected is therefore not necessarily a permission issue; it may be that the remote already contains commits you have not yet fetched.

    In this case, fetch the remote history first, determine the relationship between the two, and then choose an integration approach. If you are joining an existing project from scratch, you should usually obtain a copy of the remote project first, then start local changes.

    Do not directly overwrite remote history just to make the first push succeed. Even if the remote content looks minimal, it may already be in use by other members.

    4. Remote Synchronization: Distinguishing Fetch, Integrate, and Push

    4.1 Fetching remote information does not modify current code

    Fetching remote information is mainly used to download new commits and update the locally stored remote-tracking state; it usually does not directly rewrite the current working directory.

    Therefore, when you need to understand the team’s latest progress, you can fetch first, then compare:

    1. Fetch remote information
    2. → View remote branch changes
    3. → Compare local and remote commits
    4. → Decide whether integration is needed

    When fetching, you can prune local tracking references for branches already deleted on the remote as needed. This cleanup usually does not equal deleting a local development branch of the same name, and it certainly does not mean working directory files are removed.

    4.2 Check local state before integrating updates

    If there are uncommitted local changes, integrating remote updates may be blocked, or it may make subsequent conflict handling more complex. Before integrating, confirm how these changes should be preserved:

    • Content that forms a complete change can be committed first;
    • Content that is unfinished but needs to temporarily free up the working directory can be temporarily saved;
    • Content confirmed to be no longer needed can be discarded after verification.

    A temporary save is not formal history and should not be treated as a long-term backup. After the operation, restore it promptly, check it, and form a normal commit.

    4.3 Merging and rebasing each have their boundaries

    Merging integrates changes by preserving the relationship between both histories; it suits shared branches and scenarios that emphasize history integrity.

    Rebasing re-applies local commits on a new starting point, which can produce a more linear record but usually generates new commit identifiers. For personal development commits that have not been shared, this can help tidy up history; for commits already used by other members, it must be done cautiously.

    The team should decide in advance which branches allow history rewriting and which must remain stable, rather than deciding on the spot each time a disagreement arises.

    4.4 When a push fails, check the cause first, do not force-overwrite first

    When a push is rejected, check in order:

    1. Whether the remote already has new commits;
    2. Whether the current account has write permission;
    3. Whether the target branch is protected;
    4. Whether the push target and tracking relationship are correct;
    5. Whether the network connection or authentication state is abnormal.

    If the remote is ahead, fetch and integrate first, then push again. Force-overwriting is not a normal synchronization method. Even a force mechanism with remote-state validation cannot replace communication, permission management, and history backups.

    5. Branch Management: Establishing Clear Development Boundaries

    5.1 Branches isolate work; they do not copy the entire project

    In the common distributed version control model, a branch can be understood as a movable reference pointing to a commit. As new commits are created, the current branch pointer keeps moving forward.

    A team can set up a stable mainline and short-lived task branches: the mainline carries verified integration results, while task branches are used for feature development, defect fixes, or experiments. This division helps narrow the scope of changes and reduces the risk of multiple people directly modifying the mainline.

    5.2 Confirm the starting point before creating a branch

    A correct branch name does not mean a correct starting point. Creating a branch from an outdated mainline may increase later integration costs; creating a new branch from an unfinished task branch may bring unrelated changes along.

    The recommended flow is:

    1. Check working directory status
    2. → Fetch the latest remote information
    3. → Switch to the agreed baseline
    4. → Update the baseline to the expected version
    5. → Create the task branch
    6. → Start development

    Branch names should concisely express their purpose, for example:

    1. feature/order-query
    2. fix/date-validation
    3. chore/config-cleanup

    If the actual system restricts certain characters, use a compatible naming convention agreed on by the team.

    5.3 Handle uncommitted content before switching branches

    Uncommitted changes may persist across a branch switch, or the switch may be blocked because the target branch would overwrite the same files. Therefore, do not treat switching branches as a way to clear the working directory.

    Before switching, check the status and commit, temporarily save, or discard changes as needed. For content whose value is not yet confirmed, prefer to preserve it first, then organize it.

    5.4 Verify and clean up after merging

    When a task is done, check the diff first, then integrate it into the target branch. Whether it has passed review, whether testing is complete, and whether it meets release conditions should all be defined by the team process.

    When a task branch is no longer needed, you can delete the local and remote branch references. But before deleting, confirm:

    • The relevant commits have been accepted by the target branch;
    • No one else is still working based on that branch;
    • The branch is no longer needed as a release or rollback reference.

    Deleting a branch reference does not mean all related commits disappear immediately; however, you should not rely on this mechanism as a long-term retention solution either.

    6. Conflict Handling and Recovery from Mistakes

    6.1 Conflict resolution relies on business judgment

    When both sides modify the same content, or one side deletes a file the other modified, the system may be unable to integrate automatically. A conflict does not mean one side is necessarily wrong; it requires human judgment about the final result.

    When handling it, read the purpose of both changes and confirm whether to keep, combine, or re-implement the relevant logic. Afterward, re-check the diff and run the necessary tests.

    “The conflict markers are gone” only means the text-level work is done; it does not prove the program semantics are correct. When configuration, interfaces, and data structures change, cross-file impact especially needs checking.

    6.2 Use undo and history rewriting per scenario

    For an erroneous commit that has already been shared, it is usually better to add a reverse-change record, preserving history and reducing impact on other members.

    For local commits that have not been shared, you can adjust the commit content or structure as needed, but first confirm that no still-valuable changes will be lost.

    Some recovery operations directly affect the working directory and staging area. Before running them, be clear about whether the target is a file, staged content, a branch reference, or commit history; do not conflate “undo” at different levels.

    6.3 Handle sensitive information leaks separately

    If access credentials have already been committed, the first measure is to revoke or rotate the credentials, not just delete the file. History records, other members’ copies, and automated tasks may all have retained the information.

    Afterward, handle the history according to repository management requirements, and add ignore rules, pre-commit checks, and access controls. Traceable version control history does not mean sensitive information is suitable to enter history.

    7. A Reusable Daily Checklist

    When starting work: check the current branch, confirm the working directory status, fetch remote updates, and start changes from the correct baseline.

    Before committing: review the working directory and staging diffs, exclude sensitive information and unrelated files, complete verification, and write a commit message that explains the purpose.

    Before pushing: confirm the target branch and permissions, check whether the remote is ahead, integrate updates first if necessary, and avoid replacing normal collaboration by overwriting history.

    When a task ends: confirm review and testing are complete, verify the changes have entered the target branch, then clean up branch references that are no longer used.

    The core of version control is not how many operations you memorize, but establishing clear boundaries: each commit explains one purpose, each push confirms the sharing scope, and each branch operation verifies the starting point and impact. Only then does history truly become a reliable foundation for team collaboration, issue localization, and safe recovery.