假设我在git中有一个目录,里面有一堆文件和其他目录。
如果我这样做了
git init .
git add .我将把包括目录在内的所有内容都添加到我的git库中。但是,如果我只想添加当前目录中的文件(没有递归遍历目录),有没有非手动的方法?
手动方法是使用另一个工具挑选文件,并对这些文件运行git-add。
发布于 2013-06-08 04:40:50
一种选择是:
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/发布于 2013-06-08 04:41:12
不幸的是,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。
发布于 2013-06-08 04:41:40
就像这样
find . -maxdepth 1 -type f | xargs git add --也许吧?
第一部分列出了当前目录(但不是子目录,由于-maxdepth)中的所有文件,xargs将此列表作为参数附加到git add。
您也可以尝试更健壮的
find . -maxdepth 1 -type f -exec git add -- \{\} \+如果您的find版本支持它。(或者将\+替换为\;,但这样运行速度会更慢。)
https://stackoverflow.com/questions/16992479
复制相似问题