git merge
Merge your work
Lesson 10 of 20 · 10 min · Module 3: Branches
A finished branch is not much use on its own. Merging brings its commits back into main.
Start this lesson →Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Recognise a fast-forward
- Create a merge commit
- Read the graph and delete merged branches
The idea
git merge branch brings that branch's commits into the branch you are on. If main has not moved since the branch started, Git just moves main forward: a fast-forward. If both sides have new commits, Git combines them and records a merge commit with two parents. git log --oneline --graph draws the result, and git branch -d name deletes a merged branch; its commits stay in the history.
git merge dark-mode
git merge contact-page
git branch -d dark-mode contact-page
git log --oneline --graph
01Fast-forward: nothing to combine
main has not moved since dark-mode started, so Git simply moves main forward to the branch tip. No new commit is needed.
git merge dark-modeUpdating 37f72cb..ed6b293
Fast-forward
style.css | 1 +
1 file changed, 1 insertion(+)02A merge commit joins two lines of work
contact-page started before main got a new commit, so both sides have new work. Git combines them and records a merge commit. On your computer, Git opens your editor with a ready message; save and close it to finish. The practice terminal accepts the message for you.
git merge contact-pageMerge made by the 'ort' strategy.
contact.md | 3 +++
menu.txt | 1 +
2 files changed, 4 insertions(+)
create mode 100644 contact.md03Read the graph, then tidy up
git log --graph draws each line of history. Once a branch is merged, its label is no longer needed: git branch -d deletes it, and refuses if the branch has commits that are not merged.
git log --oneline --graph -5
git branch -d dark-mode contact-page* abe7924 (HEAD -> main) Merge branch 'contact-page'
|\
| * 90b369a (contact-page) Add a contact page
* | ed6b293 (dark-mode) Add dark mode colours
* | 37f72cb Call it a website in the README
|/
* 4abfd2a Say what the site is about
Deleted branch dark-mode (was ed6b293).
Deleted branch contact-page (was 90b369a).Your turn
dark-mode and contact-page. On main, merge dark-mode first, then contact-page. Delete both branches once they are merged, and look at the result with git log --oneline --graph.What the checks look for
- main contains the work of both branches
- dark-mode was a fast-forward and contact-page a merge commit
- Both branches are deleted
- Shows the history as a graph
Take this with you
Merging brings a finished branch back into main, by fast-forward or with a merge commit.
Go a little further
git merge --no-ff creates a merge commit even when a fast-forward is possible. Some teams prefer it because the graph keeps the branch visible.