Can I use `git checkout --patch` non-interactively?

前端 未结 2 1260
执念已碎
执念已碎 2021-01-16 09:07

I want to reset the contents of my working directory to match some revision without changing the commit my current branch is pointing to, as git reset

2条回答
  •  心在旅途
    2021-01-16 09:48

    As jthill mentioned in his answer, in the general case git read-tree can do what you want:

    git read-tree -um commit

    If, however, you want to be able to checkout/reset a subdirectory instead of the entire tree, you'll need a somewhat more complex command:

    git diff --cached commit -- subdir | git apply -R --index

    Since this command is much longer than the previous, if you plan on using it frequently you'll probably want to set up an alias for it:

    git config --global alias.reset-checkout '!f() { git diff --cached "$@" | git apply -R --index; }; f'
    

    And use like:

    git reset-checkout 451a9a4 -- path/to/directory
    

    Or just:

    git reset-checkout 451a9a4
    

提交回复
热议问题