Skip to content

Git Merge

Raven edited this page May 11, 2022 · 1 revision

Automatic merging

When the differences between two branches do not conflict, git will be able to finish a branch merge without the user intervention. Given the repo state shown below:

Git merge before
# Last commit in master
master> git show master
commit 7d7d0a244a58a95c4017c497199305011010a335
Author: Pepe Barbe <pepe@radiasoft.net>
Date:   Fri Sep 16 11:40:14 2016 -0500

    add b

diff --git a/b b/b
new file mode 100644
index 0000000..e69de29

# Last commit in branch
master> git show branch
commit 153740e3111e932c50da66fad2972bf1f87f2268
Author: Pepe Barbe <pepe@radiasoft.net>
Date:   Fri Sep 16 11:40:31 2016 -0500

    add c

diff --git a/c b/c
new file mode 100644
index 0000000..e69de29

# master contents
master> ls -lah
a b

# Merge branch into master
master> git merge branch
Merge made by the 'recursive' strategy.
 c | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 c
 
# Last commit in branch is a merge commit now
master> git show
commit 44dfdec394e518d8fd58bfe37bde14c896f4ba0a
Merge: 7d7d0a2 153740e
Author: Pepe Barbe <pepe@radiasoft.net>
Date:   Fri Sep 16 11:49:43 2016 -0500

    Merge branch 'branch'
    
# master contents
master> ls -lah
a b c

The state of the repo after the operation will be:

Git merge after

Merge conflicts

Given the state shown below:

Git merge conflict before
# Last commit in master, lines added to c
master> git show master
commit f3b84b38591c2b51f79cf0dc354d024dbfb5d7d4
Author: Pepe Barbe <pepe@radiasoft.net>
Date:   Fri Sep 16 12:12:09 2016 -0500

    Change b and c

diff --git a/b b/b
index e69de29..0cfbf08 100644
--- a/b
+++ b/b
@@ -0,0 +1 @@
+2
diff --git a/c b/c
index e69de29..0cfbf08 100644
--- a/c
+++ b/c
@@ -0,0 +1 @@
+2

# Last commit in branch, lines added to c
master> git show branch
commit 8ecb7091e78cbf3055374fdb1a2a47e2db641ed1
Author: Pepe Barbe <pepe@radiasoft.net>
Date:   Fri Sep 16 12:11:43 2016 -0500

    Change a and c

diff --git a/a b/a
index e69de29..d00491f 100644
--- a/a
+++ b/a
@@ -0,0 +1 @@
+1
diff --git a/c b/c
index e69de29..d00491f 100644
--- a/c
+++ b/c
@@ -0,0 +1 @@
+1

# Merge branch into master
master> git merge branch
Auto-merging c
CONFLICT (content): Merge conflict in c
Automatic merge failed; fix conflicts and then commit the result.

# Show status of repo, a was modified in branch, but the modification did 
# not cause conflicts, while c was modified in in such a way that Git 
# cannot perform automatic merging. 
master> git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Changes to be committed:

	modified:   a

Unmerged paths:
  (use "git add <file>..." to mark resolution)

	both modified:   c

# Below we see the contents of c after the failed merge. Areas in the file 
# that are conflicting will have the format below, where the contents 
# below HEAD are the conflicting values in the file in the current branch, 
# and the values above branch are the values in the same area for the 
# branch.
master> cat c
<<<<<<< HEAD
2
=======
1
>>>>>>> branch

# The conflict needs to be resolved manually.
master> cat c
1 < 2

# Add the fixed files to the staging index
master> git add c 
master> git status
On branch master
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:

	modified:   a
	modified:   c

# When all conflicts have been resolved and added, commit
master> git commit

master>  git show
commit 2512b58abe16fa1b13e3ea9eda8e4fbd623e32ab
Merge: f3b84b3 8ecb709
Author: Pepe Barbe <pepe@radiasoft.net>
Date:   Fri Sep 16 12:21:09 2016 -0500

    Merge branch 'branch'

diff --cc c
index 0cfbf08,d00491f..17e69c7
--- a/c
+++ b/c
@@@ -1,1 -1,1 +1,1 @@@
- 2
 -1
++1 < 2

Git state after the merge:

Git merge conflict after