简体   繁体   中英

Ignore files from git diff or git status using command shell

I would like to ignore files when running a git diff or a git status from the command shell using a simple file pattern.

I'm able to "apply the diff or status only to the files that match this pattern", but I don't know how to say "ignore the files that match this pattern" instead.

What I'm doing right now to apply the commands to a subset of files is:

$ git status */models.py
$ git diff */models.py

I'd like to be able to do this from the command line, without setting preferences to Git. Is that possible?

There is no standard way, no command line flag to do this.

If the changes you want to exclude are good changes that you will eventually want to commit, then I suggest to add them to the staging area. That way you can do git diff and they won't show up.

On the other hand, if the changes you want to exclude are crappy experimental changes that you probably won't want to commit, then it can be dangerous to add them to the staging area, as that way they are easy to commit by mistake. In that case it's better to add all the good changes to the staging area and view their diff with git diff --cached .

As for excluding from status, I recommend simply grep -v :

git status | grep -v stuff-to-exclude

您可以使用git ls-filesgrepxargs如:

git ls-files | grep -v models.py | xargs git diff

As illustrated in " git update-index --assume-unchanged on directory ", you can test and play with the command:

 git update-index --assume-unchanged 

That way, a git diff or git status should ignore the files you just marked as "unchanged".

Note: to revert that: git update-index --no-assume-unchanged .

(if this doesn't fully work ,try git update-index --skip-worktree ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM