0

I have a large Git repository that has many random files and directories in it that should be ignored. When I run git status, I get a lot of noise.

Those files and directories don't follow a specific pattern. I could manually add them one by one to .gitignore, but this is tedious.

Is there a rapid way to add many or all of the "untracked files" returned by git status to .gitignore? This seems like such an obvious task that I imagine a solution must exist.

ddd kkk
  • 21
  • 1
  • 3
  • Yes, there is a way. In the `.gitignore` file, you can ignore a lot of things all in one go, only to "unignore" some of them further down the file; see [this](http://stackoverflow.com/questions/25715898/whats-the-difference-between-git-ignoring-directory-and-directory). For a more specific answer than this, though, you would need to tell us about the structure of your working tree. – jub0bs Jan 15 '15 at 16:08
  • Why not highlight and copy all the filepaths (at once) from `git status`'s output and paste them into your `.gitignore` file? – ajp15243 Jan 15 '15 at 16:17
  • It might help to organize your ignored files better, keeping them in a single ignored subfolder. – Bruce Jan 15 '15 at 16:27
  • Someone edited the title of my question, but it now no longer really reflects my original intention. Perhaps a better title would be "How to include all untracked files in .gitignore?" – ddd kkk Jan 15 '15 at 19:34

2 Answers2

1

You can create a simple Git alias, and then call it like this:

Edited: In reponse to ddd kkk's comment

git config alias.ignore-untracked "! git status -s | grep '??' | perl -pe 's/.{3}/\//' >>  .gitignore"
git ignore-untracked
Joseph K. Strauss
  • 4,683
  • 1
  • 23
  • 40
0

Based on Joseph Strauss's solution, the final alias definition I am using is:

git config alias.ignore-untracked "! git status -s | grep '??' | cut -d\  -f2- | awk '{print \"/\" \$0; }' | sed '1i # ignore-untracked: $(date)' >> .gitignore"

This prepends the additions to gitignore with a comment that indicates the date the files were added.

This code is executed as follows:

git ignore-untracked
ddd kkk
  • 21
  • 1
  • 3