Git interactive rebase with squash

Scenario:
1. clone repo, create file, commit and push origin master
2. create feature-branch, commit new feature to feature-branch
3. checkout master, commit security fix to master
4. checkout feature-branch, commit correction for new feature
5. interactive rebase with master, squash new feature and correction to new feature into new one commit (history will contain only new combined without 2 previous)
6. checkout master, fast-forward merge with feature-branch to master
History after pull (with merge): 
Commands:
git clone https://github.com/bmwieczorek/git-squash.git
cd git-squash
echo "master: file create" >> file.txt
git add file.txt
git commit -m "master: file create" file.txt
git push origin master
git checkout -b feature-branch
echo "feature-branch: added new feature to file" >> file.txt
git add file.txt
git commit -m "feature-branch: added new feature to file.txt"
git checkout master
echo "master: security fix in file.txt" >> file.txt
git add file.txt
git commit -m "master: security fix in file.txt" file.txt
git checkout feature-branch
echo "feature-branch: correction for new feature in file.txt" >> file.txt
git add file.txt
git commit -m "feature-branch: correction for new feature in file.txt" file.txt
git log --pretty=oneline --abbrev-commit --decorate
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
git rebase -i master # replace pick with squash for correction commit
cat file.txt
vim file.txt # edit conflict for applying 'new feature' on top of 'security fix'
cat file.txt
git status
git rebase --continue # provide commit message for commit aggregating new feature and correction
cat file.txt
git status
git log --pretty=oneline --abbrev-commit --decorate
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
git checkout master
git merge feature-branch #fast-forward
git log --pretty=oneline --abbrev-commit --decorate
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
Results:
$ git clone https://github.com/bmwieczorek/git-squash.git
Cloning into 'git-squash'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
$ cd git-squash
$ echo "master: file create" >> file.txt
$ git add file.txt
$ git commit -m "master: file create" file.txt
[master (root-commit) a9d23b4] master: file create
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt
$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 244 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://bmwieczorek@github.com/bmwieczorek/git-squash.git
 * [new branch] master -> master
$ git checkout -b feature-branch
Switched to a new branch 'feature-branch'
$ echo "feature-branch: added new feature to file" >> file.txt
$ git add file.txt
$ cat file.txt
master: file create
feature-branch: added new feature to file
$ git commit -m "feature-branch: added new feature to file.txt"
[feature-branch 675a6ba] feature-branch: added new feature to file.txt
 1 file changed, 1 insertion(+)
$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
$ echo "master: security fix in file.txt" >> file.txt
$ git add file.txt
$ git commit -m "master: security fix in file.txt" file.txt
[master d9aff30] master: security fix in file.txt
 1 file changed, 1 insertion(+)
$ git checkout feature-branch
Switched to branch 'feature-branch'
$ echo "feature-branch: correction for new feature in file.txt" >> file.txt
$ git add file.txt
$ git commit -m "feature-branch: correction for new feature in file.txt" file.txt
[feature-branch 83661c5] feature-branch: correction for new feature in file.txt
 1 file changed, 1 insertion(+)
$ git log --pretty=oneline --abbrev-commit --decorate
83661c5 (HEAD -> feature-branch) feature-branch: correction for new feature in file.txt
675a6ba feature-branch: added new feature to file.txt
a9d23b4 (origin/master) master: file create
$ 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
* 83661c5 - Tue, 16 Feb 2016 13:49:03 +0100 (8 seconds ago) (HEAD -> feature-branch)
| feature-branch: correction for new feature in file.txt - Bartosz Wieczorek
* 675a6ba - Tue, 16 Feb 2016 13:47:53 +0100 (78 seconds ago)
| feature-branch: added new feature to file.txt - Bartosz Wieczorek
| * d9aff30 - Tue, 16 Feb 2016 13:48:08 +0100 (63 seconds ago) (master)
|/ master: security fix in file.txt - Bartosz Wieczorek
* a9d23b4 - Tue, 16 Feb 2016 13:44:44 +0100 (4 minutes ago) (origin/master)
 master: file create - Bartosz Wieczorek
$ git rebase -i master
error: could not apply 675a6ba... feature-branch: added new feature to file.txt

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
Could not apply 675a6ba490991b5a85b662ec0043e87b6b5e61f2... feature-branch: added new feature to file.txt

***MESSAGE***

pick 675a6ba feature-branch: added new feature to file.txt
pick 83661c5 feature-branch: correction for new feature in file.txt

# Rebase d9aff30..83661c5 onto d9aff30 (2 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


*** CHANGED TO ***
squash 83661c5 feature-branch: correction for new feature in file.txt

$ cat file.txt
master: file create
<<<<<<< HEAD
master: security fix in file.txt
=======
feature-branch: added new feature to file
>>>>>>> 675a6ba... feature-branch: added new feature to file.txt

$ vim file.txt # resolve conflict when applying 'new feature' on top of security fix
$ git status
interactive rebase in progress; onto d9aff30
Last command done (1 command done):
 pick 675a6ba feature-branch: added new feature to file.txt
Next command to do (1 remaining command):
 squash 83661c5 feature-branch: correction for new feature in file.txt
 (use "git rebase --edit-todo" to view and edit)
You are currently rebasing branch 'feature-branch' on 'd9aff30'.
 (fix conflicts and then run "git rebase --continue")
 (use "git rebase --skip" to skip this patch)
 (use "git rebase --abort" to check out the original branch)
Unmerged paths:
 (use "git reset HEAD <file>..." to unstage)
 (use "git add <file>..." to mark resolution)
 both modified: file.txt
no changes added to commit (use "git add" and/or "git commit -a")

$ git log --pretty=oneline --abbrev-commit --decorate
d9aff30 (HEAD, master) master: security fix in file.txt
a9d23b4 (origin/master) master: file create

$ 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
* 83661c5 - Tue, 16 Feb 2016 13:49:03 +0100 (3 minutes ago) (feature-branch)
| feature-branch: correction for new feature in file.txt - Bartosz Wieczorek
* 675a6ba - Tue, 16 Feb 2016 13:47:53 +0100 (4 minutes ago)
| feature-branch: added new feature to file.txt - Bartosz Wieczorek
| * d9aff30 - Tue, 16 Feb 2016 13:48:08 +0100 (4 minutes ago) (HEAD, master)
|/ master: security fix in file.txt - Bartosz Wieczorek
* a9d23b4 - Tue, 16 Feb 2016 13:44:44 +0100 (7 minutes ago) (origin/master)
 master: file create - Bartosz Wieczorek

$ cat file.txt
master: file create
master: security fix in file.txt
feature-branch: added new feature to file

$ git status
interactive rebase in progress; onto d9aff30
Last command done (1 command done):
 pick 675a6ba feature-branch: added new feature to file.txt
Next command to do (1 remaining command):
 squash 83661c5 feature-branch: correction for new feature in file.txt
 (use "git rebase --edit-todo" to view and edit)
You are currently rebasing branch 'feature-branch' on 'd9aff30'.
 (all conflicts fixed: run "git rebase --continue")
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)
 modified: file.txt

$ git rebase --continue

***MESSAGE***

feature-branch: added new feature to file.txt

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# interactive rebase in progress; onto d9aff30
# Last command done (1 command done):
# pick 675a6ba feature-branch: added new feature to file.txt
# Next command to do (1 remaining command):
# squash 83661c5 feature-branch: correction for new feature in file.txt
# You are currently rebasing branch 'feature-branch' on 'd9aff30'.
#
# Changes to be committed:
# modified: file.txt
#

*** CHANGED TO ***
feature-branch: interactive rebase --continue: added new feature to file.txt


***MESSAGE***

# This is a combination of 2 commits.
# The first commit's message is:

feature-branch: interactive rebase continue: added new feature to file.txt

# This is the 2nd commit message:

feature-branch: correction for new feature in 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 13:47:53 2016 +0100
#
# interactive rebase in progress; onto d9aff30
# Last commands done (2 commands done):
# pick 675a6ba feature-branch: added new feature to file.txt
# squash 83661c5 feature-branch: correction for new feature in file.txt
# No commands remaining.
# You are currently editing a commit while rebasing branch 'feature-branch' on 'd9aff30'.
#
# Changes to be committed:
# modified: file.txt
#

***ADDED ADDITIONAL MESSASE ***
feature-branch: squashed commit for new feature and correction


[detached HEAD e897028] feature-branch: interactive rebase continue: added new feature to file.txt
 1 file changed, 1 insertion(+)
[detached HEAD 922bdeb] feature-branch: squashed commit for new feature and correction
 Date: Tue Feb 16 13:47:53 2016 +0100
 1 file changed, 2 insertions(+)
Successfully rebased and updated refs/heads/feature-branch.

$ cat file.txt
master: file create
master: security fix in file.txt
feature-branch: added new feature to file
feature-branch: correction for new feature in file.txt

$ git log --pretty=oneline --abbrev-commit --decorate
922bdeb (HEAD -> feature-branch) feature-branch: squashed commit for new feature and correction
d9aff30 (master) master: security fix in file.txt
a9d23b4 (origin/master) master: file create

$ 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
* 922bdeb - Tue, 16 Feb 2016 13:47:53 +0100 (9 minutes ago) (HEAD -> feature-branch)
| feature-branch: squashed commit for new feature and correction - Bartosz Wieczorek
* d9aff30 - Tue, 16 Feb 2016 13:48:08 +0100 (8 minutes ago) (master)
| master: security fix in file.txt - Bartosz Wieczorek
* a9d23b4 - Tue, 16 Feb 2016 13:44:44 +0100 (12 minutes ago) (origin/master)
 master: file create - Bartosz Wieczorek

$ git status
On branch feature-branch
nothing to commit, working directory clean

$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
 (use "git push" to publish your local commits)

$ git merge feature-branch
Updating d9aff30..922bdeb
Fast-forward
 file.txt | 2 ++
 1 file changed, 2 insertions(+)

$ git log --pretty=oneline --abbrev-commit --decorate
922bdeb (HEAD -> master, feature-branch) feature-branch: squashed commit for new feature and correction
d9aff30 master: security fix in file.txt
a9d23b4 (origin/master) master: file create

$ 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
* 922bdeb - Tue, 16 Feb 2016 13:47:53 +0100 (10 minutes ago) (HEAD -> master, feature-branch)
| feature-branch: squashed commit for new feature and correction - Bartosz Wieczorek
* d9aff30 - Tue, 16 Feb 2016 13:48:08 +0100 (10 minutes ago)
| master: security fix in file.txt - Bartosz Wieczorek
* a9d23b4 - Tue, 16 Feb 2016 13:44:44 +0100 (13 minutes ago) (origin/master)
 master: file create - Bartosz Wieczorek

$ cat file.txt
master: file create
master: security fix in file.txt
feature-branch: added new feature to file
feature-branch: correction for new feature in file.txt

$ git log
commit 922bdeb14032364c1086682139bcb74fb61184f2
Author: Bartosz Wieczorek <bmwieczorek@gmail.com>
 feature-branch: squashed commit for new feature and correction
 feature-branch: interactive rebase continue: added new feature to file.txt
 feature-branch: correction for new feature in file.txt
commit d9aff30b13447b952e17f2fe9b6eee1d77d33e2e
Date: Tue Feb 16 13:48:08 2016 +0100
 master: security fix in file.txt
commit a9d23b4f3091a0db7e045fce43165cd54273634a
Date: Tue Feb 16 13:44:44 2016 +0100
 master: file create







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