Yarin
Yarin

Reputation: 183989

Reverting to previous commit non-destructively

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.

Upvotes: 1

Views: 1165

Answers (2)

Yarin
Yarin

Reputation: 183989

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

Upvotes: 4

jthill
jthill

Reputation: 60585

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.

Upvotes: 0

Related Questions