7

I am using git repository in my project. I have accidentally pushed 2 commits which I shouldn't have. And in between some one has committed on top of it. Is it possible to remove my pushed commits or I have to remove my code changes and push it as new commit, since some one has committed on top of it.

Git Master branch :

Commit A // by me

Commit B // by me

Commit C // by some one

Now I have to delete Commit A and B leaving Commit C. Any help will be really appreciated.

1

3 Answers 3

12
git reset --hard HEAD~1

Where HEAD~1 means the commit before head.

Alternatively find the commit id of the commit you want (look at the output of git log), and then do this:

git reset --hard <sha1-commit-id>
1
  • I think a) that would do vice versa, skipping C and leaving A and B, which is the opposite of what OP wants, and b) it will only affect local state and separate fix commits would still be needed.
    – eis
    Commented Nov 27, 2012 at 16:03
5

You'll have to revert those commits.

Technically what it does is that it removes those changes and makes a new commit, undoing them.

Now, reverting them will leave them on the history stil, but usually that is ok. If that's totally unacceptable only then look at the solutions like filter-branch and force pushes.

2
git rebase --onto master~3 master~1 master

You can see help by command git help rebase, and then --onto option, or you see it here. The doc has an example as some as your situation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.