'How to exclude some directories from output of git status without having to modify the working tree (e.g. _not_ modifying .gitignore)

In my working tree there's a directory with several modified files that I temporarily would like to not see when I do git status. But this should ideally be done without modifying the "state" of my working tree. How could I do this?

I looked at man git-status but couldn't see an option to exclude a specific directory.

Some workarounds:

  • Use git status | grep -v dir-to-exclude, but then I lose the pretty colours.
  • Specify all the other directories and files as arguments to git status, i.e. git status dir-1 dir-2 dir-3 file-1 file-2
  • Use git stash to temporarily store modifications in dir-to-exclude/, but that modifies my state
  • Temporarily add dir-to-exclude/ to .gitignore, but that modifies the state of my working tree and I have remember to revert the change. It also does not work for modified version controlled files.
  • Use some other command than git status, if one exists???

If there's no ready made option for git status, then somehow using grep without losing the pretty colours is perhaps what I should be using.

git


Solution 1:[1]

Try this

 git -c color.ui=always status | grep -v <dir-to-exclude>

See Colors in Git section here

Git fully supports colored terminal output, which greatly aids in visually parsing command output quickly and easily. A number of options can help you set the coloring to your preference.

color.ui

Git automatically colors most of its output, but there’s a master switch if you don’t like this behavior.

You can also set it to always to ignore the difference between terminals and pipes.

Solution 2:[2]

This might be an appropriate use for git update-index --skip-worktree. To quote from the documentation,

Skip-worktree bit can be defined in one (long) sentence: When reading an entry, if it is marked as skip-worktree, then Git pretends its working directory version is up to date and read the index version instead.

In other words, if you set this flag on a file with git update-index --skip-worktree <filename>, then Git will ignore the actual contents and metadata of the file on disk and will simply pretend it's unmodified. This only applies for read operations; if you run a Git operation that would write to the file (e.g. git checkout or git reset), then Git will give you a warning.

Personally I have this command aliased to git ignore, and the opposite version, git update-index --no-skip-worktree, aliased to git unignore. Of course if you're going to do this, you have to remember that this is entirely unrelated to the mechanism that uses .gitignore files, so you might want to choose a different name if there is any chance of confusion.

Solution 3:[3]

Duplicate of How to exclude unwanted folders content to be shown when I execute 'git status' command

Use git status . -- ':!dir' to exclude the dir folder when running git status.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 David Z
Solution 3 Ramriez