91

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?

10 Answers 10

162

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.

Sign up to request clarification or add additional context in comments.

8 Comments

If the folder is already committed, delete it first, commit the changes, then follow the above steps.
I have "Ignore" option grayed out. Does anybody know why?
Ignore is also greyed out for me.
@JoeLloyd yes it does add to git ignore. You can even commit the created/edited git ignore file like any other file.
community.atlassian.com/t5/Sourcetree-questions/… First you need to stop tracking the file before you can ignore it.
|
54

Add this to .gitignore:

* !.gitignore 

6 Comments

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
As Billy Moon explained, thanks. So you can create .gitignore files in directories. This file tells Git what to ignore or what not to.
How do you do this in SourceTree?
You don't need SourceTree for this, .gitginore is a regular file, just create it in the directory which content you want to ignore.
As mentioned by @AD7six below, do not add !.gitignore to the file as it is unnecessary.
|
27

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

1 Comment

After "Stop Tracking", the option "Ignore..." is still greyed out in my case.
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.

6 Comments

You mean that if I want to track the folder I have to add a "dummy" file and ignore the rest? :-/
@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?
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...
For the reason explained by @AasmundEldhuset, the answer given by Gergo has the right solution.
@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.
|
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.

2 Comments

To create .gitignore file in Widnows, use the Git bash command 'touch .gitignore'
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.
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.

Comments

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.

Comments

4

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/

Comments

0
  • Ignore all files in folder with Git in Sourcetree:

Ignore all files in folder with Git in Sourcetree

Comments

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

1 Comment

-1 A pointless answer. Since you do not explain how to do the ignore (in sourcetree)!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.