Cover image
anonymoussc
Feb 29, 2016 • 3 min read

How to remove several commit from github and squash them into one through rebase

In Git there are two main ways to integrate changes from one branch into another the merge and the rebase. For example, we will undo the last 3 push from remote.

“Undo” the last 3 push from remote repository

# "Undo" (for example) the last 3 pushes from remote repository
git push -f origin HEAD^^^:master

# or also can use :

git push -f origin HEAD~3:master

The last 3 commit will be available in local.

Rebasing

Start the rebase :

git rebase -i HEAD~3

After that an GNU nano (Ubuntu) editor will be opened. Example view of rebase screen below.

...
pick 5x7ef96 ok
pick b90dre9 ok
pick 738s56h ok

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

To squash the last 2 commits to the first one, change the file into :

...
pick 5x7ef96 ok
s b90dre9 ok
s 738s56h ok

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

GNU nano (Ubuntu).

Save (CTRL + x) the file and exit, next question, then choose yes / no (by pressing y or n) and push enter to confirm. Next Git will asked you to change the commit message, simply delete the last two and keep the one that will be pick (or do other possible task), and again; save (CTRL + x) the file and exit, next question, choose yes / no (by pressing y or n) and push enter to confirm.

Push the change into remote repository.

Edit 2017/11/05-E01

Notes (fixup and vim editor) :

Edit 2017/11/05-E01: Add notes.

Edit 2017/11/05-E01 24:04 AM: Further edit, more clarity about vim editor and stuff.


Squash is my passion, and it is in my blood. - Jahangir Khan

Post by: Anonymoussc (@anonymoussc)