git: How to ignore all present untracked files?

前端 未结 8 1636
北恋
北恋 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: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

提交回复
热议问题