gitgitignore

git: How to ignore all present untracked files?


Is there a handy way to ignore all untracked files and folders in a git repository?
(I know about the .gitignore.)

So git status would provide a clean result again.


Solution

  • As already been said, to exclude from status just use:

    git status -uno  # must be "-uno" , not "-u no"
    

    If you instead want to permanently ignore currently untracked files you can, from the root of your project, launch:

    git status --porcelain | grep '^??' | cut -c4- >> .gitignore
    

    Every subsequent call to git status will explicitly ignore those files.

    UPDATE: the above command has a minor drawback: if you don't have a .gitignore file yet your gitignore will ignore itself! This happens because the file .gitignore gets created before the git status --porcelain is executed. So if you don't have a .gitignore file yet I recommend using:

    echo "$(git status --porcelain | grep '^??' | cut -c4-)" > .gitignore
    

    This creates a subshell which completes before the .gitignore file is created.

    COMMAND EXPLANATION I'll explain the command:

    ANOTHER VARIANT for those who prefer using sed instead of grep and cut, here's another way:

    git status --porcelain | sed -n -e 's/^?? //p' >> .gitignore