Horst
Horst

Reputation: 259

Git reset --hard without losing history

I have the following situation:

A-B-C-D (master)

I want to undo all local changes from D and go from D to where everything was like in B, but i don't want to just go to B. I want the stage of B to be appended to the current history, so that i end up with:

A-B-C-D-B' (master)

So like reverting from D to B but with keeping this Reverting in the history.

When i do a reset --hard I lose my history from B to D and i end up with:

A-B (master)

I just want to append a point in history to the history and start working on this top point and this without creating new branches stashes etc. Like getting everything how it was in B and commiting it (with commit message: "reverted from B") on top.

Is this somehow possible?

Upvotes: 1

Views: 2212

Answers (1)

Marco Bonelli
Marco Bonelli

Reputation: 69437

Yes, git revert is exactly what you're looking for. It creates a new commit that reverts the changes of the given commits.

In your case, the following commands are all equivalent:

git revert HEAD~2..
git revert HEAD~1 HEAD
git revert <hash_of_C>..<hash_of_D>
git revert <hash_of_B>..

See the documentation for more info.

Upvotes: 5

Related Questions