Git: How to Squash your commits into one commit before merging

When you are doing a feature development, typically there are many local commits which happen, its always a good idea that squash all those commits into one commit and then merge them to the master branch.

Below are the steps to do the same
#Step1 : Start git rebase in interactive mode
$ git rebase -i master
#once you do this, you will all your commits in an editor likewise

pick 1b6f7fed87 feature1
pick 1b6f7fed85 old commit1
pick 1b6f7fed83 old commit2
pick 1b6f7fed81 old commit3

# Rebase 806f77feab..1b6f7fed87 onto 806f77feab (1 command)
#
# 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


Step3: now replace all the commits with pick to f except the top commit,
likewise 
pick 1b6f7fed87 feature1
f 1b6f7fed85 old commit1
f 1b6f7fed83 old commit2
f 1b6f7fed81 old commit3

and save the file, voila, you have successfully squashed the commits into one 

Comments

Popular posts from this blog

Apache Airflow Wait Between Tasks

Java J2EE Security Considerations

Java Spring Interview Questions