git merge – commit all what was merged

Note that when doing git pull (with default merge) git will apply all developers changes in-between your changes. That means that you may see modified files that you did not worked on (here master.txt). You need to commit and push them back to the upstream (do not exclude them unless they were wrong and you tell that explicitly). Otherwise pushing partial changes would lead to losing others work.

On the other hand with git pull –rebase (or git rebase) you first take others changes and apply your changes on the top of them so that your changes go as last ones. That is much safer as you know your changes and you will not omit accidentialy others work when pushing to main repo.

Example:

mkdir git-merge
cd git-merge
git init
echo "master: created master.txt" > master.txt
echo "master: created common.txt" > common.txt
git add .
git commit -am "master: created common.txt and master.txt"
git checkout -b feature-xx
echo "feature-xx: created feature-xx.txt" >> feature-xx.txt
git add feature-xx.txt
echo "feature-xx: append 1x common.txt" >> common.txt
git commit -am "feature-xx: append 1x common and created feature-xx.txt"
git checkout master
echo "master: append 1x master.txt" >> master.txt
echo "master: append 1x common.txt" >> common.txt
git commit -am "master: append 1x master.txt and common.txt"
git checkout feature-xx
echo "feature-xx: append 1x feature-xx.txt" >> feature-xx.txt
echo "feature-xx: append 2x common.txt" >> common.txt
git commit -am "feature-xx: append 1x feature-xx.txt and 2x common.txt"
$ git merge master
Auto-merging common.txt
CONFLICT (content): Merge conflict in common.txt
Automatic merge failed; fix conflicts and then commit the result.
$ git status
On branch feature-xx
You have unmerged paths.
 (fix conflicts and run "git commit")
Changes to be committed:
 modified: master.txt
Unmerged paths:
 (use "git add <file>..." to mark resolution)
 both modified: common.txt
$ explorer.exe .
$ cat common.txt
master: created common.txt
<<<<<<< HEAD
feature-xx: append 1x common.txt
feature-xx: append 2x common.txt
||||||| merged common ancestors
=======
master: append 1x common.txt
>>>>>>> master
$ vim common.txt
$ cat common.txt
master: created common.txt
feature-xx: append 1x common.txt
feature-xx: append 2x common.txt
master: append 1x common.txt
$ git add common.txt # resolved conflict
tortoise-commit
Here developer only selected common.txt as that was the only file he worked on the feature-xx branch. He skipped committing master.txt since he did not touch it. Hoever, that master.txt had a very important fix and he should have commited that. Note also very enigmatic default commit message that suggest only conflicted commit.txt has changed.
$ git status
On branch feature-xx
Changes not staged for commit:
 (use "git add <file>..." to update what will be committed)
 (use "git checkout -- <file>..." to discard changes in working directory)
 modified: master.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git log --decorate --abbrev-commit
commit c9a33be (HEAD -> feature-xx)
Merge: 7f3b96f b331881
Date: Wed Feb 24 17:49:22 2016 +0100
 Merge branch 'master' into feature-xx
 # Conflicts:
 # common.txt
commit 7f3b96f
Date: Wed Feb 24 17:46:55 2016 +0100
 feature-xx: append 1x feature-xx.txt and 2x common.txt
commit b331881 (master)
Date: Wed Feb 24 17:46:54 2016 +0100
 master: append 1x master.txt and common.txt
commit 64e89e9
Date: Wed Feb 24 17:46:54 2016 +0100
 feature-xx: append 1x common and created feature-xx.txt
commit 3a52a79
Date: Wed Feb 24 17:46:54 2016 +0100
 master: created common.txt and master.txt
$ git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
* c9a33be - Wed, 24 Feb 2016 17:49:22 +0100 (7 minutes ago) (HEAD -> feature-xx)
|\ Merge branch 'master' into feature-xx - Bartosz Wieczorek
| * b331881 - Wed, 24 Feb 2016 17:46:54 +0100 (10 minutes ago) (master)
| | master: append 1x master.txt and common.txt - Bartosz Wieczorek
* | 7f3b96f - Wed, 24 Feb 2016 17:46:55 +0100 (10 minutes ago)
| | feature-xx: append 1x feature-xx.txt and 2x common.txt - Bartosz Wieczorek
* | 64e89e9 - Wed, 24 Feb 2016 17:46:54 +0100 (10 minutes ago)
|/ feature-xx: append 1x common and created feature-xx.txt - Bartosz Wieczorek
* 3a52a79 - Wed, 24 Feb 2016 17:46:54 +0100 (10 minutes ago)
 master: created common.txt and master.txt - Bartosz Wieczorek
$ git push
$ git diff HEAD master.txt
diff --git a/master.txt b/master.txt
index b076f41..6900433 100644
--- a/master.txt
+++ b/master.txt
@@ -1 +1,2 @@
 master: created master.txt
+master: append 1x master.txt # that fix did not go to the repository

					
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