Why git can't do hard/soft resets by path?

前端 未结 7 1106
面向向阳花
面向向阳花 2020-12-07 17:33

$ git reset -- can reset by path.

However, $ git reset (--hard|--soft) will report an error like below:

相关标签:
7条回答
  • 2020-12-07 17:59

    The question how is already answered, I'll explain the why part.

    So, what does git reset do? Depending on the parameters specified, it can do two different things:

    • If you specify a path, it replaces the matched files in the index with the files from a commit (HEAD by default). This action doesn't affect the working tree at all and is usually used as the opposite of git add.

    • If you don't specify a path, it moves the current branch head to a specified commit and, together with that, optionally resets the index and the working tree to the state of that commit. This additional behavior is controlled by the mode parameter:
      --soft: don't touch the index and the working tree.
      --mixed (default): reset the index but not the working tree.
      --hard: reset the index and the working tree.
      There are also other options, see the documentation for the full list and some use cases.

      When you don't specify a commit, it defaults to HEAD, so git reset --soft will do nothing, as it is a command to move the head to HEAD (to its current state). git reset --hard, on the other hand, makes sense due to its side effects, it says move the head to HEAD and reset the index and the working tree to HEAD.

      I think it should be clear by now why this operation is not for specific files by its nature - it is intended to move a branch head in the first place, resetting the working tree and the index is secondary functionality.

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