2

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.

Community
  • 1
  • 1
rahulserver
  • 10,411
  • 24
  • 90
  • 164
  • "deleted from history both locally and remotely" You cannot change history on the remote repo. – matt May 10 '15 at 01:33
  • @matt then is there no way to remove C,D totally? I simply don't want my manager having to compare so many whitespace changes when he sees the diff at D with B(as I made 2 commits since he last reviewed). – rahulserver May 10 '15 at 01:38
  • possible duplicate of [Delete commits from a branch in Git](http://stackoverflow.com/questions/1338728/delete-commits-from-a-branch-in-git) – Andrew C May 10 '15 at 01:44
  • @AndrewC I have already tried that answer. Infact I have tried ALL answers on first page of google search for "Delete commit from history git" on stackoverflow. – rahulserver May 10 '15 at 01:51
  • @AndrewC and that link in your answer is THE MOST viewed topic on this issue. So its highly unlikely that I would have missed that before posting it here. – rahulserver May 10 '15 at 01:54
  • See here http://stackoverflow.com/questions/10510462/force-git-push-to-overwrite-remote-files without knowing what your local and tracking branch is would be guessing. – Andrew C May 10 '15 at 02:08
  • @AndrewC pls read carefully: "I am pushing to master and I have just one working branch at this moment." – rahulserver May 10 '15 at 10:25
  • I am reading carefully. You say you are getting "unqualified destination" which usually means your branch tracking is screwed up. – Andrew C May 10 '15 at 15:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77439/discussion-between-rahulserver-and-andrew-c). – rahulserver May 10 '15 at 15:37

3 Answers3

3

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 ***.

dcsohl
  • 7,186
  • 1
  • 26
  • 44
  • yes its indeed major pain in the ***. but I can't make him suck it up as I have to keep my job. Would try your solution though as it sounds more logical. – rahulserver May 10 '15 at 01:40
  • I would do all the reverts locally and test the results thoroughly before pushing! – dcsohl May 10 '15 at 01:41
  • I am the only person using that repo at this moment. so no issues even if we rewrite the history. – rahulserver May 10 '15 at 01:41
1

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.

Community
  • 1
  • 1
jthill
  • 55,082
  • 5
  • 77
  • 137
1

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
s5v
  • 495
  • 5
  • 20