git: How to ignore all present untracked files?

前端 未结 8 1598
北恋
北恋 2020-12-22 14:57

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

相关标签:
8条回答
  • 2020-12-22 15:12

    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 as I'm getting a lot of votes (thank you!) I think I'd better explain the command a bit:

    • 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
    
    0 讨论(0)
  • 2020-12-22 15:16

    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"
    
    0 讨论(0)
  • 2020-12-22 15:18

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

    0 讨论(0)
  • 2020-12-22 15:30

    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.

    0 讨论(0)
  • 2020-12-22 15:31

    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

    0 讨论(0)
  • 2020-12-22 15:36

    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

    0 讨论(0)
提交回复
热议问题