Git Branch History: Work Independently on Two Branches
Start from a shared commit
In the fresh history lab, main has notes.txt and README.md. Both branches start from one commit A. Create a feature branch and record a change to notes.
git switch -c featureprintf "Tea\nMilk\n" > notes.txtgit add -- notes.txtgit commit -m "Add milk to notes"git switch mainFeature now points to B, whose parent is A; main still points to A. The feature commit remains recorded although it is not the active branch.
Create another line of development
On main, change the README and commit. Then inspect all branch histories. A parent relationship points from a commit to the earlier commit on which it was based.
printf "Shopping project\nPlan weekly\n" > README.mdgit add -- README.mdgit commit -m "Document weekly plan"git log --graph --oneline --decorate --allConceptual graph: feature → B → A and HEAD → main → C → A. A is a common ancestor of B and C; neither tip includes the other tip in its parent history. Git log may draw the lanes in a different order.
Inspect both snapshots without switching
Compare the recorded trees using branch names as revisions. Inspect one historical file directly and verify the working tree is still on main.
git diff --name-status main featuregit show feature:notes.txtgit branch --show-currentgit status --shortThe comparison reports changes in README.md and notes.txt. The feature version of notes includes Milk, but the main working file still contains Tea. Integration is the next chapter; comparison alone changes no files.
Practice
Draw both parent arrows, label HEAD, then predict the file contents when switching to feature.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Git Branch History: Work Independently on Two Branches — knowledge check
Reason about branch references, snapshots and safe local operations.
A is parent of both B and C. feature points to B and main to C. Which is a common ancestor?
After committing on feature and switching to main, where is the feature commit?
Which command shows both local branch histories as a graph?
Which reads notes.txt from feature without switching?
What does git diff main feature do?