198

I'm trying to delete the last 2 commits from one of my GitHub repositories. I've tried as suggested here : git push -f origin HEAD^^:master. It seems that it works, as the last two commits are removed.

Then I deleted them from my local repository with git rebase -i HEAD~2. I remove the lines that are related to those commits, and check with git log that they are correctly removed.

After that, I make some changes in my local repository, make a new commit, and push to GitHub. The problem is that, in my GitHub account, I have the previous two commits that I've tried to delete.

I think the problem is in my local repository, because if I clone my Github repository to my local and make some changes here, when I push a new commit those old commits aren't pushed to GitHub.

030
  • 9,351
  • 10
  • 70
  • 110
Ivan Fernandez
  • 3,573
  • 5
  • 24
  • 30

4 Answers4

275

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.

KL-7
  • 43,332
  • 9
  • 85
  • 74
  • 30
    If you've already pushed this change to a remote repository. You can remove it with git push -f – Ivan Fernandez Jan 14 '13 at 11:33
  • 1
    Can you generalize this for last n number of commits? – user_19 Apr 29 '14 at 00:39
  • 8
    @user_19 you can do things like `git reset --hard HEAD^4` or `git reset --hard HEAD~4`. Though, things might get a bit complicated if your history contains merges. You can find more information about specifying revisions in corresponding section [here](https://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html). – KL-7 Apr 29 '14 at 20:23
  • 2
    If I wanted to delete last 7 commits then?? Do I need to put 7 times ^ after HEAD... please clear me – Gagan Gami Sep 05 '14 at 11:38
  • I also found the [git-reset documentation](https://git-scm.com/docs/git-reset) pretty handy. – Captain Hypertext Apr 15 '16 at 15:52
  • 4
    @GaganGami, I think you would do `git reset --hard HEAD~7`, but please correct me if I'm wrong. – Con Antonakos May 06 '16 at 14:50
  • This can lead to the "ambiguous argument 'HEAD^2': unknown revision or path not in the working tree" if the git repository is a fresh one, meaning Head is not yet created(not sure when and how create the Head is done, I though was created with a first comming), for me worked the previous asnwer, that with the ~ – Carmine Tambascia Mar 12 '20 at 11:02
188

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.

Dherik
  • 15,440
  • 11
  • 108
  • 148
  • Do the changes I made stay? – Zuhayer Tahir Apr 22 '17 at 10:16
  • @SymfonyUser, no. When you made the `hard` command, you *loose* this two commits. If you want to save the changes, create a `diff` file of these commits before apply the reset. – Dherik Apr 24 '17 at 12:05
  • 4
    @ZuhayerTahir if you want to just undo the *committing* for last 5 commits then just do `git reset HEAD~5` ( don't use `hard`). This way you'll get your changes in a staged state (ie not committed). For me see [this answer](https://stackoverflow.com/a/927386/5175709). – mfaani Nov 10 '17 at 18:24
  • @Honey Thank you for your response. I came to same conclusion. – Zuhayer Tahir Nov 13 '17 at 11:16
  • Just a heads up, I lost uncommitted work using this approach. Other than that it works. – stevec May 22 '21 at 04:59
51

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>
tulians
  • 439
  • 5
  • 23
Sial01
  • 611
  • 5
  • 5
11

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>
Liju Thomas
  • 1,044
  • 5
  • 18
  • 24
Ambika Prasad
  • 111
  • 1
  • 4
  • I would add to the `git reset HEAD~n` a `git stash` to be again at the same stage after doing the last commit one wants to keep, so it would be possible a `git pull` from that commit. – iago Nov 18 '21 at 11:29