merge
This is an example of a merge of divergent branches. It shows commands I find useful when performing merges.
Here is a simple scenario using two branches that have diverged. Notice the use of the --all option in the second log command.
Rich@RICH-PC ~/workspace/perl/bin (master)
$ git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset %C(yellow)(%ad)%Creset'
* e294d15 - (HEAD, master) Change test 44. (7 seconds ago) <Richard Weeks> (Sat Apr 4 15:54:09 2015 +0100)
* f39e358 - (origin/master) Use shift operator. (2 hours ago) <Richard Weeks> (Sat Apr 4 13:28:26 2015 +0100)
* d0c57af - Initial version. (24 hours ago) <Richard Weeks> (Fri Apr 3 15:16:49 2015 +0100)
Rich@RICH-PC ~/workspace/perl/bin (master)
$ git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset %C(yellow)(%ad)%Creset' --all
* e294d15 - (HEAD, master) Change test 44. (7 seconds ago) <Richard Weeks> (Sat Apr 4 15:54:09 2015 +0100)
| * 3f5fddb - (branch-1) Add test for value 24. (47 minutes ago) <Richard Weeks> (Sat Apr 4 15:07:36 2015 +0100)
| * f50a637 - Add another test. (63 minutes ago) <Richard Weeks> (Sat Apr 4 14:51:45 2015 +0100)
|/
* f39e358 - (origin/master) Use shift operator. (2 hours ago) <Richard Weeks> (Sat Apr 4 13:28:26 2015 +0100)
* d0c57af - Initial version. (25 hours ago) <Richard Weeks> (Fri Apr 3 15:16:49 2015 +0100)
In a more complex situation it is useful to find the common ancestor of the two different branches.
Rich@RICH-PC ~/workspace/perl/bin (master)
$ git merge-base branch-1 master
f39e3581114cf379f0c263a85c3dd14ac40130de
It is often useful to know which commits are missing from a branch. The command git log b1..b2 reports which commits in b2 are not in b1.
$ git log master..branch-1
commit 3f5fddb654189e0fc76250659b1c5b650f3bf727
Author: Richard Weeks <rich@controlcode.co.uk>
Date: Sat Apr 4 15:07:36 2015 +0100
Add test for value 24.
commit f50a6373e5fa933f6a1ab7fc58356a9571c72125
Author: Richard Weeks <rich@controlcode.co.uk>
Date: Sat Apr 4 14:51:45 2015 +0100
Add another test.
To merge the branches use the command git merge --no-ff branch-1
Rich@RICH-PC ~/workspace/perl/bin (master)
$ git merge --no-ff branch-1
Auto-merging bin/binary
CONFLICT (content): Merge conflict in bin/binary
Automatic merge failed; fix conflicts and then commit the result.
Rich@RICH-PC ~/workspace/perl/bin (master|MERGING)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: binary
Let's edit the file to see the conflict
print "Binary(0) = " . binary(0) . "\n";
print "Binary(1) = " . binary(1) . "\n";
<<<<<<< HEAD
print "Binary(42) = " . binary(42) . "\n";
=======
print "Binary(44) = " . binary(44) . "\n";
print "Binary(15) = " . binary(15) . "\n";
print "Binary(24) = " . binary(24) . "\n";
>>>>>>> branch-1
Git automatically adds conflict markers to the file. A conflict-marked area begins with <<<<<<< and ends with >>>>>>>. The two conflicting blocks themselves are divided by =======. So you can see the edit of test value 44 to 42 in branch master has caused the conflict. Git cannot determine which line should remain. Lets edit the file, remove the line with value 42 and the conflict markers.
When complete use git add and git commit for the file.
$ git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset %C(yellow)(%ad)%Creset' --all
* b888b8b - (HEAD, master) Merge branches. (8 seconds ago) <Richard Weeks> (Sat Apr 4 16:11:10 2015 +0100)
|\
| * 3f5fddb - (branch-1) Add test for value 24. (64 minutes ago) <Richard Weeks> (Sat Apr 4 15:07:36 2015 +0100)
| * f50a637 - Add another test. (80 minutes ago) <Richard Weeks> (Sat Apr 4 14:51:45 2015 +0100)
* | e294d15 - Change test 44. (17 minutes ago) <Richard Weeks> (Sat Apr 4 15:54:09 2015 +0100)
|/
* f39e358 - (origin/master) Use shift operator. (3 hours ago) <Richard Weeks> (Sat Apr 4 13:28:26 2015 +0100)
* d0c57af - Initial version. (25 hours ago) <Richard Weeks> (Fri Apr 3 15:16:49 2015 +0100)