rahulserver
rahulserver

Reputation: 11225

How do I delete a commit from git history as if it was not at all there?

My scenario is like this:

A->B->C->D

A was the original version of code I received(it had commits before it, but not mentioning it here).

In B, I applied code formatting which introduced hundreds of whitespace changes. Hence the person reviewing the code found over thousands of changes due to it. While I know that adding ?w=1 parameter to the commit url on github allows code diffs without whitespaces, the reviewer is not accepting that and tells me to simply revert to previous formatting. I think the only way to deal with this scenario is to simply delete C and D from history(locally and remotely) and then make the changes again.

I tried git revert but that seems to temporarily delete changes in remote. After next git push, it pushes the commits AGAIN.

Please suggest proper steps so that C,D totally gets deleted from history both locally and remotely as if they were never there(thereby rewriting history).

Note:

I am on MASTER branch and my local repo is totally in sync with remote.

EDIT:

I have ALREADY TRIED that accepted answer in link. Here is what I did:

git reset --hard HEAD~2

Then I did git push origin HEAD --force

But I get this command:

error: unable to push to unqualified destination: HEAD

And if i put master instead of HEAD, i get "everything up to date".

I am pushing to master and I have just one working branch at this moment.

Upvotes: 2

Views: 128

Answers (3)

s5v
s5v

Reputation: 505

if it looks like a>b>c>d and you want to get rid of the last 2 commits, just use :

git reset --hard HEAD~2

and then

git push --force

another option is to use :

git reflog

which will show a list of changes made to the repository.

followed by

git reset --hard HEAD@{n}
git push --force

where n was the last commit you need in the branch. another option is to use

git revert c d
git push --force

Upvotes: 1

jthill
jthill

Reputation: 60605

A---B---C---D master

In B, I applied code formatting which introduced hundreds of whitespace changes

the reviewer is not accepting that and tells me to revert to previous formatting


I think the only way to deal with this scenario is to simply delete C and D from history

If I'm reading this correctly and the quotes at the top here are the payload, then what you want is the history you'd have if you'd never done B. That's what git rebase is for.

git rebase --onto $A $B master

is the full spelling. It says "take the master branch commits since $B and apply all the changes in each onto $A, then move the master label to the new series. This will produce

A---B---C---D        (<-- old history's still there, nothing in your repo refers to it)
 \
  C'--D'             master

If communication isn't a problem, this is by far the best way to deal with slips. Do the rebase, force-push the new history, and have everyone refetch. If they've based work on the abandoned history they'll have to rebase too, but:

if nobody has based any work on the abandoned history, you're done.

Upvotes: 1

dcsohl
dcsohl

Reputation: 7406

If you have pushed C and D to the remote and other people have picked them up, there's not a lot you can do without running a chance of making a big mess. Your best bet is to start with git revert D. This will make a new revision, D', which looks identical to the state that C was in. Both D and D' will still be in the git history though.

Then you can git revert C and finally git revert B if that is your desire. Then your history will look like A-B-C-D-D'-C'-B' and checking out B' will give you code identical to checking out A.

Or you could tell the reviewer to suck it up and deal because all of this is a major pain in the ***.

Upvotes: 3

Related Questions