0

My scenario: I have a Visual Studio project that I initialize in Git as a new repository (git init). Now I want to specify which files should be tracked (git add). But this command always adds files from every single subfolder. Is there a way to only add (for tracking) files in the present folder without subfolders? /Mattias

1 Answers1

0

You could use a .gitignore and add the folders that you don't want to add. Since i'm guessing this is a onetime issue i would dig into that and add there the folders that i dont want.

For example if you'r in the folder foo and it have to subfolders bar1/ and bar2/ you could add those two subfolders to your gitignore. You could use several .gitignores.

# Ignore  all subdirectories in this directory using gitignore
*/
# Ignore some subdirectories
bar/
bar2/

If you'r standing in the correct directory you could use something like:

git add $(find -type f -maxdepth 1)

or

git add FolderName/\*.* 

The . adds every file from every file type, but directories don't have extensions so they wont be added.

Basically for first project setup it will probably take a little while, but it's worth it.

See also similar thread: How to `git add` non-recursively?

For reference on gitignore: http://git-scm.com/docs/gitignore

Community
  • 1
  • 1
Englund
  • 1,067
  • 7
  • 13
  • I cant seem to make the .gitignore file work. The file is positioned in the folder where I run: git add *.txt .gitignore has the line: bin/ But I still get a .txt file from within bin tracked. – Mattias Carleson Jul 09 '15 at 09:21
  • Try changing bin/ to bin/*.txt or /bin/*.txt It should work anyways, not sure whats the problem. – Englund Jul 09 '15 at 09:26
  • Well, not even adding *.txt makes it ignore txt files in the current directory. My file name is .gitignore, alright. It seems like the whole .gitignore file is ignored. Ironic... – Mattias Carleson Jul 09 '15 at 09:31
  • If you have already added the file to git you need to remove it before the gitignore will work. That might be the problem. – Englund Jul 09 '15 at 09:42
  • But I havent. Its a new project not commited yet. Just now I tried this: I have a directory with some files, never commited or anyting. I run git init. I create a .gitignore with one line, orderDB.txt. I then execute git add *.txt. Now I am under the impression that all .txt files should be tracked except the orderDB.txt. But thats not what happens. All txt including orderDB.txt gets tracked. I really dont understand why. – Mattias Carleson Jul 09 '15 at 12:28