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.

Lolly
  • 34,250
  • 42
  • 115
  • 150
  • possible duplicate of [How can I remove a commit on github?](http://stackoverflow.com/questions/448919/how-can-i-remove-a-commit-on-github) – Andy Lester Nov 27 '12 at 04:13

3 Answers3

10
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>
koninos
  • 4,969
  • 5
  • 28
  • 47
  • 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 Nov 27 '12 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.

eis
  • 51,991
  • 13
  • 150
  • 199
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.

Toby Zheng
  • 21
  • 1