Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to `git add` non-recursively?

Tags:

git

recursion

svn

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?

like image 661
bluenote10 Avatar asked Jun 05 '15 09:06

bluenote10


People also ask

How do I add untracked files?

Add All Files using Git Add. The easiest way to add all files to your Git repository is to use the “git add” command followed by the “-A” option for “all”. In this case, the new (or untracked), deleted and modified files will be added to your Git staging area.

Does git add include subdirectories?

It is important to note that git init will create a repository that includes subdirectories and their files—there is no need to create separate repositories nested within the planets repository, whether subdirectories are present from the beginning or added later.

Is git add recursive?

The command git add can add files and folders in the working tree to the staging area. It also takes the pathname (extension) for the file or a directory. The git add command will recursively add all the files in that particular directory.

How do I add all changes to git?

Enter git add --all at the command line prompt in your local project directory to add the files or changes to the repository. Enter git status to see the changes to be committed. Enter git commit -m '<commit_message>' at the command line to commit new files/changes to the local repository.


3 Answers

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

like image 172
Sven Marnach Avatar answered Oct 20 '22 03:10

Sven Marnach


If you want to add only the files from the directory without any other subfolders you can do something like:

git add FolderName/\*.* 

Where *.* means every file, from every file type. Folders don't have extensions so they won't pass.

like image 6
Wald Avatar answered Oct 20 '22 01:10

Wald


A simple alternative is to add a single file in the subdirectory, then you can proceed to add other files as desired.

git add somedir/goodfile
cd somedir
git add anotherfile
like image 1
David Sheeks Avatar answered Oct 20 '22 01:10

David Sheeks