git squash commits with interactive rebase

$ mkdir git-branch-squash
$ cd git-branch-squash
$ git init
Initialized empty Git repository in /cygdrive/c/dev/git-branch-squash/.git/
$ echo "master: initial create of file.txt" > file.txt
$ git add file.txt
$ git commit -m "master: initial create of file.txt" file.txt
[master (root-commit) 71cfb6b] master: initial create of file.txt
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt
$ git checkout -b feature-branch
Switched to a new branch 'feature-branch'
$ echo "feature-branch: start working on file.txt" >> file.txt 
$ git commit -a -m "feature-branch: start working on file.txt"
[feature-branch 7a1d64a] feature-branch: start working on file.txt
 1 file changed, 1 insertion(+)
$ echo "feature-branch: fix 1 for file.txt" >> file.txt
$ git commit -a -m "feature-branch: fix 1 for file.txt"
[feature-branch ee78458] feature-branch: fix 1 for file.txt
 1 file changed, 1 insertion(+)
$ echo "feature-branch: fix 2 for file.txt" >> file.txt
$ git commit -a -m "feature-branch: fix 2 for file.txt"
[feature-branch 9f51537] feature-branch: fix 2 for file.txt
 1 file changed, 1 insertion(+)
$ echo "feature-branch: fix 3 for file.txt" >> file.txt
$ git commit -a -m "feature-branch: fix 3 for file.txt"
[feature-branch 7efe54e] feature-branch: fix 3 for file.txt
 1 file changed, 1 insertion(+)

$ git log --graph --pretty=oneline --abbrev-commit --decorate
* 7efe54e (HEAD -> feature-branch) feature-branch: fix 3 for file.txt
* 9f51537 feature-branch: fix 2 for file.txt
* ee78458 feature-branch: fix 1 for file.txt
* 7a1d64a feature-branch: start working on file.txt
* 71cfb6b (master) master: initial create of file.txt

$ git rebase -i HEAD~4 # squash all 4 commits into one by manually changing pick to squash for last 3 commits

# message window for interactive rebase: #

pick 7a1d64a feature-branch: start working on file.txt sqaush
squash ee78458 feature-branch: fix 1 for file.txt # manually renamed pick to squash
squash 9f51537 feature-branch: fix 2 for file.txt # manually renamed pick to squash
squash 7efe54e feature-branch: fix 3 for file.txt # manually renamed pick to squash
# Rebase 71cfb6b..7efe54e onto 71cfb6b (4 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
# Note that empty commits are commented out

# message to edit message of the commit replacing squashed commits: #

# This is a combination of 4 commits.
feature-branch: implemented new feature (squashed commit) # comment added manually
# The first commit's message is:
feature-branch: start working on file.txt
# This is the 2nd commit message:
feature-branch: fix 1 for file.txt
# This is the 3rd commit message:
feature-branch: fix 2 for file.txt
# This is the 4th commit message:
feature-branch: fix 3 for file.txt
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Date: Tue Feb 16 18:25:02 2016 +0100
# interactive rebase in progress; onto 71cfb6b
# Last commands done (4 commands done):
# squash 9f51537 feature-branch: fix 2 for file.txt
# squash 7efe54e feature-branch: fix 3 for file.txt
# No commands remaining.
# You are currently editing a commit while rebasing branch 'feature-branch' on '71cfb6b'.
# Changes to be committed:
# modified: file.txt

[detached HEAD d03dc43] feature-branch: implemented new feature (squashed commit)
 Date: Tue Feb 16 18:25:02 2016 +0100
 1 file changed, 4 insertions(+)
Successfully rebased and updated refs/heads/feature-branch.

$ git log --graph --pretty=oneline --abbrev-commit --decorate
* d03dc43 (HEAD -> feature-branch) feature-branch: implemented new feature (squashed commit)
* 71cfb6b (master) master: initial create of file.txt

$ git log --abbrev-commit --decorate
commit d03dc43 (HEAD -> feature-branch)
Date: Tue Feb 16 18:25:02 2016 +0100
 feature-branch: implemented new feature (squashed commit)
 feature-branch: start working on file.txt
 feature-branch: fix 1 for file.txt
 feature-branch: fix 2 for file.txt
 feature-branch: fix 3 for file.txt
commit 71cfb6b (master)
Date: Tue Feb 16 18:24:00 2016 +0100
 master: initial create of file.txt
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