How to delete the last n commits on Github and locally?

139,303

Solution 1

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.

Solution 2

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.

Solution 3

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>

Solution 4

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>
Share:
139,303

Related videos on Youtube

Ivan Fernandez
Author by

Ivan Fernandez

About me. I'm a passionate developer who is keen on seeking new technologies related to web or mobile development. I'm currently working as a JEE programmer analyst with frameworks such as Spring, Hibernate, JQuery or Apache CXF. Besides that I'm really interested in others frameworks such as Grails, Ruby or Node.js. I'm also taking my first steps in the front-end layer (Backbone.js, Express.js, Jade, Sass ...). It's pretty nice how is integrating lately with other mobile technologies like JQuery Mobile and PhoneGap. I'm always looking for new things to learn too. Lately, I'm really interested in cloud computing and NoSQL technologies, such as Amazon Web Services, Google App Engine, Cloud Foundry or Heroku. I'm starting with some new cool stuff like Hadoop, MongoDB or MemCached. I've been writting apps getting the information from some REST API (Twitter, Yahoo or Google) lately. You can take a look at: http://mycityfinder.cloudfoundry.com/ Grails + JQuery + Google Reader + Flickr + Eventful API http://looking4tweets.herokuapp.com/ Node.js + Express.js + Backbone.js + Jade + Less. I do like writing in my blog as well. Since I started to write it, I became interested in SEO issues, Wordpress administration, ... Specialties Web development : JEE, Spring, Hibernate, Maven. Mobile development: Android, PhoneGap. Any stuff regarding blog writing. Please, visit my blog if you feel like it: http://hop2croft.wordpress.com

Updated on August 06, 2022

Comments

  • Ivan Fernandez
    Ivan Fernandez almost 2 years

    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.

  • Ivan Fernandez
    Ivan Fernandez over 11 years
    If you've already pushed this change to a remote repository. You can remove it with git push -f
  • user_19
    user_19 about 10 years
    Can you generalize this for last n number of commits?
  • KL-7
    KL-7 about 10 years
    @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.
  • Gagan Gami
    Gagan Gami almost 10 years
    If I wanted to delete last 7 commits then?? Do I need to put 7 times ^ after HEAD... please clear me
  • Captain Hypertext
    Captain Hypertext about 8 years
    I also found the git-reset documentation pretty handy.
  • Con Antonakos
    Con Antonakos about 8 years
    @GaganGami, I think you would do git reset --hard HEAD~7, but please correct me if I'm wrong.
  • Zuhayer Tahir
    Zuhayer Tahir about 7 years
    Do the changes I made stay?
  • Dherik
    Dherik about 7 years
    @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.
  • mfaani
    mfaani over 6 years
    @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.
  • Zuhayer Tahir
    Zuhayer Tahir over 6 years
    @Honey Thank you for your response. I came to same conclusion.
  • Carmine Tambascia
    Carmine Tambascia over 4 years
    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 ~
  • stevec
    stevec about 3 years
    Just a heads up, I lost uncommitted work using this approach. Other than that it works.
  • iago
    iago over 2 years
    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.