Phil for Humanity Phil for Humanity
A Guide for the Survival of Humankind and Helping the World, Society, and Yourself.

Git Merge

The command to merge branches in Git is basically:


This command will fully merge all of the contents (since the last full merge) from the merged from branch to the current branch that you are in, so it is strongly recommended to run "git pull " before merging to guarantee that you are merging to the latest (HEAD) version of the current branch.

By default, this command will merge with fast-forward turned on. Fast-forward merging is when the HEAD of the branch that you are merging to is where the branch that you are merging from started, so the HEAD of the merge to branch is moved to also be the HEAD of the merge from branch. As a result, no new commits are generated and the HEAD of two branches are pointing to the same commit. Of course, this makes the version tree a bit harder to read.

Alternately, you can use the no fast-forward or "--no-ff" option to force merging to the merge to branch with a new commit, even if a new commit is not needed.

git merge --no-ff MERGED_FROM_BRANCH

When merging, chances are that you will occasionally encounter merge conflicts. Fortunately, there are several methods to resolve conflicts. The first method is to first run "git status" to find the conflicting files and then to manually edit each of these files to resolve the conflict. Here is an example of how a conflict would look like in a text file.

< < < < < < <
Line(s) from the merge from branch.
Line(s) from the merge to branch.
> >>>>>>

Don't forget to remove the conflict tag lines by only leaving the lines that you want to keep in the file and nothing else.

A second method to resolve conflicts is to use the conflict merge tool that has a graphical user interface.

git mergetool

Finally, you can resolve all conflicts on a file by using all the changes exactly as they are on either the merge from ("--theirs") or merge to ("--ours") branch.

git checkout [--theirs|--ours] CONFLICT_FILE

Alternatively, if you know beforehand that your merge is going to have conflicts, you can always automatically resolve all conflicts if you want to always accept the changes from either branch. For instance, to automatically resolve all merge conflicts by bringing in the changes on the merge from (theirs) branch, use this command:

git merge --strategy=recursive --strategy-option=theirs --no-ff MERGED_FROM_BRANCH

Assuming that you manually resolved conflicts using any of the first three methods mentioned above, you need to resume the merging process using these procedures. First, add the files that you resolved the conflicts on. Second, create a new commit (without amending). And finally, do not forget to push your merged changes.

by Phil for Humanity
on 20121102

Related Articles
 » Perforce Resolve
 » Git Rebase Walk Through
 » Git Merge Fast Forward versus No Fast Forward