Git merge

$ git clone https://github.com/bmwieczorek/my-git-sandbox.git
$ cd my-git-sandbox

1. COMMIT ON MASTER
$ echo "master: initial create of file1" > file1
$ git add file1
$ git commit -m "master: initial create of file1" file1

2. CREATE BRANCH AND COMMIT ON BRANCH
$ git branch "my-branch"
$ git checkout my-branch
Switched to branch 'my-branch'
$ echo "my-branch: initial create of branch1" > branch1
$ git add branch1
$ vim file1
$ git add file1
$ git commit -m "my-branch: append new line to file1 and initial create of file2" branch1 file1
$ ls 
branch1 file1

3. COMMIT ON MASTER
$ git checkout master
Switched to branch 'master'
$ ls
file1
$ echo "master: after my-branch commit: initial create of file2" > file2
$ git add file2
$ vim file1
$ git add file1
$ cat file1 file2
master: initial create of file1
master: after my-branch commit: append new line to file1
master: after my-branch commit: initial create of file2
$ git commit -m "master: after my-branch commit: append new line to file1 and initial create of file2" file1 file2
$ git status
On branch master
$ git log
 master: after my-branch commit: append new line to file1 and initial create of file2
 master: initial create of file1
$ git checkout my-branch
Switched to branch 'my-branch'
$ git status
On branch my-branch
$ git log
 my-branch: append new line to file1 and initial create of file2
 master: initial create of file1
$ git checkout master
Switched to branch 'master'
$ ls
file1 file2

4. MERGE BRANCH CHANGES TO MASTER
$ git merge my-branch
Auto-merging file1
CONFLICT (content): Merge conflict in file1
Automatic merge failed; fix conflicts and then commit the result.
$ ls
branch1 file1 file2
$ cat file1
master: initial create of file1
<<<<<<< HEAD
master: after my-branch commit: append new line to file1
=======
my-branch: append new line
>>>>>>> my-branch
$ vim file1

5. RESOLVE CONFLICT AND COMMIT
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
You have unmerged paths.
 (fix conflicts and run "git commit")
Changes to be committed:
new file: branch1
Unmerged paths:
 (use "git add <file>..." to mark resolution)
both modified: file1
$ git add file1
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
All conflicts fixed but you are still merging.
 (use "git commit" to conclude merge)
Changes to be committed:
new file: branch1
 modified: file1
$ git log
 master: after my-branch commit: append new line to file1 and initial create of file2
 master: initial create of file1
$ git commit -m "master: after merge with my-branch and conflict resolution" branch1 file1
fatal: cannot do a partial commit during a merge.
$ git commit -m "master: after merge with my-branch and conflict resolution"
[master e01526d] master: after merge with my-branch and conflict resolution

6. MASTER LOG HISTORY (in chronological order)
$ git log
Merge: b255d9b 31a909e
master: after merge with my-branch and conflict resolution
Date: Thu Jan 28 12:50:33 2016 +0100
master: after my-branch commit: append new line to file1 and initial create of file2
Date: Thu Jan 28 12:44:18 2016 +0100
my-branch: append new line to file1 and initial create of file2
Date: Thu Jan 28 12:41:16 2016 +0100
master: initial create of file1

SECOND EXAMPLE (demonstrate chronological history)
$ echo "Hi: " > file
$ git add file 
$ git commit -am "Hi: " 
[master (root-commit) ada0512] Hi:
 1 file changed, 1 insertion(+)
 create mode 100644 file
$ git checkout -b my-branch
Switched to a new branch 'my-branch'
$ vim file 
$ cat file 
Hi: m-b
$ git commit -am "m-b" 
[my-branch 5c3bef9] m-b
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git checkout master 
Switched to branch 'master'
$ vim file 
$ git commit -am "m"
[master 9b146dc] m
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git checkout my-branch 
Switched to branch 'my-branch'
$ vim file 
$ git commit -am "m-bx2"
[my-branch 1af0332] m-bx2
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git checkout master
Switched to branch 'master'
$ vim file 
$ git commit -am "m2x"
[master e078728] m2x
 1 file changed, 1 insertion(+), 1 deletion(-)
$ cat file 
Hi: m2x
$ git checkout my-branch 
Switched to branch 'my-branch'
$ cat file 
Hi: m-bx2
$ git merge master
Auto-merging file
CONFLICT (content): Merge conflict in file
Automatic merge failed; fix conflicts and then commit the result.
$ cat file 
<<<<<<< HEAD
Hi: m-bx2
=======
Hi: m2x
>>>>>>> master
$ vim file 
$ git add file
$ git commit -am "merged master to my-branch"
[my-branch 521600f] merged master to my-branch
$ git log 
Merge: 1af0332 e078728
Date: Fri Jan 29 10:19:31 2016 +0100
 merged master to my-branch
Date: Fri Jan 29 10:16:26 2016 +0100
 m2x
Date: Fri Jan 29 10:15:38 2016 +0100
 m-bx2
Date: Fri Jan 29 10:15:01 2016 +0100
 m
Date: Fri Jan 29 10:14:31 2016 +0100
 m-b
Date: Fri Jan 29 10:13:42 2016 +0100
 Hi:
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s