90

I have a Bitbucket Git repository managed with Sourcetree.

I have two folders that I want to commit, but I need to ignore all the files in these folders, because they contain only temporary files.

How can I do that?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rubdottocom
  • 8,110
  • 10
  • 39
  • 59

10 Answers10

160

For Sourcetree users: If you want to ignore a specific folder, just select a file from this folder, right-click on it and do "Ignore...". You will have a pop-up menu where you can ignore "Ignore everything beneath: <YOUR UNWANTED FOLDER>"

First menu

Second menu

If you have the "Ignore" option greyed out, you have to select the "Stop Tracking" option. After that the file will be added to Staged files with a minus sign on red background icon and the file's icon in Unstaged files list will change to a question sign on a violet background. Now in Unstaged files list, the "Ignore" option is enabled again. Just do as described above.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kalzem
  • 7,320
  • 6
  • 54
  • 79
54

Add this to .gitignore:

*
!.gitignore
Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
  • 13
    This should be added inside each folder you want to track. The existence of this file itself will make git track the folder (by tracking this file). The `*` means ignore all files, and the `!.gitignore` means don't ignore the file itself – Billy Moon Mar 12 '12 at 10:36
  • 2
    As Billy Moon explained, thanks. So you can create .gitignore files in directories. This file tells Git what to ignore or what not to. – Gergo Erdosi Mar 12 '12 at 10:40
  • 21
    How do you do this in SourceTree? – 1.21 gigawatts Oct 12 '12 at 19:19
  • 1
    You don't need SourceTree for this, `.gitginore` is a regular file, just create it in the directory which content you want to ignore. – Gergo Erdosi May 23 '14 at 06:04
  • HI, How to do this if we have a global gitignore file for the project. – vigamage Oct 11 '16 at 04:50
  • 1
    As mentioned by @AD7six below, do _not_ add !.gitignore to the file as it is unnecessary. – Xcalibur Nov 07 '16 at 02:08
26

Right click on a file → IgnoreIgnore everything beneath.

If the Ignore option is grayed out then:

  1. Stage a file first
  2. Right click → Stop Tracking
  3. That file will appear in the pane below with a question mark → Right click on it→ Ignore

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrei Karcheuski
  • 3,116
  • 3
  • 38
  • 39
7

After beating my head on this for at least an hour, I offer this answer to try to expand on the comments some others have made. To ignore a folder/directory, do the following: if you don't have a .gitignore file in the root of your project (that name exactly ".gitignore"), create a dummy text file in the folder you want to exclude. Inside of Source Tree, right click on it and select Ignore. You'll get a popup that looks like this.

enter image description here

Select "Everything underneath" and select the folder you want to ignore from the drop-down list. This will create a .gitignore file in your root directory and put the folder specification in it.

If you have a .gitignore file already in your root folder, you could follow the same approach above, or you can just edit the .gitignore file and add the folder you want to exclude. It's just a text file. Note that it uses forward slashes in path names rather than backslashes, as we Windows users are accustomed to. I tried creating a .gitignore text file by hand in Windows Explorer, but it didn't let me create a file without a name (i.e. with only the extension).

Note that adding the .gitignore and the file specification will have no effect on files that are already being tracked. If you're already tracking these, you'll have to stop tracking them. (Right-click on the folder or file and select Stop Tracking.) You'll then see them change from having a green/clean or amber/changed icon to a red/removed icon. On your next commit the files will be removed from the repository and thereafter appear with a blue/ignored icon. Another contributor asked why Ignore was disabled for particular files and I believe it was because he was trying to ignore a file that was already being tracked. You can only ignore a file that has a blue question mark icon.

Oriol Roma
  • 329
  • 1
  • 5
  • 9
Karl Hoaglund
  • 603
  • 10
  • 10
  • To create .gitignore file in Widnows, use the Git bash command 'touch .gitignore' – Mikee Oct 03 '18 at 12:39
  • Here's a hint. The answer says... "If you're already tracking these, you'll have to stop tracking them. (Right-click on the folder or file and select Stop Tracking.)"... but how does one find the folder in SourceTree in the first place in order to right click on it? Answer: At the top left of the Staged Files list, there is a dropdown that says: "Pending Files...". Select that and choose "All Files". Then you can select the folder/files in order to stop tracking them. – Ph0t0n Jun 21 '23 at 18:56
7

As far as I know, Git doesn't track folders, only files - so empty folders (or folders where all files are ignored) cannot be committed. If you e.g. need the folder to be present due to some step in your build process, perhaps you can have your build tool create it instead? Or you could put one empty, unignored file in the folder and commit it.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
  • 1
    You mean that if I want to track the folder I have to add a "dummy" file and ignore the rest? :-/ – rubdottocom Mar 12 '12 at 10:36
  • 2
    @rubdottocom: I believe so; folders exist in git only because of the files that are in them. Why do you need to have an empty folder version controlled? – Aasmund Eldhuset Mar 12 '12 at 10:38
  • 2
    There are many reasons for tracking empty folders. You could want that a checkout has the folder ready for people to use, without having to go through creating them individually. Your code might depend on the existence of a certain folder, for example a folder which is expecting uploads, and if the folder does not exist, it fails to upload the files there etc... – Billy Moon Mar 12 '12 at 10:43
  • For the reason explained by @AasmundEldhuset, the answer given by Gergo has the right solution. – Billy Moon Mar 12 '12 at 10:44
  • @BillyMoon: I agree that there might be reasons; I was wondering about what _his_ particular needs were, so that we could suggest the most suitable solution. :-) Gergo's trick was neat, although it amounts to adding a (more or less) dummy file to the folder, as I suggested. His solution was more git-ish, though, so +1 to him. – Aasmund Eldhuset Mar 12 '12 at 10:52
  • Hi, yes, the folders that i want to track are a /tmp folder for temp files created during a process execution and a /uploads folder that is needed for the application but I don't want to track test uploaded files. As Billy Moon said, I want a ready to use project. Obviouly I should take care about create a folder that I need but it doesn't exists yet. – rubdottocom Mar 12 '12 at 11:28
4

If your Ignore option is grayed out, you have to stop tracking the file before you can ignore it. You can simply right click on the file and hit "Stop Tracking".

A blue icon should appear next to it. Right click on it again and click ignore.

Sergey
  • 1,608
  • 1
  • 27
  • 40
LeRoy
  • 4,189
  • 2
  • 35
  • 46
4

As mentioned by others: git doesn't track folders, only files.

You can ensure a folder exists with these commands (or equivalent logic):

echo "*" > keepthisfolder/.gitignore
git add --force keepthisfolder/.gitignore
git commit -m "adding git ignore file to keepthisfolder"

The existence of the file will mean anyone checking out the repository will have the folder.

The contents of the gitignore file will mean anything in it are ignored

You do not need to not-ignore the .gitignore file itself. It is a rule which would serve no purpose at all once committed.

OR

if you prefer to keep all your ignore definitions in the same place, you can create .gitignore in the root of your project with contents like so:

*.tmp # example, ignore all .tmp files in any folder
path/to/keepthisfolder
path/to/keepthatfolder

and to ensure the folders exist

touch path/to/keepthisfolder/anything
git add --force path/to/keepthisfolder/anything
git commit -m "ensure keepthisfolder exists when checked out"

"anything" can literally be anything. Common used names are .gitignore, .gitkeep, empty.

AD7six
  • 63,116
  • 12
  • 91
  • 123
3

Ignore full folder on source tree.

   Just Open Repository >Repository setting > Edit git ignore File and 
   you can rite some thing like this :

 *.pdb 
*.bak 
*.dll
*.lib 
.gitignore
packages/ 
*/bin/
*/obj/ 

For bin folder and obj folder just write : */bin/ */obj/

Rajesh Kumar
  • 602
  • 6
  • 20
0
  • Ignore all files in folder with Git in Sourcetree:

Ignore all files in folder with Git in Sourcetree

Community
  • 1
  • 1
Yatko
  • 8,715
  • 9
  • 40
  • 46
-1

In Sourcetree: Just ignore a file in specified folder. Sourcetree will ask if you like to ignore all files in that folder. It's perfect!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AVEbrahimi
  • 17,993
  • 23
  • 107
  • 210