git 仅添加未跟踪的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7446640/
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
Adding Only Untracked Files
提问by Rob Wilkerson
One of the commands I find incredibly useful in Git is git add -u
to throw everything but untracked files into the index. Is there an inverse of that? In the last few months, I've often found myself in a position where I've interactively added some updates to the index and I want to add all of the untracked files to that index before I commit.
我发现在 Git 中非常有用的命令之一是git add -u
将除未跟踪文件之外的所有内容都放入索引中。有相反的吗?在过去的几个月中,我经常发现自己以交互方式向索引添加了一些更新,并且我想在提交之前将所有未跟踪的文件添加到该索引中。
Is there a way to add onlythe untracked files to the index without identifying them individually? I don't see anything obvious in the help docs, but maybe I'm missing it?
有没有办法只将未跟踪的文件添加到索引中而不单独识别它们?我在帮助文档中没有看到任何明显的东西,但也许我错过了它?
Thanks.
谢谢。
回答by Mat
It's easy with git add -i
. Type a
(for "add untracked"), then *
(for "all"), then q
(to quit) and you're done.
使用git add -i
. 输入a
(对于“添加未跟踪的”),然后*
(对于“全部”),然后q
(退出)就完成了。
To do it with a single command: echo -e "a\n*\nq\n"|git add -i
要使用单个命令执行此操作: echo -e "a\n*\nq\n"|git add -i
回答by manojlds
git ls-files -o --exclude-standard
gives untracked files, so you can do something like below ( or add an alias to it):
git ls-files -o --exclude-standard
提供未跟踪的文件,因此您可以执行以下操作(或为其添加别名):
git add $(git ls-files -o --exclude-standard)
回答by hasen
Not exactly what you're looking for, but I've found this quite helpful:
不完全是你要找的,但我发现这很有帮助:
git add -AN
Will add all files to the index, but without their content. Files that were untracked now behave as if they were tracked. Their content will be displayed in git diff
, and you can add then interactively with git add -p
.
将所有文件添加到索引中,但没有它们的内容。未跟踪的文件现在表现得就像被跟踪一样。它们的内容将显示在 中git diff
,然后您可以使用 交互式添加git add -p
。
回答by joshuadavey
You can add this to your ~/.gitconfig file:
您可以将其添加到您的 ~/.gitconfig 文件中:
[alias]
add-untracked = !"git status --porcelain | awk '/\?\?/{ print }' | xargs git add"
Then, from the commandline, just run:
然后,从命令行,只需运行:
git add-untracked
回答by Tom Fenech
People have suggested piping the output of git ls-files
to git add
but this is going to fail in cases where there are filenames containing white space or glob characters such as *
.
人们建议将 to的输出通过管道git ls-files
传输,git add
但在文件名包含空格或全局字符(如*
.
The safe way would be to use:
安全的方法是使用:
git ls-files -o --exclude-standard -z | xargs -0 git add
where -z
tells git to use \0
line terminators and -0
tells xargs the same. The only disadvantage of this approach is that the -0
option is non-standard, so only some versions of xargs
support it.
where-z
告诉 git 使用\0
行终止符并-0
告诉 xargs 相同。这种方法的唯一缺点是该-0
选项是非标准的,因此只有某些版本xargs
支持它。
回答by pborenstein
git ls-files
lists the files in the current directory. If you want to list untracked files from anywhere in the tree, this might work better:
git ls-files
列出当前目录中的文件。如果您想从树中的任何位置列出未跟踪的文件,这可能会更好:
git ls-files -o --exclude-standard $(git rev-parse --show-toplevel)
To add all untracked files in the tree:
要在树中添加所有未跟踪的文件:
git ls-files -o --exclude-standard $(git rev-parse --show-toplevel) | xargs git add
回答by Rohan Khude
I tried this and it worked :
我试过了,它奏效了:
git stash && git add . && git stash pop
git stash
will only put all modified tracked files into separate stack, then left over files are untracked files. Then by doing git add .
will stage all files untracked files, as required. Eventually, to get back all modified files from stack by doing git stash pop
git stash
只会将所有修改过的跟踪文件放入单独的堆栈,然后剩下的文件是未跟踪的文件。然后通过这样做git add .
将根据需要暂存所有未跟踪的文件。最终,通过执行从堆栈中取回所有修改过的文件git stash pop
回答by Tore Aurstad
Lot of good tips here, but inside Powershell I could not get it to work.
这里有很多很好的技巧,但在 Powershell 中我无法让它工作。
I am a .NET developer and we mainly still use Windows OS as we haven't made use of .Net core and cross platform so much, so my everyday use with Git is in a Windows environment, where the shell used is more often Powershell and not Git bash.
我是 .NET 开发者,我们主要还是使用 Windows 操作系统,因为我们还没有过多地使用 .Net 内核和跨平台,所以我日常使用 Git 是在 Windows 环境中,其中使用的 shell 经常是 Powershell而不是 Git bash。
The following procedure can be followed to create an aliased function for adding untracked files in a Git repository.
可以按照以下过程创建别名函数,用于在 Git 存储库中添加未跟踪的文件。
Inside your $profile file of Powershell (in case it is missing - you can run: New-Item $Profile)
在 Powershell 的 $profile 文件中(如果丢失 - 您可以运行:New-Item $Profile)
notepad $Profile
记事本 $Profile
Now add this Powershell method:
现在添加这个 Powershell 方法:
function AddUntracked-Git() {
&git ls-files -o --exclude-standard | select | foreach { git add $_ }
}
Save the $profile file and reload it into Powershell. Then reload your $profile file with: . $profile
保存 $profile 文件并将其重新加载到 Powershell。然后重新加载您的 $profile 文件:$profile
This is similar to the source command in *nix environments IMHO.
这类似于 *nix 环境中的 source 命令恕我直言。
So next time you, if you are developer using Powershell in Windows against Git repo and want to just include untracked files you can run:
所以下次,如果你是在 Windows 中使用 Powershell 来对抗 Git 存储库的开发人员,并且只想包含未跟踪的文件,你可以运行:
AddUntracked-Git
AddUntracked-Git
This follows the Powershell convention where you have verb-nouns.
这遵循 Powershell 约定,其中您有动词-名词。
回答by atreyHazelHispanic
git add . (add all files in this directory)
git 添加。(添加此目录中的所有文件)
git add -all (add all files in all directories)
git add -all (添加所有目录下的所有文件)
git add -N can be helpful for for listing which ones for later....
git add -N 可以帮助列出以后使用的那些......
回答by Simeon
If you have thousands of untracked files (ugh, don't ask) then git add -i
will not work when adding *
. You will get an error stating Argument list too long
.
如果您有数千个未跟踪的文件(呃,别问),那么git add -i
在添加*
. 您将收到一个错误说明Argument list too long
。
If you then also are on Windows (don't ask #2 :-) and need to use PowerShell for adding all untracked files, you can use this command:
如果您当时也在 Windows 上(不要问 #2 :-)并且需要使用 PowerShell 添加所有未跟踪的文件,则可以使用以下命令:
git ls-files -o --exclude-standard | select | foreach { git add $_ }