当我尝试时git add -p someNewFile.txt
在新文件(未跟踪的文件)上,git 会简单地输出No changes.
并停止。我必须告诉 git 我打算先跟踪新文件。
git add -N someNewFile.txt
git add -p
然而,由于该文件未被跟踪,它会显示为一个无法分割的巨大块(因为它是全新的!)。所以,然后我需要将大块编辑成更小的部分。如果您对此不熟悉,请查看这个参考 http://nuclearsquid.com/writings/git-add/开始。
更新 - Hunk 编辑信息
我想更新这个以防上述参考消失。因为新文件未被跟踪,git add -p
会将文件中的每一行显示为一大块中的新行。然后它会问你想对那个大块头做什么,并给出以下提示:
Stage this hunk [y,n,q,a,d,/,e,?]?
假设您不想提交整个块(因此,整个文件;因为我不确定您为什么要使用git add -p
在这种情况下?),您需要指定选项e
告诉 git 你想编辑这个块。
一旦你告诉 git 你想要编辑该块,它就会将你带到你选择的编辑器中,以便你可以进行更改。所有行都应以前缀+
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