如何在初始git add期间添加所有文件而不添加任何目录?

时间:2013-06-07 20:32:26

标签: git

假设我有一个目录,其中有一堆文件和git中的其他目录。

如果我这样做

git init .
git add .

我将把包括目录在内的所有内容添加到我的git存储库中。 但是,如果我只想添加当前目录中的文件(不对目录进行递归遍历),是否有非手动方式来执行此操作?

手动方式是使用其他工具挑选文件并在这些文件上运行git-add。

4 个答案:

答案 0 :(得分:1)

一个选项是:

git add --interactive

允许您逐个选择文件。可能是繁重的;但它会允许你跳过目录。你有这个:

find . -depth 1 -and ! -type d -exec git add {} \;

以下是一个例子:

ebg@tsuki(26)$ find . -depth  1 -and ! -type d -print
./a
./b
ebg@tsuki(27)$ find . -depth  1 -and ! -type d -exec git add {} \;
ebg@tsuki(28)$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   a
#   new file:   b
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   c/

答案 1 :(得分:1)

不幸的是,Git没有内置的功能。 你可以通过shell循环轻松完成。假设您正在使用bash,这将起作用:

#!/bin/bash

for f in `ls -A`; do
    if [ -f $f ]; then
        git add $f
    fi
done

将添加当前目录中的所有文件。

请注意,与所有bash脚本一样,如果您只需要一次,则可以在一行上写下:
for f in $(ls -A); do if [ -f $f ]; then git add $f; fi; done

这只是一个概念证明脚本,当然可以改进;例如它可以先构建一个列表,然后在该列表上调用git add一次。

答案 2 :(得分:1)

这样的东西
find . -maxdepth 1 -type f | xargs git add --

也许?

第一部分列出当前目录中的所有文件(但由于-maxdepth而不在子目录中),xargs将此列表作为git add的参数附加。

您还可以尝试更强大的

find . -maxdepth 1 -type f -exec git add -- \{\} \+

如果您的find版本支持它。 (或者将\+替换为\;,但这会慢一些。)

另请参阅:Bash: How to list only files?

答案 3 :(得分:0)

根据文件和目录的命名方式,总是值得信赖:

git add *.*
相关问题