237

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
  • 10,842
  • 12
  • 78
  • 123
Ivan Fernandez
  • 4,173
  • 5
  • 25
  • 30

5 Answers5

312

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
  • 46,000
  • 9
  • 87
  • 74
  • 35
    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
  • 2
    Can you generalize this for last n number of commits? – user_19 Apr 29 '14 at 00:39
  • 9
    @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
  • 5
    @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
245

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
  • 17,757
  • 11
  • 115
  • 164
  • 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
57

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
  • 671
  • 5
  • 5
3

If your changes have not been pushed yet simply enter the command.

git reset --hard HEAD~n 

If you have already pushed your changes you will need to run the following code.

git push origin HEAD --force 
Habib01
  • 63
  • 6
1

Removing the last commit

To remove the last commit from git, you can simply run

git reset --hard

HEAD^ If you are removing multiple commits from the top, you can run

git reset --hard HEAD~2

to remove the last two commits. You can increase the number to remove even more commits.

If you want to "uncommit" the commits, but keep the changes around for reworking, remove the "--hard"

git reset HEAD^

which will evict the commits from the branch and from the index, but leave the working tree around.

If you want to save the commits on a new branch name, then run

git branch newbranchname

before doing the git reset.

Answer - This Gist Answer helped me

Sumit Munot
  • 3,748
  • 1
  • 32
  • 51