Merge (squash) multiple git commits into one


How to combine multiple git commits as one commit?


Step 1:


git rebase -i HEAD~n # n is the number of commits you want to merge.

-i means interactive.

Step 2: You will see interactive git screen:

pick fda59df commit 1
pick x536897 commit 2
pick c01a668 commit 3
# Rebase 49687a0..d426a8a onto 49687a0

# # 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 

# # If you remove a line here THAT COMMIT WILL BE LOST. # However, if you remove everything, the rebase will be aborted. #


Step 3: 

To merge all commit to one we can edit above content as  following, we can also reorder commits by moving them around:

squash c01a668 commit 3
pick fda59df commit 1
squash x536897 commit 2

Don’t need to work on each commit as they will be ignored. After saving the squash settings, your editor will open once more to ask for a commit message for the squashed commit.


You can cancel rebase any time by:

git rebase --abort