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/
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.
Why is this happening ? I also tried to change the .gitignore entries to
/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.
As said before you first have to remove the file from tracking and then make Sourcetree to ignore the file.
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:
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:
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
:
stackoverflow$ git rm --cached junkfile
rm 'junkfile'
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
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
I had a problem with bin folder being tracked (entire folder).
So here are the quick steps (more visual, as I'm more of a visual type of person):
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 .
To stop tracking files in 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.
I had a project where .gitignore
was here /projectname/.gitignore
and therefore was not being read. So I moved it to /.gitignore
and all was well again.
我发现通过删除存储库,然后在重新创建存储库时将.gitignore文件放在文件夹中,将忽略忽略文件。
这对我有用:
git update-index --assume-unchanged some.file
苦苦挣扎几个小时,尝试了很多东西,包括这里的建议 - 无济于事......最终对我有用的是在bitbucket.org中设置repo时创建.gitignore文件 ,将该文件拉到我的本地,粘贴了我试图自己创建的文件的完全相同的内容,现在终于全部工作了。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.