是否可以 git-checkout 单行而不是整个文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6963129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to git-checkout a single line instead of the entire file?
提问by LuisVM
If I have modified several lines of a versioned file, is it possible to undo the changes of a lineby command-line?
如果我修改了一个版本化文件的几行,是否可以通过命令行撤消一行的更改?
Just like I would do for an entire file with:
就像我对整个文件所做的一样:
git checkout /path/to/file.extension
but doing something like, say
但做类似的事情,说
git checkout /path/to/file.extension --line 10
is this possible?
这可能吗?
回答by Matt Enright
You can use git checkout -p
to see each hunk individually and decide whether to check them out or leave them as is (and that takes an optional path argument as well if you'd like to narrow it down further).
您可以使用git checkout -p
单独查看每个大块并决定是将它们检出还是保持原样(如果您想进一步缩小范围,这也需要一个可选的路径参数)。
回答by Shaun Luttin
To elaborate on Matt's answer, git checkout --patch -- <path argument>
starts an interactive mode with the following options:
为了详细说明马特的回答,请git checkout --patch -- <path argument>
使用以下选项启动交互模式:
y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk or any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
The y
n
s
and e
options are a good place to start.
在y
n
s
和e
选项是一个良好的开端。
See also:
也可以看看:
- https://git-scm.com/docs/git-checkoutfor
--patch
- https://git-scm.com/docs/git-addfor the interactive options.