ddd kkk
ddd kkk

Reputation: 21

How to quickly incorporate all untracked files and directories in .gitignore?

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.

Upvotes: 0

Views: 105

Answers (2)

ddd kkk
ddd kkk

Reputation: 21

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

Upvotes: 0

Joseph K. Strauss
Joseph K. Strauss

Reputation: 4903

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

Upvotes: 1

Related Questions