Using git, how do you reset the working tree to the state of the index?

后端 未结 4 906
伪装坚强ぢ
伪装坚强ぢ 2020-12-23 19:02

Situation:

  1. Edit files
  2. Add files to the index
  3. Edit more files

Now we have three different states. The state of HEAD, the state

相关标签:
4条回答
  • 2020-12-23 19:20

    git checkout :/ discards all changes in the working tree and replaces it with what's in the index, regardless of the current working directory.

    https://git-scm.com/docs/gitglossary#gitglossary-aiddefpathspecapathspec

    0 讨论(0)
  • 2020-12-23 19:25

    I tend to use git checkout . which discards all changes from the working directory down. This makes a difference if you're not at the root of the repository.

    This command doesn't remove newly created files which is usually a good thing. If you need to do this then you can use git clean as well.

    0 讨论(0)
  • 2020-12-23 19:29

    You can use git stash save --keep-index to do this. After saving the stash, you can use git stash drop if you don't want to keep it around.

    0 讨论(0)
  • 2020-12-23 19:37

    You can use git-checkout-index. Be aware that you need to add

    • -f to force it to overwrite existing files, or
    • -f -a to enforce overwriting all paths in the index.
    0 讨论(0)
提交回复
热议问题