Merging is the act of applying the changes from one branch onto another branch. The above visualization shows you 2 different branches which finally come together in a final commit which is accomplished by a merge.
git merge <branch-name>
With this command the currently active branch “recieves” the changes from the branch in the given paramenter in the command
If we take the example from “branches” you can execute the following command to apply to changes from “small-feature” to the master branch. (It is required that you are currently on the master branch in the command line)
git merge small-feature
After that the following output will be shown
Updating 4bcd766..4d7056f Fast-forward source.txt | 1 + 1 file changed, 1 insertion(+)
With that the 1 line from the small-feature branch will be added to the file in the master branch.
GIT usually tries to merge the code itself as good as possible.
But sometimes GIT doesn’t know which side is correct and therefore produces a “merge conflict”.
When does a merge conflict occur?
A merge conflict occurs if you edit the same file at the same line in 2 different branches and try to merge these branches.
How does a merge conflict look like?
Lets stay with the example from “branches” but with the following adaptations:
- On the master branch the content of source.txt has been changed from “master” to “master-old”.
- On the small-feature branch the content from source.txt has been changed from “master” to “master-new”.
Now we perform a git merge small-feature while the master branch is active. With that we now get the following output:
Auto-merging source.txt CONFLICT (content): Merge conflict in source.txt Automatic merge failed; fix conflicts and then commit the result.
Now the content of source.txt looks like this:
<<<<<<< HEAD master-old ======= master-new small-feature >>>>>>> small-feature
As you can see GIT adds a few lines here:
- Start of the merge conflict section
- Separates the 2 different content from the 2 given branches
- End of the merge conflict section
How to solve a merge conflict?
Basically you have to manually “clean up” your source code, fix your logic and remove everything GIT added automatically.
In our example we want to stick with the changes from the master branch but also keep the new line from the small-feature branch.
With that our new content of our source.txt looks like this:
After editing our file and looking at the git status we can see:
On branch master You have unmerged paths. (fix conflicts and run "git commit") Unmerged paths: (use "git add <file>..." to mark resolution) both modified: source.txt no changes added to commit (use "git add" and/or "git commit -a")
This means due to the fact, that we now edited our source.txt again, we have to add and commit our newly “merged” file again to the repositoy.
git add source.txt git commit -m "merged"
The final git log looks something like this:
commit f9997203d5f7887ca30b4e69752291706d81755d (HEAD -> master) Merge: dc50e32 2d79e70 Author: Kevin Pfeifer <email@example.com> Date: Sat Jun 22 21:09:31 2019 +0200 merged commit dc50e3242da6cc17d822e3adf04ae4ca9edd62b1 Author: Kevin Pfeifer <firstname.lastname@example.org> Date: Sat Jun 22 20:53:35 2019 +0200 changed master to master-old commit 2d79e708b4f7aa2dd4898a990bfd464b8a6080f4 (small-feature) Author: Kevin Pfeifer <email@example.com> Date: Sat Jun 22 20:53:14 2019 +0200 changes master to master-new commit b71b422594ecaf924909cd7477ee73b45a2c9685 Author: Kevin Pfeifer <firstname.lastname@example.org> Date: Sat Jun 22 20:52:32 2019 +0200 added small-feature commit 3958f0b2f8f3f0473a6ef6194df4077ac4e45dc5 Author: Kevin Pfeifer <email@example.com> Date: Sat Jun 22 20:52:21 2019 +0200 master file created