简体   繁体   English

使用命令shell忽略git diff或git status中的文件

[英]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. 我想使用简单的文件模式从命令shell运行git diffgit status时忽略文件。

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. 我能够“将diff或status仅应用于与此模式匹配的文件”,但我不知道如何说“忽略匹配此模式的文件”。

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. 我希望能够从命令行执行此操作,而无需将首选项设置为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. 这样你可以做git diff而且它们不会出现。

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 . 在这种情况下,最好将所有好的更改添加到暂存区域,并使用git diff --cached查看它们的差异。

As for excluding from status, I recommend simply grep -v : 至于从状态中排除,我建议只使用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 on directory ”中所示,您可以使用以下命令进行测试和播放:

 git update-index --assume-unchanged 

That way, a git diff or git status should ignore the files you just marked as "unchanged". 这样, git diffgit status应该忽略你刚刚标记为“未更改”的文件。

Note: to revert that: git update-index --no-assume-unchanged . 注意:要恢复: git update-index --no-assume-unchanged

(if this doesn't fully work ,try git update-index --skip-worktree ). (如果这不完全有效,请尝试git update-index --skip-worktree )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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