sjas
sjas

Reputation: 19787

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.

Upvotes: 175

Views: 248117

Answers (12)

Diego
Diego

Reputation: 5556

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:

  • git status --porcelain is used instead of git status --short because manual states "Give the output in an easy-to-parse format for scripts. This is similar to the short output, but will remain stable across git versions and regardless of user configuration." So we have both the parseability and stability;
  • grep '^??' filters only the lines starting with ??, which, according to the git status manual, correspond to the untracked files;
  • cut -c4- removes the first 3 characters of every line, which gives us just the relative path to the untracked file;
  • the | symbols are pipes, which pass the output of the previous command to the input of the following command;
  • the >> and > symbols are redirect operators, which append the output of the previous command to a file or overwrites/creates a new file, respectively.

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

Upvotes: 306

hex
hex

Reputation: 1

To add to @Diego's answer above, I think the mentioned solutions will fail for files with spaces (and possibly other special characters, like " itself, although I haven't checked) in their names. This line:

echo "$(git status --porcelain | grep '^??' | cut -c4- | sed 's/^\"//;s/\"$//')" >> .gitignore

removes starting and trailing double quotes where present which fixes it.

I'm not very good with sed though, there may be a better way to do this, or edge cases.

Upvotes: 0

Nuha Shawahna
Nuha Shawahna

Reputation: 41

git reset --hard HEAD
git clean -fxd

will remove any untracked files, and make your local branch up to date with the remote branch. this is good option if you don't want to preserve any local changes you already made.

Upvotes: 4

Snowcrash
Snowcrash

Reputation: 86367

None of the above answers worked for me.

In my case, I want a personal config file to be ignored permanently (git status -uno only works for one use).

I'm part of a team so can't add it to .gitignore as that's checked in.

Mu solution was to add it to:

.git/info/exclude

which is like your own personal .gitignore file.

Upvotes: 16

Martin
Martin

Reputation: 1001

IMHO better than the accepted answer is to use the following:

git config --local status.showUntrackedFiles no

The accepted answer does not work for when new files are added that are not in .gitignore

Upvotes: 14

Jenny D
Jenny D

Reputation: 1245

Two ways:

  • use the argument -uno to git-status. Here's an example:

    [jenny@jenny_vmware:ft]$ git status
    # On branch ft
    # Untracked files:
    #   (use "git add <file>..." to include in what will be committed)
    #
    #       foo
    nothing added to commit but untracked files present (use "git add" to track)
    [jenny@jenny_vmware:ft]$ git status -uno
    # On branch ft
    nothing to commit (working directory clean)
    
  • Or you can add the files and directories to .gitignore, in which case they will never show up.

Upvotes: 6

William George
William George

Reputation: 35

I came here trying to solve a slightly different problem. Maybe this will be useful to someone else:

I create a new branch feature-a. as part of this branch I create new directories and need to modify .gitignore to suppress some of them. This happens a lot when adding new tools to a project that create various cache folders. .serverless, .terraform, etc.

Before I'm ready to merge that back to master I have something else come up, so I checkout master again, but now git status picks up those suppressed folders again, since the .gitignore hasn't been merged yet.

The answer here is actually simple, though I had to find this blog to figure it out:

Just checkout the .gitignore file from feature-a branch

git checkout feature-a -- feature-a/.gitignore
git add .
git commit -m "update .gitignore from feature-a branch"

Upvotes: 0

sjas
sjas

Reputation: 19787

Found it in the manual

The mode parameter is used to specify the handling of untracked files. It is optional: it defaults to all, and if specified, it must be stuck to the option (e.g. -uno, but not -u no).

git status -uno

Upvotes: 28

poolie
poolie

Reputation: 9526

If you want to permanently ignore these files, a simple way to add them to .gitignore is:

  1. Change to the root of the git tree.
  2. git ls-files --others --exclude-standard >> .gitignore

This will enumerate all files inside untracked directories, which may or may not be what you want.

Upvotes: 67

vhanla
vhanla

Reputation: 839

In case you are not on Unix like OS, this would work on Windows using PowerShell

git status --porcelain | ?{ $_ -match "^\?\? " }| %{$_ -replace "^\?\? ",""} | Add-Content .\.gitignore

However, .gitignore file has to have a new empty line, otherwise it will append text to the last line no matter if it has content.

This might be a better alternative:

$gi=gc .\.gitignore;$res=git status --porcelain|?{ $_ -match "^\?\? " }|%{ $_ -replace "^\?\? ", "" }; $res=$gi+$res; $res | Out-File .\.gitignore

Upvotes: 2

football
football

Reputation: 308

-u no doesn't show unstaged files either. -uno works as desired and shows unstaged, but hides untracked.

Upvotes: 5

VonC
VonC

Reputation: 1329622

If you have a lot of untracked files, and don't want to "gitignore" all of them, note that, since git 1.8.3 (April, 22d 2013), git status will mention the --untracked-files=no even if you didn't add that option in the first place!

"git status" suggests users to look into using --untracked=no option when it takes too long.

Upvotes: 1

Related Questions