What is the `git restore` command and what is the difference between `git restore` and `git reset`?

后端 未结 2 675
北恋
北恋 2020-11-29 03:57

When I want to unstage a staged file, all of my Git tutorials show something like:

$ git add *
$ git status
On branch master
Changes to be committed:
  (use &         


        
2条回答
  •  无人及你
    2020-11-29 04:24

    For your 1st question "What is git-restore?":

    git-restore is a tool to revert non-commited changes. Non-commited changes are: a) changes in your working copy, or b) content in your index (a.k.a. staging area).

    This command was introduced in git 2.23 (together with the git-switch) to separate multiple concerns previously united in git-checkout.

    git-restore can be used in three different modes, depending on whether you like to revert work in the working copy, in the index, or both.

    git restore [--worktree] overwrites in your working copy with the contents in your index (*). In other words, it reverts your changes in the working copy. Whether you specify --worktree or not does not matter because it is implied if you don't say otherwise.

    git restore --staged overwrites in your index with the current HEAD from the local repository. In other words, it unstages previously staged content. In so far, it is indeed equivalent to the old git reset HEAD .

    To overwrite both, the working copy and the index with the current HEAD, use git restore --staged --worktree --source HEAD . This version does both: revert your working copy to HEAD and unstage previously staged work.

    For your 2nd question "What's the difference between git-restore and git-reset?":

    There are overlaps between these two commands, and differences.

    Both can be used to modify your working copy and/or the staging area. However, only git-reset can modify your repository. In this sense, git-restore seems the safer option if you only want to revert local work.

    There are more differences, which I can't enumerate here.

    (*) A file not added to the index is still regarded to be in the index, however in it's "clean" state from the current HEAD revision.

提交回复
热议问题