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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14491727/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:05:13  来源:igfitidea点击:

'git add --patch' to include new files?

gitshellgit-add

提问by Alexander Bird

When I run git add -p, is there a way for git to select newly made files as hunks to select??

当我运行时git add -p,有没有办法让 git 选择新制作的文件作为选择?

So if I make a new file called foo.java, then run git add -p, git will not let me choose that file's content to be added into the index.

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

采纳答案by Ulysse BN

To do this with every new files, you can run:

要对每个新文件执行此操作,您可以运行:

git add -N .
git add -p

If you want to use it frequently, you can create an alias in your ~/.bashrc:

如果你想经常使用它,你可以在你的 中创建一个别名~/.bashrc

alias gapan='git add --intent-to-add . && git add --patch'


N.B: If you use this with an empty new file, git will not be able to patch it and skip to the next one.

注意:如果你在一个空的新文件中使用它,git 将无法修补它并跳到下一个。

回答by CatShoes

When I tried git add -p someNewFile.txton a new file (an untracked file), git would simply output No changes.and stop. I had to tell git that I intended to track the new file first.

当我尝试git add -p someNewFile.txt处理一个新文件(一个未跟踪的文件)时,git 只会输出No changes.并停止。我不得不告诉 git 我打算先跟踪新文件。

git add -N someNewFile.txt
git add -p

However, since the file was untracked, it would show up as one giant hunk that couldn't be split (because it is all new!). So, then I needed to edit the hunk into smaller bits. If you're not familiar with that, checkout this referenceto get started.

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

Update - Hunk editing infoI wanted to update this in case the above reference goes away.Because the new file is untracked, git add -pwill show every line in the file as a new line in one hunk. It will then ask you what you want to do with that hunk, giving you the following prompt:

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

Stage this hunk [y,n,q,a,d,/,e,?]?

Stage this hunk [y,n,q,a,d,/,e,?]?

Assuming that you do not want to commit the whole hunk (and thus, the whole file; because I am not sure why you would want to use git add -pin that case?), you will want to specify option eto tell git that you want to edit the hunk.

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

Once you tell git that you want to edit the hunk, it should drop you into your editor of choice so you can make your changes. All lines should be prefixed with a +and git has some explanatory comments (prefixed with a #) at the end of the file. Simply delete any lines that you do not want in your initial commit of the file. Then save and quit the editor.

一旦你告诉 git 你想要编辑这个大块头,它应该会让你进入你选择的编辑器,这样你就可以进行更改。所有行都应该以 a 为前缀,+并且 git#在文件末尾有一些解释性注释(以 a 为前缀)。只需删除您在最初提交文件时不需要的任何行。然后保存并退出编辑器。

Git's explanation of git's hunk options:

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

回答by Matthieu Moy

git add -pis really about adding changes to already tracked files.

git add -p实际上是将更改添加到已跟踪的文件中。

The command to interactively select files to add is git add -i. For example:

交互式选择要添加的文件的命令是git add -i. 例如:

$ git add -i

*** Commands ***
  1: status   2: update   3: revert   4: add untracked
  5: patch    6: diff     7: quit     8: help
What now> a
  1: another-new.java
  2: new.java
Add untracked>> 2
  1: another-new.java
* 2: new.java
Add untracked>> 
added one path

*** Commands ***
  1: status   2: update   3: revert   4: add untracked
  5: patch    6: diff     7: quit     8: help
What now> q
Bye.
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   new.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        another-new.java

(The real command has colors which I couldn't cut-and-paste here, so it's nicer than it seems)

(真正的命令有我无法在这里剪切和粘贴的颜色,所以它比看起来更好)

Actually, the patch command of git add -idoes the same as git add -p, so the second is a subset of the first (even though I admit I love add -pand hate add -imyself!).

实际上,p的ATCH命令git add -i不一样git add -p,所以第二个是第一个的子集(尽管我承认我爱add -p和恨add -i我自己!)。

回答by doublejosh

There's also a very similar approach making use of the --cachedflag...

还有一个非常相似的方法使用--cached标志......

1) Turn your unstaged changes into staged, just like your added file.

1)将未暂存的更改转换为暂存的,就像添加的文件一样。

git add edited-file.txt
git add new-file.txt
git add directory-of-changes/

2) Look at the diff (note: you can include both edits and new files).

2)查看差异(注意:您可以同时包含编辑和新文件)。

git diff --cached

3) Create the patch.

3)创建补丁。

git diff --cached > my_patch_file.patch