How to delete the last n commits on Github and locally?
To remove the last two commits locally I'd suggest using:
git reset --hard HEAD^^
Rebase is a completely different operation that won't help you here.
If you want to remove the 2 (two) last commits, there is an easy command to do that:
git reset --hard HEAD~2
You can change the 2
for any number of last commits you want to remove.
And to push this change to remote, you need to do a git push
with the force (-f
) parameter:
git push -f
However, I don't recommend to do any git
command with -f
or --hard
options involved if there are new commits on remote (Github) after this commits that you want to remove. In that case, always use git revert
.
The following works for me
git reset HEAD~n
It removes the last n
commits from local repo, as HEAD^
removes only one. If you need to remove these changes from remote, you might need to force push as you will be behind remote.
git push -f origin <branch>
To remove the last n commits:
git reset HEAD~n
If you need to remove these changes from remote, you might need to force push as you will be behind remote.
git push -f origin <Branch Name>