77

我正在处理一个 maven 项目,我想忽略在我的项目(评估)根文件夹的 /target 文件夹中生成和存储的文件。在我的 git 存储库的根目录中,我有一个 .gitignore 文件,其中包含以下条目(后端只是一个包含 Eclipse 项目评估的文件夹)

backend/Evaluation/logs/
backend/Evaluation/target/

出于某种原因,SourceTree 不会忽略存储在这两个文件夹中的文件,当我编译我的项目时,有一些未提交的更改是 /target 文件夹中的一些 .class 文件。

为什么会这样?我还尝试将 .gitignore 条目更改为

/后端/评估/日志/**

但它也没有用。

有任何想法吗 ?

4

8 回答 8

147

因为问题中有 Sourcetree 标签,所以我将回答您如何使用 Sourcetree 执行此操作,这非常简单。

如前所述,您首先必须从跟踪中删除文件,然后使 Sourcetree 忽略该文件。

  1. 更改文件中的某些内容以显示给您,或者从打开的存储库底部的“文件状态”选项卡中选择它。
  2. 右键单击该文件,您会看到“忽略...”,但它是灰色的,因为如上所述它已经在跟踪中。
  3. 右键单击文件并选择“停止跟踪”
  4. 该文件将显示一个红色按钮,表示它将被删除(从跟踪中)
  5. 同一文件的新条目将显示在“文件状态”中,带有蓝色问号表示新文件(新因为您在 4 中说从跟踪中删除)
  6. 右键单击 5 中的文件,现在您可以看到“忽略...”可以选择。单击它,Sourcetree 将在 .gitignore 文件中为该文件添加一个新条目
  7. 点击右上角的“设置”可以看到.gitignore文件,选择“高级”,然后第一个条目是关于忽略文件的,点击“编辑”,它将被打开。
于 2014-04-11T14:01:57.123 回答
104

假设我们有一个文件无意中添加到我们的存储库中:

stackoverflow$ echo this file is important > junkfile
stackoverflow$ git add junkfile
stackoverflow$ git ci -m 'added a file'

在某些时候,我们意识到该文件并不重要,因此我们将其添加到我们的.gitignore文件中:

stackoverflow$ echo junkfile >> .gitignore
stackoverflow$ git ci -m 'ignore junkfile' .gitignore

稍后,我们对该文件进行一些更改:

stackoverflow$ sed -i 's/ is / is not/' junkfile 

当然,即使文件列在 中.gitignore,我们已经告诉 git 我们要跟踪它,所以git status显示:

stackoverflow$ git status
# On branch master
# 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:   junkfile
#
no changes added to commit (use "git add" and/or "git commit -a")

我们需要从存储库中删除该文件(不从我们的工作树中删除该文件)。我们可以使用以下--cached标志来做到这一点git rm

stackoverflow$ git rm --cached junkfile
rm 'junkfile'

这阶段delete操作...

stackoverflow$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    junkfile
#

...所以我们需要提交它:

stackoverflow$ git commit -m 'removed junkfile from repository'
[master 19f082a] removed junkfile from repository
 1 file changed, 1 deletion(-)
 delete mode 100644 junkfile

现在如果我们运行git statusgit 会忽略这个文件:

stackoverflow$ git status
# On branch master
nothing to commit, working directory clean

即使它仍然在我们的工作树中:

stackoverflow$ cat junkfile 
this file is not important
于 2014-02-07T13:57:26.007 回答
11

我在跟踪 bin 文件夹(整个文件夹)时遇到问题。

所以这里是快速步骤(更直观,因为我更像是一个视觉类型的人):

  1. 出于安全目的,我压缩了整个 bin 文件夹
  2. 将其移至桌面
  3. 将(当前).gitignore 也移动到桌面
  4. 删除整个 BIN 文件夹
  5. 提交更改(通过最喜欢的 git commit 工具)
  6. 提交,推动,完成!
  7. 返回到基本项目的文件夹
  8. 移回 .gitignore 文件
  9. 可选 - 也将 bin 文件夹移回(解压缩)。
  10. 结果,您将看到 git 工具不再跟踪 bin 文件夹中的更改。

我希望这可以帮助别人。

于 2014-04-13T09:36:57.017 回答
11

如果文件已经添加或提交到您的 Git 存储库,您必须先停止跟踪它们,然后它们才会被.gitIgnore忽略。

要停止在 SourceTree 中跟踪文件:

右键单击文件。选择“停止跟踪”。
这不会删除您的文件,它只是停止跟踪它们

从命令行停止跟踪文件:

git rm --cached example.txt
于 2014-11-04T21:16:15.547 回答
2

这对我有用:

git update-index --assume-unchanged some.file
于 2018-05-29T01:17:25.197 回答
2

除了上面已经提供的答案之外,还请注意,如果.gitignore文件不在根文件夹中,则该文件将不起作用。

我有一个项目在.gitignore这里/projectname/.gitignore,因此没有被阅读。所以我把它移到了/.gitignore,一切都好起来了。

于 2015-12-27T09:37:12.640 回答
0

我发现通过删除存储库,然后在重新创建存储库时将 .gitignore 文件放在文件夹中,忽略文件受到尊重。

于 2016-12-04T18:56:58.447 回答
0

挣扎了几个小时,尝试了很多事情,包括这里的建议 - 无济于事......最终对我有用的是在 bitbucket.org 中设置 repo 时创建 .gitignore 文件,将该文件拉到我的本地人,粘贴了我试图自己创建的文件的完全相同的内容,现在终于可以正常工作了。

于 2019-07-29T19:48:19.853 回答