【问题标题】:Remove untracked file from appearing in git status从 git status 中删除未跟踪的文件
【发布时间】:2017-08-16 21:37:21
【问题描述】:

当我执行git status 时,会出现:

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   src/app.js
    modified:   src/view/view.js
    modified:   src/view/layout.js
    modified:   src/view/layout2.js
    modified:   src/view/layout3.js
    modified:   src/view/layout4.js
    modified:   src/view/layout5.js

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    ../hidemeplease.json

通常我会使用git add -A 来一次性暂存所有更改,但这也会添加未跟踪的文件。所以现在我需要煞费苦心地将我的更改一次一个文件暂存。

如何阻止未跟踪的文件出现在git status 中?我不想删除该文件,只是忽略它,或者让git status 忽略它。

我尝试过这样做:

git update-index --assume-unchanged hidemeplease.json

但无济于事。

有什么想法吗?

【问题讨论】:

  • 所以这和Javascript有关?
  • 这些文件是 javascript 文件 :)

标签: javascript git github terminal


【解决方案1】:

未跟踪的文件,在 Git 中,是一个路径名没有出现在索引中的文件。

由于它不在索引中,您不能告诉git update-index 将索引条目标记为“假定未更改”或“跳过工作树”。但这实际上是消息。

如果文件不在索引中,git status 通常会抱怨它,git add .添加它。为防止这些情况发生,请在 .gitignore 文件中列出文件的路径名。

.gitignore 文件包含 Git 将关闭而不是自动添加的路径名列表。所以它真的应该叫.git-shut-up-about-these-files-and-do-not-auto-add-them。不过,这个名字有点笨拙,因此是.gitignore

请注意,如果一个文件的路径名 在索引中,则在 .gitignore 文件中列出该文件根本没有任何作用。这就是为什么当前的投诉是好消息。

【讨论】:

    【解决方案2】:

    如果您想忽略它,您可能需要将其添加到项目根目录中的.gitignore 文件中,或者至少添加到您的 git 根目录中。

    考虑将.gitignore 放在与文件hidemeplease.json 相同的文件夹中。那么前一个文件的内容可能就这么简单:

    hidemeplease.json
    

    【讨论】:

      猜你喜欢
      • 2011-06-04
      • 2021-04-08
      • 2014-08-09
      • 2018-07-21
      • 2019-10-30
      • 1970-01-01
      • 2014-02-12
      • 2016-02-27
      • 2017-05-10
      相关资源
      最近更新 更多