1

I'm using Github Desktop on Windows 10. I have a .gitignore file with which I'd like to ignore everything in the directory, including all subdirectories.

What's frustrating is that most files are excluded, but I'm still getting a few random files that I cannot seem to ignore.

I have a directory, say, My Dir/Sub-dir, I want to ignore. I also want to ignore all files of extension, say, *.swf. Thus, I write this .gitignore file:

My Dir/Sub-dir

*.swf

But, when I go back to Github Desktop, I still get two files similar to the following in the list:

My Dir/Sub-dir/anotherdir/randomfile.xml
My Dir/Sub-dir/animation.swf

What's going on? Is this a bug? Or am I missing something?

EDIT:

Other alternative .gitignores I've tried are:

My*Dir/Sub-dir
*.swf

 

My\ Dir/Sub-dir
*.swf

 

My Dir/Sub-dir/
*.swf

 

/My Dir/Sub-dir
*.swf

EDIT:

So, I've tried the git rm --cached <file> command on my files, and it worked - until one of the files changes again. Github Desktop then once again says they need to be updated.

p.s. It may be that they somehow got indexed in the master branch, as I'm currently in a different branch. Would this cause it? And, if so, how would I eliminate these files from all branches?

Codesmith
  • 5,779
  • 5
  • 38
  • 50

2 Answers2

1

To ignore all trees under specific directories, append the trailing slash to the directory:

# Ignore directory at all levels, even if the directory is nested under other directories
My Dir/Sub-dir/
# Ignore the directory only if it exists at the root of your repository
/My Dir/Sub-dir/
# Ignore the sub-directory, no matter where it appears within your repository
Sub-dir/
# Ignore all swf files
*.swf

If you are continuing to experience difficulties, your problem may be that the files you wish to ignore are already indexed by git (via the git add command). If the files are not committed yet, you can remove them from the index with git reset -- 'My Dir/Sub-dir/'. If your files have been committed, you can remove them from the index with git rm --cached <file>.

Mike Hill
  • 3,622
  • 23
  • 27
  • 1
    That did it! It just got cached. So, having found my git install from GitHub Desktop (https://stackoverflow.com/questions/34565238/where-does-github-desktop-install-command-line-version-of-git#answer-46966548) I was able to remove it from the index. – Codesmith Oct 19 '18 at 15:16
  • Sorry, I *thought* it worked.. Check out my second edit. Maybe you'll have an idea. – Codesmith Oct 19 '18 at 16:30
  • @Codesmith after removing it from the index (via `git rm --cached`) did you commit the removal? – Mike Hill Oct 19 '18 at 21:41
  • To clarify, if there is a file committed to the repo that you want to remove (without removing your local copy) you can use `git rm --cached` to stage the deletion of that file from the repo. You will still need to commit that change in order for the file to actually get deleted from the repo, and until this commit is made you may see your env (GitHub Desktop) re-add it automatically when changes are made. On other hand, if the file isn't committed yet then `git reset -- ` will do the trick. – Mike Hill Oct 19 '18 at 21:48
  • Also just to add as a note to clarify the answer above, `.gitignore` only *prevents* **non-indexed** files from being added to the repo -- it won't do anything to files that are already indexed. – Mike Hill Oct 19 '18 at 21:49
  • Thanks @MikeHill, good to know that about `.gitignore`. So, what's odd is `git rm --cached` appears to work fine (showing the `filename [-]`) *until* I commit it and it changes again, when it thus re-appears as a 'changed' file. Do note, I likely committed these files to the master branch, and I'm currently on a different branch. Could that cause the issue? – Codesmith Oct 19 '18 at 22:47
  • As long as you don't switch from your current branch then that should be fine. Have you checked the changes in the commit (`git show` after the commit) to ensure that the removal was actually committed? And did you also add matching patterns to your `.gitignore`? The files will keep getting re-added if a suitable pattern does not exist there. You will want to commit the `.gitignore` changes for the future, too. – Mike Hill Oct 20 '18 at 23:48
0

I guess the whitespace could be a problem. Try

My\ Dir/Sub-dir

or

My*Dir/Sub-dir

This could be a good reference .gitignore entire directory with whitespace in name

FreshD
  • 2,914
  • 2
  • 23
  • 34
  • Yes, that is a good point. And I had found that suggestion online and tried it. Unfortunately, it still didn't work... :-/ I'll edit my question. – Codesmith Oct 18 '18 at 21:36