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) :
- Choose
f
orfixup
= like “squash”, but discard the commit’s log message. - vim editor: Type
i
to insert or start editing - vim editor: To quit, type
esc
to enter “Command mode”:q
to quit (short for:quit
):q!
to quit without saving (short for:quit!
):wq
to write and quit:wq!
to write and quit even if file has only read permission (if file does not have write permission: force write):x
to write and quit (similar to:wq
, but only write if there are changes):exit
to write and exit (same as:x
):qa
to quit all (short for:quitall
)
Edit 2017/11/05-E01: Add notes.
Edit 2017/11/05-E01 24:04 AM: Further edit, more clarity about vim editor and stuff.
Post by: Anonymoussc (@anonymoussc)Squash is my passion, and it is in my blood. - Jahangir Khan