Show contents
Chapter 4 of 7 · 20 min
Branches, merges and conflicts
By the end of this chapter, you'll be able to
- Create a branch, switch between branches and delete a branch
- Merge a branch with git merge
- Tell a fast-forward merge apart from a merge commit
- Resolve a merge conflict
A branch is a label
A branch is simply a label that points to a commit. When you commit, the label of the current branch moves forward one step. HEAD indicates the branch you are on. Creating a branch copies no file: it is instant.
We start from a repository with one commit:
git init
echo "<h1>Kofi's Bakery</h1>" > index.html
git add index.html
git commit -m "Add the home page"
git branch contact
git branch
contact
* main
git branch name creates the branch, git branch alone lists them. The star marks the current branch: we are still on main.
Work on a branch
git switch changes branch. The files in the working directory are updated to match the last commit of the branch:
git switch contact
echo "<p>Phone: +221 33 000 00 00</p>" > contact.html
git add contact.html
git commit -m "Add the contact page"
git switch main
ls
Switched to branch 'main'
index.html
On main, contact.html does not exist: it is only in the contact branch. To create a branch and go to it in one go, use git switch -c name. You will also come across the older git checkout command, which does the same thing (git checkout -b name to create), and much more.
Merge a branch
To bring the work of contact into main, go to main and merge:
git merge contact
Updating 3f2a9c1..7d4e8b2
Fast-forward
contact.html | 1 +
1 file changed, 1 insertion(+)
create mode 100644 contact.html
Fast-forward: main had not moved since contact was created, so Git simply moved the main label up to the last commit of contact. No new commit is created.
If both branches have moved, Git must combine their changes. It then creates a merge commit, which has two parents:
git switch -c hours
echo "Open from 7am to 7pm" > hours.txt
git add hours.txt
git commit -m "Add the opening hours"
git switch main
echo "<p>Hot bread from 7am</p>" >> index.html
git commit -am "Announce hot bread"
git merge hours
Merge made by the 'ort' strategy.
hours.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 hours.txt
In your terminal, Git first opens your text editor to let you edit the message of the merge commit (“Merge branch 'hours'”): save and close to continue. ort is the name of the merge method Git uses. The history now has two lines that join:
$ git log --oneline --graph
* a91f3c2 (HEAD -> main) Merge branch 'hours'
|\
| * 5be0d17 (hours) Add the opening hours
* | 2c7e4a9 Announce hot bread
|/
* 7d4e8b2 (contact) Add the contact page
* 3f2a9c1 Add the home page
It records the result in a merge commit whose parents are the two branches.
Text description of the animation
- Git looks for the last commit both branches have in common.
- It looks at what main changed since that ancestor.
- Then at what hours changed since the same ancestor.
- The changes do not overlap: Git keeps both.
- It records the result in a merge commit whose parents are the two branches.
A merged branch can be deleted: its work is in main.
git log --format="%s" -1
git branch -d contact hours
git branch
* main
-1 limits the history to the last commit. git branch -d refuses to delete a branch whose work has not been merged (you would need -D, to be used knowingly).
Conflicts
If both branches changed the same lines of a file, Git cannot guess which one to keep: this is a conflict. Let's set one up:
echo "The good bread of the neighbourhood" > slogan.txt
git add slogan.txt
git commit -m "Add the slogan"
git switch -c new-slogan
echo "The best bread in Dakar" > slogan.txt
git commit -am "Change the slogan"
git switch main
echo "Fresh bread every morning" > slogan.txt
git commit -am "Reword the slogan"
git merge new-slogan
Auto-merging slogan.txt
CONFLICT (content): Merge conflict in slogan.txt
Automatic merge failed; fix conflicts and then commit the result.
The merge stops halfway. Git has written both versions in the file, between conflict markers:
cat slogan.txt
git status -s
<<<<<<< HEAD
Fresh bread every morning
=======
The best bread in Dakar
>>>>>>> new-slogan
UU slogan.txt
Between <<<<<<< HEAD and =======, the version of your current branch; between ======= and >>>>>>>, the version of the merged branch. UU means “modified on both sides, not resolved yet”. To resolve: write the final content (one of the versions, or a mix), remove the markers, stage the file, then finish with a commit.
echo "Fresh bread every morning, the best in Dakar" > slogan.txt
git add slogan.txt
git commit -m "Merge the new slogan"
git log --format="%s" -1
cat slogan.txt
Merge the new slogan
Fresh bread every morning, the best in Dakar
You can also abandon a conflicted merge with git merge --abort: everything goes back to how it was before the git merge.
Your turn
Mini-lab: cause and resolve a conflictGit simulator
Terminal
Click “Run”: the commands run in a fresh repository. Type “help” for the list of commands.Teaching simulator: it reproduces the essential Git commands in your browser, with nothing to install · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
The last command fails on a conflict. Add the commands to resolve it: display the file, write the final price, stage it, finish the merge, then display git log --oneline.
Worked exercise
Worked exercise: the price list page
In the bakery's repository, create a prices branch and put in it a file prices.txt containing “Baguette: 250 FCFA”, recorded in a commit “Add the price list”. Go back to main, merge prices, then delete the branch. Finally, check that prices.txt is present on main.
Hint 1
git switch -c prices creates the branch and moves to it.Hint 2
main.Show the solution and common mistakes
git switch -c prices
echo "Baguette: 250 FCFA" > prices.txt
git add prices.txt
git commit -m "Add the price list"
git switch main
git merge prices
git branch -d prices
cat prices.txt
git branch
Baguette: 250 FCFA
* main
new-slogan
main did not move while you worked on prices: the merge is a fast-forward. The new-slogan branch, merged earlier, could be deleted too.
Common mistakes:
- Running
git merge mainfromprices: thenpricesreceives the work ofmain, the opposite of what we wanted. - Deleting the branch before merging:
git branch -drefuses precisely to avoid losing that work.
Summary
- 1A branch is a label that moves forward with each commit; HEAD indicates the current branch.
- 2git switch -c creates a branch and moves to it, git switch changes branch.
- 3git merge brings a branch into the current branch: as a fast-forward, or with a merge commit.
- 4A conflict happens when both branches changed the same lines: edit, remove the markers, git add, then git commit.
- 5git branch -d deletes a branch that has already been merged.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.