开发者

Removing a commit with git [duplicate]

This question already has answers here: Closed 11 years ago.

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开发者_如何学JAVA 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:

  1. Remove the remote "meir" branch on the local and remote repository and start over.
  2. 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:

  1. 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.
  2. 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.

  1. git reset –hard <2nd commit id>
  2. git push
  3. 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

  1. git log - Note the commit ID before 53259b225. let us say it is xxxxxxxx
  2. 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

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

Using squash

  1. 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.
  2. Do "git reset --hard " (use caution with this command; ensure the steps above).
  3. Do " git merge --squash " - This will revoke the commit and all the committed files will be staged.
  4. Do "git status" etc. and verify that the intended changes are in your index (and working tree).
  5. 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
  6. Do " git commit -m '' ".

As you see, this can also be used to merge few commits into one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜