Removing a commit with git

Possible Duplicate:
How to delete a 'git commit'

I am new to git and I encountered a situation which I need some help with. I cloned a remote repository to my PC, and then opened a new branch named "meir".

For practice purposes I first commited some changes I've made on a file. Afterwards, I changed the file the way I wanted it to be and commited again. Then I pushed my local branch to the remote repository.

The problem is that I have the first commit which was a test and I would not like it to be on the remote server. I thought about 2 possible ways to fix it:

  • Remove the remote "meir" branch on the local and remote repository and start over.
  • Remove only the first commit and then push the branch again to the server so it will also lose the first commit.
  • What commands do I need to run to achieve each of the options?

    Thanks, Meir


    Generally speaking, you should not change commit history after you have pushed to public branch in a remote repository. It will cause pain to people who have pulled/cloned from the remote repository.

    But if you really want to do it, you could use interactive rebase and forced push:

  • Use git rebase -i to squash the first commit with the second so that the local branch is setup the way you want the remote branch to be.
  • Use git push -f (with proper branch spec) to "force push" to remote branch. Normal git push without -f will complain that the push is not a fast-forward push and will deny it.
  • Another option is to remove the remote branch 'meir' with git push origin :meir , resturcture your local repository with git rebase -i and then push as normally.


    There are multiple ways of doing it, but none of them are recommnded though So, Use them at your own risk.

    Pushing one of the commits

    Using rebase

    If you have 2 commits & wish to push only one of them, which is not the latest one.

  • git reset –hard <2nd commit id>
  • git push
  • git reset –hard <1st commit id>
  • With this only changes corresponding to 2nd Commit Id will be pushed & your latest commit will be not pushed.

    But, if the latest commit has to be pushed and not the 2nd Commit, you have to first reverse the commits

  • git log - Note the commit ID before 53259b225. let us say it is xxxxxxxx
  • git rebase -i xxxxxxxx
  • In the editor that opens up, you should see two lines representing your commits 53259b225 & 227552392 like

    pick 227552392 blah blah pick 53259b225 blah blah

    Reorder these 2 lines to look like

    pick 53259b225 blah blah pick 227552392 blah blah

    and then save the file.

    With this you are essentially changing the order of your commits. If you worked on different sets of files in both the commits, then this would a relatively easier process. If not then you may have some merging to do.

    If at any point of time if you think you have messed up, then you can give the command

  • git rebase --abort to take you back to what your stream looked like before you started the interactive rebase.
  • Using squash

  • Do "git log" and note your HEAD commit (first commit in the list) and note the "base commit" for your changes (the commit just before (in time) the first commit you are replacing). Make sure there no files in add index.
  • Do "git reset --hard " (use caution with this command; ensure the steps above).
  • Do " git merge --squash " - This will revoke the commit and all the committed files will be staged.
  • Do "git status" etc. and verify that the intended changes are in your index (and working tree).
  • if you doesn't want to push any of the files, do git reset HEAD or if you wish to make changes to those files, make those changes and stage them
  • Do " git commit -m '' ".
  • As you see, this can also be used to merge few commits into one.

    链接地址: http://www.djcxy.com/p/9004.html

    上一篇: 处理提交5提交前。 如何做到最好?

    下一篇: 使用git删除提交