Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
872 views
in Technique[技术] by (71.8m points)

svn - How to `git add` non-recursively?

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.

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.

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?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...