Branching and Merging: Tracking Parallel Work
Branching and Merging lets you develop a change separately, then integrate it with other work. A branch is a movable name pointing to a commit. A merge integrates another history into the current branch. Together, these operations let you separate unfinished work while preserving how the combined result developed.
Branch names follow your work
A branch name identifies its current tip: the commit at the end of that line of development. Creating a branch at an existing commit adds a name; it does not duplicate the earlier commits. Initially, two branch names can identify exactly the same commit.
During ordinary development on a branch, HEAD identifies which branch receives your next commit. The new commit records its parent, and the current branch name advances to it. Other branch names stay where they were unless an operation explicitly moves them.
Example: If main and feature both point to B, committing C while on feature moves feature to C. The name main still points to B. Switching back to main selects its version of the tracked project files; it does not erase C.
Ancestry determines the merge result
A commit is reachable from a branch tip through its recorded history. One commit is an ancestor of another when you can reach the earlier commit by following parent links backward from the later one. Calendar dates and branch names do not establish ancestry.
To integrate feature into main, switch to main, then run git merge feature. The currently checked-out branch receives the result. The source branch name does not automatically move.
For an ordinary merge of two branch histories, the ancestry cases are:
Relationship before merging | Result |
|---|---|
The source tip is already an ancestor of, or equal to, the target tip | Already up to date; no new commit |
The target tip is an ancestor of the source tip | Fast-forward is possible; the target name can advance without a new commit |
Neither tip is an ancestor of the other | The histories have diverged; completing an ordinary merge creates a commit with both tips as parents |
A fast-forward changes a reference rather than adding a snapshot. Explicit flags control this choice: --ff-only refuses integration when a fast-forward is impossible, while --no-ff requests a merge commit even when a fast-forward is possible.
When a new merge commit M joins two histories, count shared commits only once. If Rt and Rs are the sets of commits reachable from the target and source tips, including the tips themselves, then:
The final 1 counts M. It does not apply to a fast-forward because that operation creates no commit.
Integrate deliberately
Before merging, finish recording unrelated work and check that your tracked files have no pending changes. Confirm the destination branch so you do not integrate in the opposite direction.
A merge conflict pauses the merge for your decision. Inspect both changes and their shared starting version. Edit the affected files into the intended combined result, remove conflict markers, and test the behaviour. Use git add to record each resolved file in the staging area, review the staged changes, then finish with git merge --continue.
What to remember
- A branch name moves; existing commits retain their identities.
- Ancestry determines whether integration can fast-forward.
- Merge into the intended destination branch.
- Shared history counts once, even when multiple paths reach it.
- Test the integrated behaviour, including manually resolved changes.
Example: Integrating Timetable Exports
A campus timetable application has the linear shared history A, B, C, D. Main then adds E and F, while export adds G, H, and I. These are all the commits, with no earlier merges. A common ancestor is a commit included in both tips' parent histories; D is the latest one here.
At D, config.py contains timeout_seconds = 20. Main changes this assignment to timeout_seconds = 30, while export changes it to timeout_seconds = 25 and adds export functionality elsewhere. The approved integration requirement is to retain the export functionality and use a timeout of 30 seconds. Assume Git reports a conflict on this assignment and automatically combines the other edits. There are no pending changes before integration.
a) How many commits are exclusive to each branch before integration?
The shared prefix contains ________. Including that prefix, the main history contains ________, and the export history contains ________. Subtract the shared count from each total to identify its exclusive work.
The exclusive commits are E and F on main, and G, H, and I on export. The answer is 2 commits exclusive to main and 3 exclusive to export.
b) Integrate export into main using an ordinary merge. Can this fast-forward, and how many distinct commits will be reachable from main after you resolve the conflict and complete the merge?
First select the destination with git switch main, then start integration with git merge export. Neither F nor I is an ancestor of the other, so this integration cannot fast-forward. Completing it creates a new merge commit, which we will call M.
Before adding M, the union of the histories contains ________. The completed merge has ________, F and I. Count the shared prefix once, add both exclusive histories, then add M.
As a check, adding the branch totals and removing their overlap gives 6+7−4=9 existing commits. The new merge commit brings the total to 10 reachable commits. This is a two-parent merge, not a fast-forward.
c) Resolve the reported conflict, complete the merge, and identify the final branch tips.
Compare the original 20-second setting with the competing 30-second and 25-second settings. The approved requirement specifies 30 seconds, so edit the assignment to timeout_seconds = 30 and remove its conflict markers. Keep the automatically combined export functionality.
The requirement determines the resolution; the branch name does not. Review the combined changes and run the relevant timeout and export tests. After correcting any failures, stage the resolved file with git add config.py, review the staged changes, and confirm that no unresolved paths remain.
Finish with git merge --continue. This creates M with parents F and I and advances main to M. The source branch export stays at I because merging it does not move its name.
The final configuration uses 30 seconds, with main at M and export at I.
A developer is reviewing branch information in a local copy connected to a remote. Match each term to its description. These descriptions identify roles: one reference can serve more than one role.
The configured comparison and integration partner of a particular local line of development.
An editable named pointer in your copy that advances when you commit while it is selected.
A reference stored in your copy that records the last observed position of a branch in a named remote.
Upstream branch
Local branch
Remote-tracking branch
A merge into main has paused with a textual conflict in pricing.py. No unrelated changes are present. Arrange the actions to complete the merge. Use a workflow that tests the edited file before staging it and reviews the staged changes before creating the merge commit. Assume the tests pass and create no tracked changes.
Loading ordering steps…