简体   繁体   English

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

[英].gitignore file in Sourcetree not working

I am working on a maven project and I want to ignore the files generated and stored in the /target folder of my project (Evaluation) root folder.In the root of my git repository I have a .gitignore file with the following entries (backend is just a folder which contains the eclipse project Evaluation) 我正在研究一个maven项目,我想忽略生成并存储在我的项目(评估)根文件夹的/ target文件夹中的文件。在我的git存储库的根目录中,我有一个带有以下条目的.gitignore文件(后端)只是一个包含eclipse项目评估的文件夹)

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

For some reason SourceTree does not ignore the files stored in these two folders and when I compile my project there are some uncommitted changes which are some .class files inside the /target folder. 由于某种原因,SourceTree不会忽略存储在这两个文件夹中的文件,当我编译项目时,会有一些未提交的更改,这些更改是/ target文件夹中的一些.class文件。

Why is this happening ? 为什么会这样? I also tried to change the .gitignore entries to 我还尝试将.gitignore条目更改为

/backend/Evaluation/logs/** /后端/评估/日志/ **

but it did not work too. 但它也没有用。

Any ideas ? 有任何想法吗 ?

Because there is the Sourcetree tag in the question I will answer you how to do this with Sourcetree , it's really simple. 因为问题中有Sourcetree标签,我会回答你如何使用Sourcetree做到这一点 ,它非常简单。

As said before you first have to remove the file from tracking and then make Sourcetree to ignore the file. 如前所述,首先必须从跟踪中删除文件,然后使Sourcetree忽略该文件。

  1. Change something in the file so that it is shown to you or select it from the "file status" tab at the bottom of the open repository. 更改文件中的某些内容,以便向您显示或从打开的存储库底部的“文件状态”选项卡中选择它。
  2. Right click on the file and you see "Ignore...", but it is grayed out because as said above its already in track. 右键单击该文件,您会看到“忽略...”,但它显示为灰色,因为如上所述它已经在轨道中。
  3. Right click on the file and chose "Stop Tracking" 右键单击该文件,然后选择“停止跟踪”
  4. The file will be shown with a red button which indicates it will be removed (from tracking) 该文件将显示一个红色按钮,表示它将被删除(从跟踪)
  5. A new entry of the same file will be shown in "file status" with a blue question mark indicates a new file (new because you said remove from tracking in 4) 同一文件的新条目将显示在“文件状态”中,蓝色问号表示新文件(新文件,因为您说从4中删除了跟踪)
  6. Right click on the file from 5 and now you see the "Ignore..." is able to choose. 右键单击5中的文件,现在您可以选择“忽略...”。 Click on it and Sourcetree will put a new entry for the file in the .gitignore file 单击它,Sourcetree将在.gitignore文件中为该文件添加一个新条目
  7. You can see the .gitignore file if you click on "Setting" on the top right, choose "advanced" and then the first entry is about the ignore file, click on "edit" and it will be open. 如果单击右上角的“设置”,选择“高级”,然后第一个条目是关于忽略文件,单击“编辑”,它将打开,您可以看到.gitignore文件。

Let's say we have a file that was inadvertently added to our repository: 假设我们有一个无意中添加到我们的存储库的文件:

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

At some point, we realize that the file is unimportant so we add it to our .gitignore file: 在某些时候,我们意识到该文件并不重要,因此我们将其添加到我们的.gitignore文件中:

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

Later on, we make some changes to that file: 稍后,我们对该文件进行了一些更改:

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

And of course, even though the file is listed in .gitignore , we have already told git that we want to track it, so git status shows: 当然,即使文件列在.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")

We need to remove this file from the repository (without removing the file from our work tree). 我们需要从存储库中删除此文件(不从我们的工作树中删除该文件)。 We can do this with the --cached flag to git rm : 我们可以使用--cached标志来执行此操作来git rm

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

This stages the delete operation... 这将分阶段delete操作......

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

...so we need to commit it: ......所以我们需要承诺:

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

Now if we run git status git will ignore this file: 现在,如果我们运行git status git将忽略此文件:

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

Even though it's still here in our working tree: 即使它仍在我们的工作树中:

stackoverflow$ cat junkfile 
this file is not important

I had a problem with bin folder being tracked (entire folder). 我在跟踪bin文件夹时遇到问题(整个文件夹)。

So here are the quick steps (more visual, as I'm more of a visual type of person): 所以这里是快速步骤(更直观,因为我更像是一个视觉类型的人):

  1. Just for security purposes, I zipped entire bin folder 出于安全考虑,我压缩了整个bin文件夹
  2. Move it to desktop 将其移至桌面
  3. Move (current) .gitignore to the desktop as well 将(当前).gitignore移动到桌面
  4. Delete entire BIN folder 删除整个BIN文件夹
  5. Commit changes (via favourite git commit tool) 提交更改(通过最喜欢的git提交工具)
  6. Commit, push, done! 提交,推送,完成!
  7. Go back to the basic project's folder 返回基本项目的文件夹
  8. Move back the .gitignore file 移回.gitignore文件
  9. Optional - move back (unzip) the bin folder as well. 可选 - 也可以向后移动(解压缩)bin文件夹。
  10. As a result, you will see that the git tool is no longer tracking changes from the bin folder. 因此,您将看到git工具不再跟踪bin文件夹中的更改。

I hope this helps someone. 我希望这可以帮助别人。

If the files have already been added or committed to your Git repository, you must first stop tracking them before they will be ignored by .gitIgnore . 如果文件已经添加或提交到您的Git存储库,则必须先停止跟踪它们,然后才能被.gitIgnore忽略。

To stop tracking files in SourceTree: 要停止在SourceTree中跟踪文件:

Right-click on files. 右键单击文件。 Choose "Stop Tracking". 选择“停止跟踪”。
( this won't delete your files, it just stops tracking them ) 这不会删除你的文件,只是停止跟踪它们

To stop tracking files from Command-line: 要停止从命令行跟踪文件:

git rm --cached example.txt

In addition to the answers already provided above, also note that the .gitignore file will not work if it is not in the root folder. 除了上面已经提供的答案之外,还要注意.gitignore文件如果不在根文件夹中则不起作用。

I had a project where .gitignore was here /projectname/.gitignore and therefore was not being read. 我有一个项目.gitignore在这里/projectname/.gitignore ,因此没有被阅读。 So I moved it to /.gitignore and all was well again. 所以我把它移到了/.gitignore ,一切都很顺利。

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

这对我有用:

git update-index --assume-unchanged some.file

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM