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

前端 未结 7 1144
面向向阳花
面向向阳花 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:41

    Explanation

    The git reset manual lists 3 ways of invocation:

    • 2 are file-wise: These do not affect the working tree, but operate only on the files in the index specified by :

      • git reset [-q] [] [--] ..
      • git reset (--patch | -p) [] [--] [...]
    • 1 is commit-wise: Operates on all files in the referenced , and may affect the working tree:

      • git reset [] []

    There's no mode of invocation that operates only on specified files and affects the working tree.

    Workaround

    If you want to both:

    • Reset the index/cache version of a file(s)
    • Checkout the file(s) (ie, make the working tree match the index and commit version)

    You can use this alias in your git config file:

    [alias]
      reco   = !"cd \"${GIT_PREFIX:-.}\" && git reset \"$@\" && git checkout \"$@\" && git status --short #"  # Avoid: "fatal: Cannot do hard reset with paths."
    

    You can then do one of:

    $ git reco 
    
    $ git reco  
    
    $ git reco -- 
    

    (Mnenonic for reco: reset && checkout)

提交回复
热议问题