How do I reset the working tree without moving HEAD?

后端 未结 3 1392
青春惊慌失措
青春惊慌失措 2021-01-04 13:19

Given a git branch with some commits on it (C is the most recent commit):

A -> B -> C

How do I reset my workspace so that all the fil

相关标签:
3条回答
  • 2021-01-04 13:52

    You could use a combination of hard and soft resets:

    git reset --hard B
    git reset --soft C
    

    The first would move HEAD to B and make all your files look like B. The second would then move it back to C without changing any files.

    This method has the advantage that you are not in a detached-head state and all the differences between B and C will just show up as inverse diffs of your last actual commit. You will still be on your original branch.

    You would probably have to specify C as a SHA-1 rather than a ref name, unless you specifically created one for the purpose.

    0 讨论(0)
  • 2021-01-04 13:53

    Just use git checkout:

    $ git checkout HEAD~
    $ # Or
    $ git checkout HEAD^
    $ # Or
    $ git checkout B
    

    This will bring you into a detached head state where the HEAD is detached from a branch.

    0 讨论(0)
  • 2021-01-04 14:01

    git checkout [-p|--patch] [<tree-ish>] [--] <pathspec>...

    git checkout with <paths> or --patch is used to restore modified or deleted paths to their original contents from the index or replace paths with the contents from a named <tree-ish> (most often a commit-ish).

    So you need to run this at root of your repository (works fine for any sub-tree or file(s) too):

    git checkout HEAD~ -- .
    

    This will result in git applying changes necessary to revert files to HEAD~ state, the changes will be in the index.

    0 讨论(0)
提交回复
热议问题