我可以非交互式地使用`git checkout --patch`吗? [英] Can I use `git checkout --patch` non-interactively?

查看:1323
本文介绍了我可以非交互式地使用`git checkout --patch`吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的工作目录的内容重置为匹配某些修订,而不更改我当前分支指向的提交,如 git reset 确实。 git checkout --patch 可以做到这一点,但它是以交互方式进行的,需要我为每个我想要的更改回答 yes 重置。这很烦人,当我重置了很多变化,在大多数情况下,我宁愿只是重置所有的东西。

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.

有没有办法使用 git checkout --patch (或相当于)非交互式?

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

推荐答案

= 他的回答中提到的https://stackoverflow.com/users/1290731/jthill> jthill ,在一般情况下 git read-tree 可以做你想做的:

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


git read-tree -um 提交

但是,如果您希望能够检出/重置子目录而不是整个树,则需要一个更复杂的命令:

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 - c ached 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

这篇关于我可以非交互式地使用`git checkout --patch`吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆