gpt4 book ai didi

git - 'git add --patch' 来包含新文件?

转载 作者:IT王子 更新时间:2023-10-29 01:25:30 28 4
gpt4 key购买 nike

当我运行 git add -p 时,git 是否可以选择新创建的文件作为 hunks 来选择?

因此,如果我创建一个名为 foo.java 的新文件,然后运行 ​​git add -p,git 不会让我选择要添加到索引中的文件内容。

最佳答案

当我在新文件(未跟踪文件)上尝试 git add -p someNewFile.txt 时,git 只会输出 No changes. 并停止。我必须告诉 git 我打算先跟踪新文件。

git add -N someNewFile.txt
git add -p

但是,由于该文件未被跟踪,它会显示为一个无法拆分的巨大块(因为它是全新的!)。所以,然后我需要将大块头编辑成更小的部分。如果您对此不熟悉,请查看 this reference开始。

更新 - Hunk 编辑信息我想更新它以防上面的引用消失。因为新文件未被跟踪,git add -p 将文件中的每一行显示为一个大块头。然后它会问你想用那个大块头做什么,给你以下提示:

上演这个大块头 [y,n,q,a,d,/,e,?]?

假设你不想提交整个大块(因此,整个文件;因为我不确定你为什么要在那种情况下使用 git add -p ?),你需要指定选项 e 来告诉 git 你想要编辑 hunk。

一旦您告诉 git 您想要编辑 hunk,它就会将您带到您选择的编辑器中,以便您进行更改。所有行都应以 + 为前缀,并且 git 在文件末尾有一些解释性注释(以 # 为前缀)。只需删除您在文件的初始提交中不需要的任何行。然后保存并退出编辑器。

Git对git的hunk选项的解释:

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

关于git - 'git add --patch' 来包含新文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14491727/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com