美文网首页
Git 代码管理中 .gitignore 文件无效的解决方法

Git 代码管理中 .gitignore 文件无效的解决方法

作者: 稀释1 | 来源:发表于2021-05-11 10:58 被阅读0次

    在用 Git 进行代码管理的时候,我们会用 .gitignore 文件来描述哪些文件是不需要进行版本管理的,也就是被忽略掉。如果我们在第一次提交的时候,忘记添加 .gitignore 文件或者在首次添加了 .gitignore 文件之后,又对 .gitignore 文件进行了修改,你会发现这两种情况下,.gitignore 文件是不生效的!

    我们可以用 Git 命令行来解决以上问题。

    先以一个 Android 项目作为演示,创建一个 Android 项目,然后不添加 .gitignore 文件,直接提交到本地:

    image.png

    从以上提交的结果可以看出,没有添加 Android 工程应有的 .gitignore 文件,比如 .class 文件没有被忽略掉。现在我们去下载 Android 工程对应的 .gitignore 文件:
    https://github.com/github/gitignore
    找到 Android 工程对应的 .gitignore 文件,下载保存起来。

    备注:请保存文件的后缀名为 [.gitignore],另外:保存下来的文件名中,请将 [Android.gitignore] 修改成 [.gitignore],如果你还是 Android.gitignore 这样的文件名,会造成 .gitignore 文件无效!Windows 下面可能会出现修改失败的情况,可以用 [rename Android.gitignore .gitignore] CMD 命名来修改名字!

    然后我们将此 .gitignore 文件添加到这个仓库中:


    image.png

    注意:如果你发现你添加了 .gitignore 文件之后,.gitignore 文件的变化没有被 track,那么你需要去 SourceTree 的全局 .gitignore_global 文件中查看 .gitignore 文件是不是被声明了不被 track!

    SourceTree –> Preferences –> Git –> Global Ignore List


    image.png

    我们尝试的修改一下 .java 文件,并保存:


    image.png

    从上图中我们可以看到 .java 文件编译产生的 .class 文件依然被 track。而我们的 .gitignore 文件中是有 .class 文件的,说明这个时候添加的 .gitignore 文件是无效果的。

    由于缓存的原因,当在提交之后添加 .gitignore 文件,或者在 .gitignore 文件中增加或者删除,都是无效的。

    我们可以通过命令行的方式,解决以上问题!首先我们可以通过 SourceTree 界面中的 Terminal 打开 Git 命令行终端。或者你可以通过 Git 终端进入到当前项目的根目录:

    image.png

    我们首先在里面输入:”git rm -r –cached .”,此命令为清除缓存!


    image.png

    然后输入:”git add .”,此命令为添加更改变化!备注:此命令,你可以通过 SourceTree 这样的可视化客户端来通过勾选 unstaged files 来操作。

    1

    ifeegoo:GitignoreFileDemo ifeegoo$ git add .
    

    最后:”git commit -m”,此命令为添加更改变化!备注:此命令,你可以提过 SourceTree 这样的可视化客户端来通过点击 commit 来操作。

    image.png

    我们看到上面删除了很多文件,而这些类型的文件正是 .gitignore 文件中声明的不需要被 track 的文件。这种删除是逻辑删除,非物理删除!

    我们来测试下刚才的操作,是否让 .gitignore 文件重新生效:

    git-code-management-test-after-refresh-adding-gitignore-file

    从上图我们可以看出,修改了 .java 文件并保存,并没有出现编译文件 .class 被 track,这说明,这次的重新使 .gitignore 文件生效的操作是可行的!

    如果说,我们现在又要添加某一类型的文件到 .gitignore 文件中,比如 .txt 文件,不让 .txt 文件被 track,我们还是按照刚才的步骤来操作即可!

    git-code-management-adding-new-file-type-in-gitignore-file

    git-code-management-refresh-adding-new-file-type-in-gitignore-file

    但是,如果你想要从 .gitignore 文件中移除某一类型的文件,让这种类型的文件重新被 track 起来,以上方法是不行的!

    git-code-management-removing-file-type-in-gitignore-file

    比如,我们现在移除 .class 类型的文件:

    git-code-management-removing-file-type-in-gitignore-file

    对于以上情况,我们需要用到强制提交的命令:
    git add -f *.class

    <pre class="brush: bash; highlight: [1]; title: ; notranslate" title="" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 13px; white-space: pre-wrap; padding: 9.5px; color: rgb(51, 51, 51); border-radius: 4px; display: block; margin: 0px 0px 10px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.15); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">ifeegoo:GitignoreFileDemo ifeegoogit add -f *.class ifeegoo:GitignoreFileDemo ifeegoo git commit -m "Force add .class file."
    [master 9503b83] Force add .class file.
    11 files changed, 0 insertions(+), 0 deletions(-)
    create mode 100644 bin/classes/com/ifeegoo/demo/gitignorefile/BuildConfig.class
    create mode 100644 bin/classes/com/ifeegoo/demo/gitignorefile/MainActivity.class
    create mode 100644 bin/classes/com/ifeegoo/demo/gitignorefile/Rattr.class create mode 100644 bin/classes/com/ifeegoo/demo/gitignorefile/Rdimen.class
    create mode 100644 bin/classes/com/ifeegoo/demo/gitignorefile/Rdrawable.class create mode 100644 bin/classes/com/ifeegoo/demo/gitignorefile/Rid.class
    create mode 100644 bin/classes/com/ifeegoo/demo/gitignorefile/Rlayout.class create mode 100644 bin/classes/com/ifeegoo/demo/gitignorefile/Rmenu.class
    create mode 100644 bin/classes/com/ifeegoo/demo/gitignorefile/Rstring.class create mode 100644 bin/classes/com/ifeegoo/demo/gitignorefile/Rstyle.class
    create mode 100644 bin/classes/com/ifeegoo/demo/gitignorefile/R.class
    </pre>

    git-code-management-force-commit-after-removing-file-type-in-gitignore-file

    我们修改一下 .java 文件然后保存,看看产生的编译文件 .class 是否再次被 track,答案是肯定的!如果你又想将 .class 文件添加到 .gitignore 文件中,我们采取第一种方式即可生效!

    git-code-management-test-after-refresh-removing-file-type-in-gitignore-file

    Git 代码管理中,我们在没有添加 .gitignore 文件的前提下提交了代码之后再提交 .gitignore 文件,或者是中途添加某一文件类型到 .gitignore 文件中,需要通过以下命令行的方式,让 .gitignore 文件生效:

    <pre class="brush: bash; title: ; notranslate" title="" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 13px; white-space: pre-wrap; padding: 9.5px; color: rgb(51, 51, 51); border-radius: 4px; display: block; margin: 0px 0px 10px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.15);">git rm -r --cached .
    git add .
    git commit -m "Refresh adding .gitignore file."
    </pre>

    如果是中途从 .gitignore 文件中移除某一文件类型,想要这个文件类型重新被 track,需要通过以下命令行的方式,让 .gitignore 文件生效:

    <pre class="brush: bash; title: ; notranslate" title="" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 13px; white-space: pre-wrap; padding: 9.5px; color: rgb(51, 51, 51); border-radius: 4px; display: block; margin: 0px 0px 10px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.15);">git add -f *.class
    git commit -m "Refresh removing .class from .gitignore file."
    </pre>

    备注:注意你所处的分支,如果你在当前分支修改,切换到其他分支是不生效的,如果多人开发,注意合并修改!本人的这个答案在 Stack Overflow 上获得了较高的 Vote:
    http://stackoverflow.com/questions/11451535/gitignore-not-working/32377642#32377642 如果大家觉得这个答案对于你来说有帮助,也欢迎你给我的这个答案投个赞同票,谢谢!

    2017-04-14(Fri) 追加:感谢读者提出来的本文中的一个显示错误:

    <pre class="brush: bash; title: ; notranslate" title="" style="font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 13px; white-space: pre-wrap; padding: 9.5px; color: rgb(51, 51, 51); border-radius: 4px; display: block; margin: 0px 0px 10px; line-height: 20px; word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.15);">git rm -r --cached .
    </pre>

    以上命令行中 cached 前面有两个 “-“,这个主要是格式显示的问题。现已经修改!由于后台堆积了大量的垃圾评论,一不小心清除了提出本文中的一个显示错误的读者,在此表示感谢,如果这个读者看到了,可以告知我一下,谢谢!

    转自:https://www.ifeegoo.com/git-code-management-dot-gitignore-file-has-no-effect-solution.html

    相关文章

      网友评论

          本文标题:Git 代码管理中 .gitignore 文件无效的解决方法

          本文链接:https://www.haomeiwen.com/subject/gtygdltx.html