Sourcetree中的.gitignore文件无法正常工作

Ste*_*eSt 63 git atlassian-sourcetree sourcetree

我正在开发一个maven项目,我想忽略生成并存储在我的项目(Evaluation)根文件夹的/ target文件夹中的文件.在我的git存储库的根目录中,我有一个带有以下条目的.gitignore文件(后端)只是一个包含eclipse项目评估的文件夹)

backend/Evaluation/logs/
backend/Evaluation/target/
Run Code Online (Sandbox Code Playgroud)

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

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

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

但它也没有用.

有任何想法吗 ?

mfg*_*cha 125

因为问题中有Sourcetree标签,我会回答你如何使用Sourcetree做到这一点,它非常简单.

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

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

  • SourceTree得到了很好的回答.不是每个人都使用命令行. (7认同)
  • 请注意,如果您在单击"停止跟踪"之前手动尝试将文件添加到.gitignore文件中,则步骤5将无法按照说明进行操作.这些文件不会显示在您的存储库中,因为它们被正确忽略. (5认同)

lar*_*sks 98

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

stackoverflow$ echo this file is important > junkfile
stackoverflow$ git add junkfile
stackoverflow$ git ci -m 'added a file'
Run Code Online (Sandbox Code Playgroud)

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

stackoverflow$ echo junkfile >> .gitignore
stackoverflow$ git ci -m 'ignore junkfile' .gitignore
Run Code Online (Sandbox Code Playgroud)

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

stackoverflow$ sed -i 's/ is / is not/' junkfile 
Run Code Online (Sandbox Code Playgroud)

当然,即使列出了文件.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")
Run Code Online (Sandbox Code Playgroud)

我们需要从存储库中删除此文件(不从我们的工作树中删除该文件).我们可以使用以下--cached标志执行此操作git rm:

stackoverflow$ git rm --cached junkfile
rm 'junkfile'
Run Code Online (Sandbox Code Playgroud)

这使delete操作阶段...

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

......所以我们需要承诺:

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

现在,如果我们运行git statusgit将忽略此文件:

stackoverflow$ git status
# On branch master
nothing to commit, working directory clean
Run Code Online (Sandbox Code Playgroud)

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

stackoverflow$ cat junkfile 
this file is not important
Run Code Online (Sandbox Code Playgroud)

  • 很好和精心的答案,但需要使用命令行而不是SourceTree (3认同)

小智 11

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

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

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

我希望这可以帮助别人.


tfm*_*gue 11

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

要停止在SourceTree中跟踪文件:

右键单击文件.选择"停止跟踪".
(这不会删除你的文件,只是停止跟踪它们)

要停止从命令行跟踪文件:

git rm --cached example.txt
Run Code Online (Sandbox Code Playgroud)