Sourcetree中的.gitignore文件无法正常工作
我正在做一个Maven项目,我想忽略生成并存储在我的项目(评估)根文件夹的/目标文件夹中的文件。在我的git仓库的根目录中,我有一个.gitignore文件,其中包含以下条目(后端只是一个包含eclipse项目评估的文件夹)
backend/Evaluation/logs/ backend/Evaluation/target/ 出于某种原因,SourceTree不会忽略存储在这两个文件夹中的文件,当我编译我的项目时,会有一些未提交的更改,它们是/ target文件夹中的一些.class文件。
为什么发生这种情况? 我也尝试将.gitignore条目更改为
/后端/评估/日志/ **
但是这也不起作用。
有任何想法吗 ?
比方说,我们有一个无意中添加到我们的存储库的文件:
 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 status git会忽略这个文件: 
 stackoverflow$ git status # On branch master nothing to commit, working directory clean 
即使它仍然在我们的工作树中:
 stackoverflow$ cat junkfile this file is not important 
因为在问题中有Sourcetree标签,我会回答你如何用Sourcetree做到这一点 ,这非常简单。
如前所述,您首先必须从跟踪中删除文件,然后使Sourcetree忽略该文件。
- 更改文件中的某些内容,以便显示给您,或者从打开的存储库底部的“文件状态”选项卡中select它。
- 右键点击文件,你会看到“忽略…”,但它是灰色的,因为如上所述已经在轨道上。
- 右键单击文件并select“停止跟踪”
- 该文件将显示一个红色的button,表明它将被删除(跟踪)
- 相同文件的新条目将显示在“文件状态”中,蓝色问号表示新文件(新的,因为你说从4中的跟踪中删除)
- 右键单击文件从5,现在你看到“忽略…”可以select。 点击它,Sourcetree会为.gitignore文件中的文件添加一个新条目
- 如果点击右上angular的“设置”,select“高级”,然后第一项是关于忽略文件,点击“编辑”就会打开.gitignore文件。
如果这些文件已经被添加或提交到你的Git仓库,你必须先停止跟踪它们,然后才会被.gitIgnore忽略。
停止在SourceTree中跟踪文件:
 用鼠标右键单击文件。 select“停止跟踪”。 
  ( 这不会删除你的文件,它只是停止跟踪他们 ) 
要停止从命令行跟踪文件:
 git rm --cached example.txt 
我有一个跟踪bin文件夹(整个文件夹)的问题。
所以这里是快速的步骤(更多的视觉,因为我更像一个视觉types的人):
- 只是为了安全起见,我压缩了整个bin文件夹
- 将其移动到桌面
- 将(当前).gitignore移到桌面上
- 删除整个BIN文件夹
- 提交更改(通过最喜欢的git提交工具)
- 承诺,推动,完成!
- 回到基本项目的文件夹
- 移回.gitignore文件
- 可选 – 移回(解压缩)bin文件夹。
- 因此,您将看到git工具不再跟踪bin文件夹中的更改。
我希望这可以帮助别人。
 除了上面已经提供的答案之外,还要注意.gitignore文件如果不在根文件夹中将不起作用。 
 我有一个项目.gitignore在这里/projectname/.gitignore ,因此没有被阅读。 所以我把它移到/.gitignore ,一切都很好。 
我发现,通过删除存储库,然后在重新创build存储库时在文件夹中创build.gitignore文件,忽略文件受到尊重。