Monday, August 24, 2015

git - how to combine 2 commits into one single commit


-Say we have 2 commit 4c87195 ( It has first.txt file ) and bfa1b94 ( It has second.txt file )

$ git log
* bfa1b94 (HEAD, topic) second commit - commit-2
* 4c87195 first commit - commit-1
* 52d4c41 (master) inital repo - commit zero


-We want to combine the last 2 commits in one commit

-We need to un-index ( un-stage) both commit first.

$ git reset --soft HEAD~2
$ git s
# On branch topic
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#       new file:   first.txt
#       new file:   second.txt
#


-Then, create a new commit using un-index ( un-stage) file.

$ git commit -am "combined commit - using first and second commit
$ git log
* 6941ac6 (HEAD, topic) combined commit - using first and second commit
* 52d4c41 (master) inital repo - commit zero


-New commit 6941ac6  will have files from last 2 commits

$ git show |grep +++
+++ b/first.txt
+++ b/second.txt