3

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.

chr
  • 121
  • 8
  • Does this answer your question? [How do you make Git ignore files without using .gitignore?](https://stackoverflow.com/questions/653454/how-do-you-make-git-ignore-files-without-using-gitignore) – phd Apr 20 '20 at 08:48
  • https://stackoverflow.com/search?q=%5Bgit%5D+ignore+file+without+.gitignore – phd Apr 20 '20 at 08:48

3 Answers3

4

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.

Saurabh P Bhandari
  • 6,014
  • 1
  • 19
  • 50
  • 1
    This works well for me, especially as I was already using ```alias gst='git status'```, which I'm now modifying to ```alias gst='git -c color.ui=always status' ```. Thus, the following works nicely now: ```gst | grep -v ```. Note: The "gst" is just my own acronym for "git status" as it's quick to type. – chr Apr 20 '20 at 06:56
1

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.

David Z
  • 128,184
  • 27
  • 255
  • 279
  • For my use case this feels too much like modifying the state, i.e. I have to remember to undo it later. – chr Apr 20 '20 at 07:02
  • Ah, are you looking for something that can be given as an option to `git status`, like for one-time use? In that case, yeah, this is not what you need. I'll leave the answer here in case it benefits other people in similar situations, though. – David Z Apr 20 '20 at 07:10
1

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.

Magnuti
  • 63
  • 9