git Sourcetree 中的 .gitignore 文件不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21616463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:42:18  来源:igfitidea点击:

.gitignore file in Sourcetree not working

gitatlassian-sourcetreesourcetree

提问by SteveSt

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 ?

有任何想法吗 ?

回答by mfgmicha

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)
  6. Right click on the file from 5 and now you see the "Ignore..." is able to choose. Click on it and Sourcetree will put a new entry for the file in the .gitignore file
  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.
  1. 更改文件中的某些内容,以便向您显示或从打开的存储库底部的“文件状态”选项卡中选择它。
  2. 右键单击该文件,您会看到“忽略...”,但它是灰色的,因为如上所述它已经在跟踪中。
  3. 右键单击该文件并选择“停止跟踪”
  4. 该文件将显示一个红色按钮,表示它将被删除(从跟踪)
  5. 同一文件的新条目将显示在“文件状态”中,带有蓝色问号表示新文件(新的,因为您在 4 中说从跟踪中删除)
  6. 右键单击 5 中的文件,现在您可以看到“忽略...”可以选择。单击它,Sourcetree 将在 .gitignore 文件中为该文件添加一个新条目
  7. 你可以看到.gitignore文件,如果你点击右上角的“设置”,选择“高级”,然后第一个条目是关于忽略文件的,点击“编辑”,它将被打开。

回答by larsks

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 .gitignorefile:

在某些时候,我们意识到该文件并不重要,因此我们将其添加到我们的.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 statusshows:

当然,即使文件在 中.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 --cachedflag to git rm:

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

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

This stages the deleteoperation...

这将分阶段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 statusgit will ignore this file:

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

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

回答by Hrvoje

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

I hope this helps someone.

我希望这可以帮助别人。

回答by tfmontague

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

回答by Ibrahim Dauda

In addition to the answers already provided above, also note that the .gitignorefile will not work if it is not in the root folder.

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

I had a project where .gitignorewas here /projectname/.gitignoreand therefore was not being read. So I moved it to /.gitignoreand all was well again.

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

回答by Michael

This worked for me:

这对我有用:

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

回答by ByteMyPixel

Struggled with this for a few hours, tried a number of things including the suggestions here - to no avail... what finally worked for me was to create the .gitignore file when setting up the repo within bitbucket.org, pulled that file to my local, pasted the exact same contents of the file I was trying to create on my own, and it is finally all working now.

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

回答by SingleStepper

I found that by deleting the repository, then having the .gitignore file in the folder when the repository is recreated the ignore file is respected.

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