如何非递归`git add`? [英] How to `git add` non-recursively?
问题描述
当我 git添加
文件夹时,整个内容和所有子文件夹会自动执行。如果该文件夹包含我不想提交的子文件夹,则必须手动取消它们,然后将它们添加到 .gitignore
。显式暂停感觉像我在这里做错了什么。
When I git add
a folder, the whole content and all subfolders are staged automatically. In case the folder contains subfolders which I do not want to commit, I have to unstage them manually and add them to .gitignore
afterwards. The explicit unstaging feels like I'm doing something wrong here.
解决方案是编辑 .gitignore
在添加之前。但是,如果文件夹结构非常深/复杂,这有点棘手,因为很容易忘记忽略某些深层嵌套的文件/文件夹。
A solution would be to edit the .gitignore
before adding. But in cases where the folder structure is very deep/complex this is a bit tricky, because it is easy to forget to ignore certain deeply nested files/folders.
我的正在寻找是一个逐步添加
像SVN的 - 非递归
,允许逐级添加文件夹没有分段整个内容。但是我找不到 git add
的这个功能。所以我想知道:对于这种非递归添加,推荐的git工作流程是什么?
What I was looking for is a step-wise add
like SVN's --non-recursive
, allowing to add folders level by level without staging the whole content. However I couldn't find this functionality for git add
. So I'm wondering: What is the recommended git workflow for such a non-recursive add?
考虑到其他人具有确切的相反的问题:也许我上面描述的行为与我的git版本(1.9.1)/设置有关?
Considering that others had the exact opposite problem: Maybe the behavior I described above is an issue with my git version (1.9.1) / settings?
推荐答案
添加一个完整的复杂目录层次结构是一件不寻常的事情(当然不是通常的git开发工作流程的一部分),所以git没有它的特殊功能。您始终可以使用外部工具来组合要添加的文件列表,并将该列表提供给 git add
,例如,以非递归方式添加当前目录中的文件,做
Adding a whole complex directory hierarchy is an unusual thing to do (and certainly not something that happens as part of the usual git development workflow), so git doesn't have a special feature for it. You can always use an external tool to assemble a list of files you want to add and feed that list to git add
, e.g. to only add the files in the current directory non-recursively, do
git add $(find . -type f -maxdepth 1)
或者,您可以使用
Alternatively, you could use
git ls-files --others --directory > file-list
在当前目录中创建未跟踪文件的列表,并在编辑器中进行编辑删除您不想添加的所有内容。 (确保删除 file-list
本身。)然后你可以使用
to create a list of untracked files in your current directory, and edit it in an editor to remove everything you don't want to add. (Make sure to remove file-list
itself.) You can then use
git add $(cat file-list)
在编辑列表中添加文件和目录。 (你离开的目录仍然会递归添加)。
to add the files and directories in the edited list. (Directories you leave in will still be added recursively).
这篇关于如何非递归`git add`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!