72

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 ?

Ibrahim Dauda
  • 607
  • 8
  • 18
SteveSt
  • 1,127
  • 2
  • 13
  • 21
  • With a single trailing splat? `backend/Evaluation/logs/*` – Lindsay Winkler Feb 06 '14 at 23:49
  • 9
    Also note that Git will not ignore files *that have already been added to your repository*. If git is alerting you about uncommitted changes to these files, you have previously added them to your repository. – larsks Feb 07 '14 at 00:06
  • @larsks , so how can I remove these files from my repository , or the whole folder also . – SteveSt Feb 07 '14 at 09:53

8 Answers8

134

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.

  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.
mfgmicha
  • 3,404
  • 1
  • 22
  • 25
  • 8
    Well answered for SourceTree. Not everyone uses command line. – Ibrahim Dauda Dec 27 '15 at 09:40
  • 10
    Note that step 5 won't work as described if you manually tried to add the files to your .gitignore file before you clicked 'Stop tracking'. The files will not show up in your repository since they are correctly ignored. – user63457 Dec 07 '16 at 09:57
  • how can i do this for a folder in sourcetree ? – Vikram Aug 05 '17 at 02:08
100

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
pal4life
  • 2,680
  • 4
  • 29
  • 53
larsks
  • 194,279
  • 34
  • 297
  • 301
11

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):

  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.

I hope this helps someone.

ESala
  • 5,973
  • 3
  • 30
  • 50
Hrvoje
  • 170
  • 1
  • 7
  • 1
    this is the solution I needed, using repository hosted with visual studio online. Had to delete folder, update gitignore and commit and then add folder back. Then the obj and bin folders were ignored. thanks – J King Aug 04 '16 at 19:58
  • 1
    This is the exact fix i needed. This should also be the accepted answer. Bravo ! @Hrvoje – Sathish Prabhakaran Jun 23 '17 at 12:26
11

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
tim-montague
  • 12,048
  • 2
  • 50
  • 40
2

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.

Ibrahim Dauda
  • 607
  • 8
  • 18
  • 1
    I am also seeing this, but isn't this a bug in Sourcetree? It seems that git spec ( https://git-scm.com/docs/gitignore ) allows .gitignore's outside of root? – astgtciv Feb 09 '17 at 10:23
2

This worked for me:

git update-index --assume-unchanged some.file
Stephen Rauch
  • 40,722
  • 30
  • 82
  • 105
Michael
  • 31
  • 3
0

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.

SingleStepper
  • 351
  • 1
  • 10
0

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.

ByteMyPixel
  • 142
  • 2
  • 3
  • 14