.gitignore file in Sourcetree not working
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.
- 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.
- Right click on the file and you see "Ignore...", but it is grayed out because as said above its already in track.
- Right click on the file and chose "Stop Tracking"
- The file will be shown with a red button which indicates it will be removed (from tracking)
- 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)
- 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
- 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.
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