如何让git忽略忽略.gitignore

时间:2014-09-18 09:43:01

标签: git gitignore git-status

我有一个包含.gitignore文件的Git存储库,该文件列出了一些要忽略的文件(在构建期间创建)。

$ cat .gitignore
foo
$ ls
bar
$ git status
On branch master
nothing to commit, working directory clean
$ touch foo
$ ls
bar foo
$ git status
On branch master
nothing to commit, working directory clean
$

在该存储库的特定克隆中,我执行想要查看.gitignore中列出的文件。

可以使用--ignored的{​​{1}}选项完成此操作:

git status

尼斯。我的问题是:如何为给定的存储库创建永久存储器,以便我不必记住添加$ git status On branch master nothing to commit, working directory clean $ git status --ignored On branch master Ignored files: (use "git add -f <file>..." to include in what will be committed) foo nothing to commit, working directory clean $ 标志?

--ignored

我确定有一些$ git status On branch master Ignored files: (use "git add -f <file>..." to include in what will be committed) foo nothing to commit, working directory clean $ uration允许我配置我的克隆以忽略git config的内容......

另外,我不想为所有存储库提供此选项;仅限少数几个。

3 个答案:

答案 0 :(得分:1)

您需要做的是为istatus命令创建一个别名(我们称之为$ git status)。

$  git istatus
On branch master
Ignored files:
  (use "git add -f <file>..." to include in what will be committed)

    foo

nothing to commit, working directory clean

如何做到described here,甚至a similar question on SO

TL; DR:基本上你只需要在〜/ .gitconfig中添加行:

[alias]
    istatus = status --ignored

答案 1 :(得分:1)

不可能(尚未)

Git(至少v2.1及更早版本)允许您配置git status以默认列出被忽略的文件。

Git允许您做的是通过status.showUntrackedFiles选项修改git status关于未跟踪文件的行为。

也许未来的Git版本会以&#34; status.showIgnoredFiles&#34;选项...

那么,你能做什么?

1 - 在本地定义别名

正如Kleskowy在他的answer中所建议的那样,显而易见且可能是最好的事情是定义存储库本地的别名:

git config alias.istatus "status --ignored"

然后,在相关存储库中运行git istatus而不是git status也会列出被忽略的文件。

2 - 在git

周围定义一个小包装器

警告:以下方法适用于用户级别,而不适用于存储库级别。

或者,如果您不反对git周围有一个小包装器,则可以定义以下内容,在--ignored运行时自动使用git status标志:

git() {
    if [[ ($1 == "status") ]]; then
        command git status --ignored "${@:2}";
    else
        command git "$@";
    fi;
}

将这些行放在.<shell>rc文件中并获取后者。

以下是我的某个存储库中该包装器的测试结果(在bash中):

enter image description here

请注意,即使--ignored标志未明确使用,也会列出被忽略的文件。

结论

除非有人能想出更好的方法,否则你所要求的实际上是对Git的功能要求;因此,我不确定你的问题在这里有它的位置......

答案 2 :(得分:0)

所提议的解决方案都没有达到我真正想要的效果。

然而,@Jubobs对包装函数的建议终于让我知道如何实际做到这一点。

概要

  • 标记更改的给定存储库,最好使用 git方式。这可以使用git config和一些用户定义的变量

  • 来完成
  • 为什么要停止向单个命令(--ignored)添加单个选项(status)? 如果我们可以为每个git命令传递默认参数,那就太好了,

实施

所以我最终得到了以下小片段,在初创公司采购:

git() {
  local GIT=$(which git)
  case "x$1" in
    x|xconfig)
      ${GIT} "$@"
    ;;
    *)
      ${GIT} $1 $(${GIT} config --get $1.options) "${@:2}"
    ;;
  esac
}

所以在我的特殊存储库中,我只是这样做:

$ git config --add status.options "--ignored"

之后,在此repo中运行以下命令将向我显示甚至被忽略的文件,而其他存储库将像往常一样运行。

$ git status

讨论

  • 您可以通过创建名为${cmd}.options的配置选项将您的个人选项添加到任何 git命令。 例如如果您希望装饰您的git log输出,只需执行以下操作:

    $ git config log.options "--decorate"
    
  • 出于安全原因,脚本会排除config子命令。这应该可以防止自己在膝盖上射击:

    $ git config add config.options --fuzz
    
  • 最后,它使用一些bashisms(即${@:2})来允许包含空格的参数;如果您的shell不是bash,请小心......

相关问题