Git 状态 - 有没有办法只显示特定目录中的更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/715321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:18:44  来源:igfitidea点击:

Git status - is there a way to show changes only in a specific directory?

gitdirectorystatus

提问by Kuba Suder

I would like to see a list of files modified since the last commit, as git statusshows, but I care only about files located in a single directory. Is there a way to do this? I tried git status <directory>, but it seems this does something completely different (lists all changed files, as they would be if I wrote git add <directory>first).

我想查看自上次提交以来修改的文件列表,如图git status所示,但我只关心位于单个目录中的文件。有没有办法做到这一点?我试过git status <directory>,但它似乎做了一些完全不同的事情(列出所有更改的文件,就像我git add <directory>先写的那样)。

The documentation for git-status doesn't tell much, apart from the fact that it accepts the same options that git-commit does (but git-commit's purpose isn't to show lists of changed files).

除了它接受与 git-commit 相同的选项(但 git-commit 的目的不是显示已更改文件的列表)之外,git-status 的文档并没有说明太多内容。

回答by Sam Doidge

From within the directory:

从目录中:

git status .


You can use any path really, use this syntax:

您真的可以使用任何路径,请使用以下语法:

git status <directoryPath>

For instance for directory with path "my/cool/path/here"

例如对于路径为“my/cool/path/here”的目录

git status my/cool/path/here

回答by CB Bailey

The reason that git statustakes the same options as git commitis that the purpose of git statusis to show what would happen if you committed with the same options as you passed to git status. In this respect git statusis really git commit --preview.

其原因git status是采用相同的选项git commit是的目的git status是为了显示,如果你使用相同的选项致力于为您传递到会发生什么git status。这方面git status真是git commit --preview

To get what you want, you could do this which shows staged changes:

为了得到你想要的,你可以这样做,它显示了分阶段的变化:

git diff --stat --cached -- <directory_of_interest>

and this, which shows unstaged changes:

这显示了未分阶段的更改:

git diff --stat -- <directory_of_interest>

or this which shows both:

或者这两者都显示:

git diff --stat HEAD -- <directory_of_interest>

回答by Can Berk Güder

Simplest solution:

最简单的解决方案:

  1. Go to the directory
  2. git status | grep -v '\.\.\/'
  1. 进入目录
  2. git status | grep -v '\.\.\/'

Of course this discards colors.

当然,这会丢弃颜色。

回答by K-Gun

As a note, if you simplify to check git stats without going to git directory;

请注意,如果您简化检查 git stats 而不去 git 目录;

### create file
sudo nano /usr/local/bin/gitstat

### put this in

#!/usr/bin/env bash

dir=

if [[ $dir == "" ]]; then
    echo "Directory is required!"
    exit
fi

echo "Git stat for '$dir'."

git --git-dir=$dir/.git --work-tree=$dir diff --stat

### give exec perm
sudo chmod +x /usr/local/bin/gitstat

And calling that simple script: gitstat /path/to/foo-project. You can also use it while in foo-projectjust doing gitstat .and so suppose shorter than git status -s, git diff --stator git diff --stat HEADif your are always using console instead of gui's.

并调用那个简单的脚本:gitstat /path/to/foo-project. 你也可以在foo-project做的时候使用它gitstat .,所以假设比 短git status -sgit diff --stat或者git diff --stat HEAD如果你总是使用控制台而不是 gui。

Credits:

学分: