Squashing Commits


Step by Step Instructions



1.Run the following command to see list of your most recent commits

$ git log --pretty=oneline --abbrev-commit

This might look some thing like this-
ddef6dd First commit message
02221a7 Second commit message
74abb12 Third commit message
8ca46ba Fourth commit message
7255abb Fifth commit message

...other commits from where you forked Push...


2.Your goal is to squash the first three most recent commits(ddef6dd, 02221a7,74abb12).For this run the following command-

$ git rebase -i HEAD~3

Your editor will open with a file that looks something like this (note that the commits are listed in reverse chronological order):
pick 74abb12 Third commit message
pick 02221a7 Second commit message
pick ddef6dd First commit message


# Rebase ddef6dd..7255abb onto 7255abb

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


3.Change the file so that squash appears next to the commits you wish to squash, leaving pick next to the commit message you intende to preserve. A new editor may open, in which you can change the commit message if you wish. Otherwise, save and close the file, and you’re done.



4.If you have pushed to your remote prior to squashing commits, you will need to run git push -f with the name of your branch to force GitHub to update your remote repository.