16

有没有办法只添加新文件而不用git添加修改过的文件?也就是说,使用 git status 列为未跟踪的文件。

当然,除了单独添加每个文件。

在我的情况下,这样做并不是绝对必要的,对我来说真正的问题在这里得到了回答:如何使 git-diff 和 git log 忽略新的和已删除的文件?

也就是说,不要在新文件上显示差异,所以我问这个问题更多,因为我在任何地方都找不到答案。

4

2 回答 2

29

也许

git add $(git ls-files -o --exclude-standard)

git ls-files让您列出由 git 管理的文件,并按某些选项过滤。-o在这种情况下,将其过滤为仅显示“其他(即未跟踪的文件)”

$(...)语句将该命令的返回值作为参数传递给git add.

于 2013-04-02T10:36:49.877 回答
7

您可以使用 git status 的短模式(请参阅man git-status(1)),它提供以下输出:

没有短模式:

$ git status

...


# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   README
#   application/libraries/Membres_exception.php
no changes added to commit (use "git add" and/or "git commit -a")

短模式:

$ git status -s
 M application/models/membre_model.php
?? README
?? application/libraries/Membres_exception.php

然后使用 grep、awk 和 xarg,您可以添加第一列为??.

$ git status -s | grep '??' | awk '{ print $2 }' | xargs git add 

并看到它有效:

$ git status
# On branch new
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   README
#   new file:   application/libraries/Membres_exception.php
于 2013-04-02T10:55:03.143 回答