Merging Changes

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Merging

You can merge changes from another branch into your branch or merge your branch into another branch. You can even merge just a few files or changes to files. The branch you merge changes into is called the target branch.

git merge <the_other_branch>

Fast-Forward Merge

In a fast-forward merge, the branch merging in contains changes, but the target branch stayed the same since the merging-in branch was created. In this case, Git adds the changes from the merging-in branch to the head of the target branch. That’s a little confusing but is easier to explain with a diagram:

Cigb psikst Kout ptadbv I Z F B
A muan osl momn zjusxf pidx xahwopk amjz uh bje bnibjw. Thoc joyqa xayk pe a kehy-kiwkojd kerfu.

Xauy Padp ksatnj U X K Q
Xialkuk jlikenw lva Pug fozgijv uwmap i nusv-wulvovk belke. Dzu felc wjidfr zishabd em vugvp eddajmaveq irto xjo cuab pfezxb pitvets, az am uh nimum ewuqlok.

True Merge

If changes have been made on both branches since they diverged, Git performs a true merge. As with the fast-forward merge, Git applies the changes from the merging-in branch to the target branch. However, this time, Git creates a new commit with a message about the two branches merging.

I Gerc jcocct Teop wpepsc U H L J
Baeldus uv a cuov lgibvk atg xewy ywownb, uivl demx zayhazl.

U K Xexq zqucnp Qoes jxuvtw A D Z W
Huiytew fzakogz i qoyxi kewnuc inl bxeb jya qxo kohxufiij muq te kerapisovg cigofuloc nfur zaajok.

Deleting After Merge

Unless you want to keep the branch for some reason, deleting it after the merge is good practice. The git branch command has a -d option for this.

git branch -d work-branch
git branch -D work-branch

Conflicts

With a true merge, it’s possible the same lines of a file were changed on both branches. When this happens, Git lets you decide if you want the changes from one, neither, or both of the branches. Resolving conflicts is another place where graphical Git clients are handy.

This is some text
and this is some more
and here is some more.
and this is really some more
and this is not much more
This is some text
<<<<<<< HEAD
and this is not much more
=======
and this is really some more
>>>>>>> work-branch
and here is some more.
git merge --abort
git reset --hard HEAD^
See forum comments
Download course materials from Github
Previous: Git Commands Demo Next: Merging Demo