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

前端 未结 2 1261
执念已碎
执念已碎 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
    
    0 讨论(0)
  • 2021-01-16 10:09

    git read-tree-umcommit

    resets the index and worktree to that commit (the -m option says to merge what's being read into the index -- with just the one tree and index this is a bit of a degenerate case), the u option says to update the worktree when done).

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