git merge –squash

'git merge --squash feature-branch' command takes all the commits from feature-branch, combines them into one commit and updates the stage area and working directory in master but does NOT automatically commits them into master. After veryfing the changes you need commit the modified files manually. Note the branch and master do NOT join eventually and master contains only one merged commit.

mkdir git-merge-squash
cd git-merge-squash
git init
echo "master: initial create of file" > file.txt
git add file.txt
git commit -m "master: initial create of file" file.txt
git checkout -b "feature-branch"
echo "feature-branch: updated 1x file.txt" >> file.txt
git commit -a -m "feature-branch: updated 1x file.txt"
echo "feature-branch: updated 2x file.txt" >> file.txt
git commit -a -m "feature-branch: updated 2x file.txt"
git checkout master
git merge --squash feature-branch

$ git status
On branch master
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)
modified: file.txt
$ git diff file.txt
$ cat file.txt
master: initial create of file
feature-branch: updated 1x file.txt
feature-branch: updated 2x file.txt
$ git diff HEAD
diff --git a/file.txt b/file.txt
index e760575..96d40b4 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1,3 @@
 master: initial create of file
+feature-branch: updated 1x file.txt
+feature-branch: updated 2x file.txt
$ git commit -am "master: merged feature branch to master (merge --squash)"
[master 5ce8ac9] master: merged feature branch to master (merge --squash)
 1 file changed, 2 insertions(+)
$ git log --decorate --abbrev-commit
commit 5ce8ac9 (HEAD -> master)
Date: Fri Feb 19 15:10:35 2016 +0100
master: merged feature branch to master (merge --squash)
commit 5c432b9
Date: Fri Feb 19 14:37:35 2016 +0100
master: initial create of file

$ 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
* 5ce8ac9 - Fri, 19 Feb 2016 15:10:35 +0100 (26 seconds ago) (HEAD -> master)
| master: merged feature branch to master (merge --squash) - Bartosz Wieczorek
| * f2667ad - Fri, 19 Feb 2016 14:37:35 +0100 (33 minutes ago) (feature-branch)
| | feature-branch: updated 2x file.txt - Bartosz Wieczorek
| * dd4c796 - Fri, 19 Feb 2016 14:37:35 +0100 (33 minutes ago)
|/ feature-branch: updated 1x file.txt - Bartosz Wieczorek
* 5c432b9 - Fri, 19 Feb 2016 14:37:35 +0100 (33 minutes ago)
 master: initial create of file - Bartosz Wieczorek




					
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