0

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?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Horst
  • 229
  • 1
  • 5
  • 15
  • So... how does what you want differ from `git revert`? – Amadan Oct 28 '19 at 09:45
  • 1
    Possible duplicate of [How do I revert a Git repository to a previous commit?](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit) – Michał Politowski Oct 28 '19 at 10:11
  • Reset doesn’t lose history. It will lose uncommitted changes, and move a branch pointer. You even get an unreachable commit via the ref log. – evolutionxbox Oct 28 '19 at 10:14

1 Answers1

4

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.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • `git revert B..` in this case. – Amadan Oct 28 '19 at 09:51
  • @Amadan hmm doesn't that revert B too? – Marco Bonelli Oct 28 '19 at 09:53
  • [Docs](https://git-scm.com/docs/gitrevisions#Documentation/gitrevisions.txt-Theememtwo-dotRangeNotation): "you can ask for commits that are reachable from `r2` excluding those that are reachable from `r1` by `^r1 r2` and it can be written as `r1..r2`." Thus, `B..` is all commits reachable from `HEAD` that are not reachable by `B` (i.e. it also excludes `B`). – Amadan Oct 28 '19 at 09:56