仅添加未跟踪的文件
我发现 Git 中非常有用的命令之一是 git add -u ,可以将除未跟踪文件之外的所有内容都放入索引中。有相反的情况吗?
例如仅将未跟踪的文件添加到索引而不单独识别它们的方法?
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?
Such as a way to add only the untracked files to the index without identifying them individually?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
使用 git add -i 很容易。输入
a
(表示“添加未跟踪”),然后输入*
(表示“全部”),然后输入q
(退出),然后就可以了完毕。要使用单个命令来完成此操作:
echo -e "a\n*\nq\n"|git add -i
It's easy with
git add -i
. Typea
(for "add untracked"), then*
(for "all"), thenq
(to quit) and you're done.To do it with a single command:
echo -e "a\n*\nq\n"|git add -i
git ls-files -o --exclude-standard 提供未跟踪的文件,因此您可以执行如下操作(或为其添加别名):
git ls-files -o --exclude-standard
gives untracked files, so you can do something like below ( or add an alias to it):不完全是您正在寻找的内容,但我发现这非常有用:
将所有文件添加到索引,但不包含其内容。未跟踪的文件现在的行为就像已被跟踪一样。它们的内容将显示在 git diff 中,然后您可以使用 git add -p 交互添加。
Not exactly what you're looking for, but I've found this quite helpful:
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 withgit add -p
.我尝试了这个,它起作用了:
git stash 只会将所有修改的跟踪文件放入单独的堆栈中,然后剩下的文件是未跟踪的文件。然后,通过执行 git add . 将根据需要暂存所有未跟踪的文件。最终,通过执行 git stash pop 来从堆栈中取回所有修改过的文件
I tried this and it worked :
git stash
will only put all modified tracked files into separate stack, then left ones are untracked files. Then by doinggit add .
will stage all files untracked files, as required. Eventually, to get back all modified files from stack by doinggit stash pop
您可以将其添加到 ~/.gitconfig 文件中:
然后,从命令行运行:
You can add this to your ~/.gitconfig file:
Then, from the commandline, just run:
人们建议将 git ls-files 的输出通过管道传输到 git add ,但是如果文件名包含空格或通配符(例如
*。
安全的方法是使用:
其中
-z
告诉 git 使用\0
行终止符,而-0
告诉 xargs 相同。这种方法的唯一缺点是-0
选项是非标准的,因此只有某些版本的xargs
支持它。People have suggested piping the output of
git ls-files
togit add
but this is going to fail in cases where there are filenames containing white space or glob characters such as*
.The safe way would be to use:
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 ofxargs
support it.git ls-files 列出当前目录中的文件。如果您想列出树中任何位置的未跟踪文件,这可能会更好:
要在树中添加所有未跟踪文件:
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:To add all untracked files in the tree:
如果您有数千个未跟踪的文件(呃,别问),那么在添加
*
时git add -i
将不起作用。您将收到一条错误消息,指出参数列表太长
。如果您也在 Windows 上(不要问#2:-)并且需要使用 PowerShell 来添加所有未跟踪的文件,您可以使用以下命令:
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 statingArgument 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:
这里有很多好的技巧,但在 Powershell 中我无法让它工作。
我是一名 .NET 开发人员,我们主要还是使用 Windows 操作系统,因为我们还没有太多使用 .Net core 和跨平台,所以我日常使用 Git 都是在 Windows 环境中,其中使用的 shell 更常见的是 Powershell而不是 Git bash。
可以按照以下过程创建一个别名函数,用于在 Git 存储库中添加未跟踪的文件。
在 Powershell 的 $profile 文件中(如果丢失 - 您可以运行:
New-Item $Profile)
notepad $Profile
现在添加此 Powershell 方法:
保存 $profile 文件并将其重新加载到 Powershell 中。
然后使用以下命令重新加载您的 $profile 文件:
。 $profile
这类似于 *nix 环境中的 source 命令恕我直言。
所以下次,如果您是开发人员,在 Windows 中使用 Powershell 来对抗 Git 存储库
并且只想包含未跟踪的文件,您可以运行:
AddUntracked-Git
这遵循 Powershell 约定,其中有动词名词。
Lot of good tips here, but inside Powershell I could not get it to work.
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.
The following procedure can be followed to create an aliased function for adding untracked files in a Git repository.
Inside your $profile file of Powershell (in case it is missing - you can run:
New-Item $Profile)
notepad $Profile
Now add this Powershell method:
Save the $profile file and reload it into Powershell.
Then reload your $profile file with:
. $profile
This is similar to the source command in *nix environments IMHO.
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:
AddUntracked-Git
This follows the Powershell convention where you have verb-nouns.
git 添加 . (添加此目录中的所有文件)
git add -all (添加所有目录中的所有文件)
git add -N 有助于列出稍后的文件。 ..
git add . (add all files in this directory)
git add -all (add all files in all directories)
git add -N can be helpful for for listing which ones for later....
添加所有未跟踪的文件
git 命令是
另外,如果您想获取有关各种可用选项的更多详细信息,您可以输入命令
而不是第一个命令,这样您将获得更多选项,包括添加所有未跟踪文件的选项,如下所示:
To add all untracked files
git command is
Also if you want to get more details about various available options , you can type command
instead of first command , with this you will get more options including option to add all untracked files as shown below :