Reset Git to commit without changing HEAD to detached state

git

Solution

This should do it:

git reset --hard C
git reset --soft D

First you reset the `HEAD`, index and worktree to `C`. Then you reset `HEAD` (and only `HEAD`, as explained in "Practical uses of `git reset --soft`?") to `D`.

Note that a commit at this point would create a new commit with `C` content, replacing `D` by a `D'` looking like `C`. That changes the history, and is not much different than a simple `git reset --hard C`.

Another option would `git revert C` on top of `D`, but `D` would still be visible in the history, which might be what you don't want.

Problem

I'd like to restore the files of the git working copy to a given commit, without setting that commit as the HEAD commit. If I git checkout to a given commit I obtain a detached HEAD, and after commiting changes, the commit tree would look something like: ``` A | B | C | \ D E ``` While the behaviour I'd like to obtain is: ``` A | B | C | D | <- git command so my files are restored to C, but my HEAD still points to D E ``` Thanks

Original source

Related problems