21

I'm trying to avoid showing unwanted folder's content (ex:- .settings, .metadata etc ) when i execute git status command.

I modified .gitignore file after adding the folder names to it but still I'm getting all these files when I execute git status.

lfurini
  • 3,729
  • 4
  • 30
  • 48
Raj
  • 1,467
  • 3
  • 23
  • 51

7 Answers7

28

Run this from the git project directory, where dir is directory you want to exclude.

git status . -- ':!dir'

For your example if want to exclude multiple directories (e.g. settings and metadata).

git status . -- ':!settings' ':!metadata'
wizzfizz94
  • 1,288
  • 15
  • 20
  • 1
    This is currently the best option I have used in 2021 – Niallty Aug 13 '21 at 22:22
  • This solution best answers the question as the scope of the question was about git. `grep -v` is an option but it is a more complicated solution. This simpler solution should be preferred. – Will Nielsen Sep 26 '21 at 15:03
  • ignore dir: `git status -s -- ":(exclude)*wwwroot*" ` – scil Jul 01 '22 at 04:42
5

.gitignore works only on untracked files.

To stop tracking an already tracked file in git, type

git rm --cached <file>
git rm -r --cached <folder>

and then commit the change

git commit -m "Stop tracking some files"
rohit89
  • 5,745
  • 2
  • 25
  • 42
5

You can just remove them with grep -v if you're only interested not seeing them.

galva
  • 1,253
  • 10
  • 17
3

I will not echo what others have said about the usage of .gitignore because I believe they have covered it, but I like your question about omitting results from git status.

I have found no way to omit specific directories other than specifying the sub-directory for which you want status, i.e. git status <subdirectory>, where the current directory is the default.

This is insufficient in some cases depending on how a project is organized. For example, in Go when using dep, there is a vendor directory that gets checked in and it would be desirable to exclude only that, but the best I can do is to git status on a series of sub-directories (not fun). Same goes for git diff.

ThatsAMorais
  • 659
  • 8
  • 16
3

According to the git glossary pathspec, if you want a quick peek on the changes made to all the folder other than **/settings/, here is how

git status ':(exclude,top)**/settings/*' ./
zyy
  • 1,271
  • 15
  • 25
1

I have not found a generic solution to this problem using only git status command. However, for the exclusive scenario of developing on a Golang project where you want to exclude the /vendor, I typically append '| grep -v vendor' to the git status.

G-9
  • 35
  • 3
0

.gitignore and related facilities only work on untracked files. As soon as a file is tracked (added to Git) you can put it inside .gitignore, but it will have no effect at all. There is no good way to handle this, besides removing those files from the repository and having them not checked in.

You might read about --assume-unchanged, but this is a very dangerous option. You can tell Git to assume a file is unchanged, even though you have changes in it. But that also means that the security mechanisms that make sure you don't loose uncommitted changes do not work on those files. So if you have a changed .settings and then switch to a branch where the checked in .settings file is different than on the branch you are currently on, the file will be overwritten with the new branch's state and your local changes are lost.

Vampire
  • 35,631
  • 4
  • 76
  • 102
  • What `--assume-unchanged`? Do you mean `git status --assume-unchanged`? – John Jan 10 '22 at 02:24
  • `git-status` doe not have `--assume-unchanged` @John. What I meant is [git update-index --assume-unchanged ...](https://git-scm.com/docs/git-update-index#Documentation/git-update-index.txt---no-assume-unchanged) – Vampire Apr 05 '23 at 18:37