Ajedi32
Ajedi32

Reputation: 48368

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

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 does. git checkout --patch does this, but it does so interactively, requiring me to answer yes for every change I want to reset. This is annoying when I'm resetting a lot of changes, in most cases I'd rather just reset everything.

Is there a way to use git checkout --patch (or equivalent) non-interactively?

Upvotes: 2

Views: 1556

Answers (2)

Ajedi32
Ajedi32

Reputation: 48368

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

Upvotes: 1

jthill
jthill

Reputation: 60275

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).

Upvotes: 3

Related Questions