how to add all currently untracked files/folders to git ignore?

Try this:

git status -s | grep -e "^\?\?" | cut -c 4- >> .gitignore

Explanation: git status -s gives you a short version of the status, without headers. The grep takes only lines that start with ??, i.e. untracked files, the cut removes the ??, and the rest adds it to the .gitignore file.


A simpler command to do this is

git ls-files --others --exclude-standard >> .gitignore

You might want to edit the result to replace repeated patterns with wildcards.


If your working tree is clean except for the untracked files/folders, a shorter solution is possible using awk:

git status -s | awk '{print $2}' >> .gitignore

Tags:

Git

Gitignore