Sourcetree 中的 .gitignore 文件不起作用 [英] .gitignore file in Sourcetree not working

查看:36
本文介绍了Sourcetree 中的 .gitignore 文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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

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

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.

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

Why is this happening ? I also tried to change the .gitignore entries to

/backend/Evaluation/logs/**

/backend/Evaluation/logs/**

但它也不起作用.

有什么想法吗?

推荐答案

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

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'

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

At some point, we realize that the file is unimportant so we add it to our .gitignore file:

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 

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

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:

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 来做到这一点:

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:

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

这将执行 delete 操作...

This stages the delete operation...

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

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

Now if we run git status git will ignore this file:

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

这篇关于Sourcetree 中的 .gitignore 文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆