Branching Workflows & Resolving Merge Conflicts
In professional engineering teams, developers never commit directly to the primary production branch (`main`). Instead, each new feature, bug fix, or data experiment is isolated on an independent **feature branch**. In this chapter, you will master Git's lightweight branching mechanism, execute smooth merges, demystify what causes merge conflicts, and resolve conflicts step-by-step like a senior engineer.
- 01Understand Git branches as lightweight 41-byte movable pointers to specific commits rather than duplicate folder copies
- 02Create and switch between branches using the modern git switch -c and legacy git checkout -b commands
- 03Execute fast-forward merges when histories have not diverged and three-way merges when parallel work has occurred
- 04Identify the exact anatomy of merge conflict markers (<<<<<<< HEAD, =======, >>>>>>> branch)
- 05Resolve merge conflicts cleanly using both terminal commands and graphical editor diff tools in VS Code
- 06Clean up stale feature branches safely using git branch -d and force delete with -D
3.1 What is a Branch? Lightweight Movable Pointers
3.2 Branch Operations: `git branch`, `git checkout` vs Modern `git switch`
List branches, create with switch -c and checkout -b, inspect with branch -v, and delete merged branches.
# List all local branches with their latest commit SHA and message
git branch -v
# Method A (Modern): Create and switch to a new branch with git switch
git switch -c feature/email-heuristics
# Method B (Classic): Create and switch using git checkout
# git checkout -b feature/email-heuristics
# Make changes and commit on the branch
echo "RISK_WEIGHT_URGENT = 25" > heuristics.py
git add heuristics.py
git commit -m "feat: define initial urgency scoring heuristic"
# Switch back to the main branch
git switch main
# Inspect all local branches (feature branch is visible)
git branch -a- 1.git branch -v lists branches and shows the latest commit message for each.
- 2.git switch -c creates and immediately checks out the new branch.
- 3.Commit files on the feature branch.
- 4.git switch main brings your working directory back to the clean main branch snapshot.
- 5.git branch -a confirms both branches exist safely in your repository.
Switched to a new branch 'feature/email-heuristics' [feature/email-heuristics a1b2c3d] feat: define initial urgency scoring heuristic 1 file changed, 1 insertion(+) create mode 100644 heuristics.py Switched to branch 'main'
• If you have uncommitted changes in your working directory that conflict with files on the target branch, Git prevents switching: 'error: Your local changes to the following files would be overwritten by checkout'. You must either commit or stash (`git stash`) your changes first.
Why it happens: Git swaps files in your filesystem instantaneously because it only has to update references and stream cached objects from the local .git database.
3.3 Merging Branches: Fast-Forward vs Three-Way Merge
Perform a fast-forward merge of feature-scanner into main and inspect the unified history.
# Ensure you are on the target branch (main)
git switch main
# Merge the feature branch into main
git merge feature-scanner
# Inspect the updated commit log
git log --oneline --graph- 1.Always verify you are on the destination branch before running git merge.
- 2.git merge feature-scanner merges the commits.
- 3.Inspect git log: both HEAD -> main and feature-scanner now point to the same latest commit.
Updating b4c5d6e..c3d4e5f Fast-forward scanner.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 scanner.py
Why it happens: Because main had not moved since feature-scanner was created, Git used a fast-forward update.
3.4 Demystifying Merge Conflicts: Anatomy & Resolution
Stage the resolved conflict file and record the final merge commit.
# After manually editing the conflicted file in your editor:
git status
# Stage the resolved file
git add scanner.py
# Finalize the merge commit
git commit -m "merge: resolve threshold conflict between main and feature-threshold"
# Delete the merged feature branch safely
git branch -d feature-threshold- 1.git status lists conflicted files under 'Unmerged paths'.
- 2.git add tells Git the conflict in that file is resolved.
- 3.git commit creates the merge commit.
- 4.git branch -d cleans up the branch pointer.
[main d4e5f6a] merge: resolve threshold conflict between main and feature-threshold Deleted branch feature-threshold (was c3d4e5f).
• If you want to abort an in-progress merge and return your repository to the state before running git merge, run: git merge --abort.
Why it happens: git branch -d checks if the branch is fully merged into HEAD before deleting, protecting you from losing unmerged work.
Practice exercise
Work through the exercise before starting the chapter assignment
Exercise 3: Create, Switch, and Delete a Branch
intermediateIn your local terminal, create a branch named 'test-experiment', switch to it, make a commit, switch back to 'main', and verify that your main branch remains on its previous commit.
Chapter summary
Key concepts and syntax cards for review
- 01A Git branch is a lightweight 41-byte pointer referencing a commit; HEAD points to the currently active branch
- 02git switch -c <branch> creates and switches to a new branch cleanly
- 03Fast-forward merges occur when no divergent commits exist on the destination branch
- 04Merge conflicts occur when conflicting changes happen on the same lines of the same file
- 05Git conflict markers (<<<<<<<, =======, >>>>>>>) clearly delimit opposing edits for manual resolution
- 06git merge --abort safely cancels an active merge conflict and restores previous repository state
git branchLists all local branches, marking the currently checked-out branch with an asterisk (*)git switch -c <name>Creates a new branch named <name> and immediately checks it outgit checkout -b <name>Classic Git command to create and switch to a new branch in one stepgit branch -aLists all local and remote tracking branches in the repositorygit switch <name>Switches the working directory to the existing branch <name>git merge <branch>Merges commits from <branch> into the current active branchgit merge --abortAborts an in-progress merge conflict and resets files to pre-merge statusgit branch -d <name>Safely deletes branch <name> if it has already been merged into HEADKnowledge check
Verify your grasp of the chapter concepts
What happens during a 'fast-forward' merge in Git?
Which command cancels an in-progress merge conflict and returns your files to clean pre-merge status?