Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete a commit to git [duplicate]

There are a few commits on a remote branch that i would like to get rid of. For example if the history looks like:

A->B->C->D

I would like to delete C and D to give me:

A->B

where B is now HEAD?

like image 621
vikash dat Avatar asked Oct 18 '12 16:10

vikash dat


2 Answers

To delete last commit

$ git reset --hard HEAD^

To delete the latest 2 commits

$ git reset --hard HEAD~2

You can download the branch locally. Then delete the commits and perform a push --force

$ git pull origin
$ git checkout origin/<branchname>
$ git checkout -b <branchname>
$ git reset --hard HEAD~2
$ git push origin <branchname> --force
like image 148
Simone Carletti Avatar answered Oct 12 '22 19:10

Simone Carletti


I am going to lay out all the different ways to delete commits for you and when you should use them. Before you do any of these I highly recommend you copy your branch to another branch just to be safe.

Do this by doing git checkout -b copy_branch and then switch back to your original branch by doing git checkout the_branch_i_want_to_delete_from_again

If you have already pushed, as in your case, you can skip to #3, but if you haven't pushed you can look at 1 and 2.

1) I have not pushed yet or am working alone and the commit(s) I want to remove are the most recent commits:

If i have: A---B---C---D

and I want to delete C and D.

Then do git reset --hard sha_of_B

which results in: A---B

If you have already pushed, as in your case, you could still do it this way then do a git push --force origin the_branch, but this is not recommended as you could mess other people up working on this project. You should follow #3 instead.

2) I have not pushed yet or am working alone and the commits(s) I want to remove are in the middle of my branch:

If i have: A---B---C---D

and I want do delete C.

git rebase -i sha_of_B_the_commit_before_the_one_i_want_to_delete

which opens up the extremely useful interactive rebase screen:

pick sha_of_C C
pick sha_of_D D

As git prompts you "# If you remove a line here THAT COMMIT WILL BE LOST." that is what we are going to do.

I delete the line pick sha_of_C C, which leaves me with:

pick sha_of_D D

I save it in vi with :wq, which results in:

A---B---D

If you have already pushed, as in your case, you could still do it this way then do a git push --force origin the_branch, but this is not recommended as you could mess other people up working on this project. You should follow #3 instead.

3) I have already pushed and I am working with other people:

If I have: A---B---C---D and I want to delete C and D.

Then do

git revert sha_of_D

Note that you may have to resolve conflicts here and then commit. Then do

git revert sha_of_C

Note that you may have to resolve conflicts here and then commit.

This results in:

A---B---C---D---Reverted_D---Reverted_C

This is safe to push as you are really just adding a commit that reverses all of C's and D's changes, essentially deleting it.

You should generally avoid doing a git push --force at all costs unless absolutely necessary. But If you are about to do a push force, use protection, you should definitely make a copy of your branch before doing so. It is generally a good rule to just stick to #3 if you have already pushed at all.

Hopefully this is helpful.

Dan

like image 34
Dan Fischer Avatar answered Oct 12 '22 19:10

Dan Fischer