git 添加所有跟踪文件 - 包括父目录中的文件

git add all tracked files - also those in parent directory

假设我有一个 git 根文件夹 mine_git,其中有一个子目录 subdir。所以我做了一些工作,我在 subdir - git status 列出所有更改的文件:

subdir$ git status -uno
# On branch master
# ...
#
#   modified:   mysubdirfile.txt
#   modified:   ../a-main-file.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

K,所以我想将所有这些跟踪和修改的文件添加到暂存区(或缓存?索引?不确定名称),以便之后提交;所以我发出:

subdir$ git add -u

...然后我再次检查:

subdir$ git status -uno
# On branch master
# Changes to be committed:
#   ...
#
#   modified:   mysubdirfile.txt
#
# Changes not staged for commit:
#   ...
#
#   modified:   ../a-main-file.txt
#
# Untracked files not listed (use -u option to show untracked files)

因此,只有我当前位置下的那些文件被 git add 编辑,而 不是 parent/sibling 文件夹中的那些文件 - 即使这些文件被跟踪这个 git 存储库,并显示在 git status!

然后我通常需要手动复制粘贴文件名,这样才能做到git add ../a-main-file.txt。显然这是一个痛苦 - 那么是否有一些命令可以添加 git status -uno 列出的 所有 文件,而不管它们是否位于当前级别以下?

最新版本的 git (2.4.3) 应该已经这样做了。来自 man 1 git-add:

   -u, --update
       Update the index just where it already has an entry matching <pathspec>. 
       This removes as well as modifies index entries to match the working tree, 
       but adds no new files.

       If no <pathspec> is given when -u option is used, all tracked files in the entire
       working tree are updated (old versions of Git used to limit the update to the 
       current directory and its subdirectories).

也就是说,您可以尝试 git add -u -- ../relative/path/to/project/root。我手边没有旧的 git 版本,所以无法测试它。