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