1

We need to revert to a prior commit, but because changes since that commit have been pushed into production we can't destroy repo history with git reset.

Reverting to past commits has been addressed here and here, but neither provides a satisfactory non-destructive solution.

We tried to follow @Ben's answer by checking out the previous commit in a separate branch, but when we tried to merge it into master we get an "Already up-to-date." message and nothing happens.

$ git checkout 0766c053 -b reverted
$ git checkout master
$ git merge reverted
Already up-to-date.
Community
  • 1
  • 1
Yarin
  • 173,523
  • 149
  • 402
  • 512
  • What exactly is unsatisfactory in `git revert`? Seems it'd be exactly what you're looking for. – Amadan Feb 12 '14 at 02:27
  • @Amadan- `git revert` only reverts one specific commit, ignoring any commits made since then. We need to revert back *to* a previous commit. Git doesn't have a `git revert --to` function. – Yarin Feb 12 '14 at 02:35
  • 1
    Ah I see. In that case, this should help: http://stackoverflow.com/a/1470452/240443 – Amadan Feb 12 '14 at 02:55

2 Answers2

4

I got it, thanks to this link from @Amadan and to @BobSlocum, bashful and mysterious, who gave a fine answer and then deleted it and went away...

Here's all you have to do:

git revert --no-commit 0766c053..HEAD

This will revert everything from the HEAD back to the commit hash. (The --no-commit flag lets git revert all the commits at once, instead of littering history with messages for each commit in the range.)

Community
  • 1
  • 1
Yarin
  • 173,523
  • 149
  • 402
  • 512
  • 1
    @Amadan's link did a better job of explaining what I said. Didn't want to look like I was trying to shark his glory:) – Shaun O'Keefe Feb 12 '14 at 04:17
  • 1
    No worries. :) +1, as you wrote out a proper answer. Also, maybe noteworthy is a general syntax: if you want to revert everything from X to Y, `X^..Y` works. (`X..Y` will revert everything *since* X, not X itself). – Amadan Feb 12 '14 at 04:24
0

Someone can no doubt cook up a git reset sequence to do it, but here's the direct route:

git checkout -B master $(
        git commit-tree -p master -m - 0766c053^{tree} 
)

and git commit --amend it to add a commit message.

jthill
  • 55,082
  • 5
  • 77
  • 137