retrography
11/17/2015 - 4:27 PM

Squashing the last pushed commits

Squashing the last pushed commits

  1. If you have branched out from the master

Make sure your local master clone is up to date

git checkout master
git pull remoteRepoName master

Then

git rebase -i master
  1. If you haven't branched out

Get a list of latest commits:

git log --pretty=oneline

Either get the SHA for the commit before the commits you want to combine, or count the number of commits you would like to combine.

Then:

git rebase -i HEAD~3

or:

git rebase -i b76d157

Which gives you

pick fb554f5 This is commit 1
pick 2bd1903 This is commit 2
pick d987ebf This is commit 3

# Rebase 9cbc329..d987ebf onto 9cbc329
#
# 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.
#

Pick the first and squash the rest

pick fb554f5 This is commit 1
squash 2bd1903 This is commit 2
squash d987ebf This is commit 3

# Rebase 9cbc329..d987ebf onto 9cbc329
#
# 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.
#

After saving, a new comment editor opens. Enter a comment for the newly minted commit (you may want to remove or keep the old ones).

Then force push

git push -f