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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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_branchand then switch back to your original branch by doinggit checkout the_branch_i_want_to_delete_from_againIf 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_Bwhich 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.
which opens up the extremely useful interactive rebase screen:
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: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
Note that you may have to resolve conflicts here and then commit.
Then do
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 --forceat 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