2

Say I have a folder

/project1/superlong/path/to/the/folder/I/want/to/exclude/in/my/gitignore/file/

and I have that same folder in several projects, but each project have a different first folder in that superlong path.

Let me show you what I'm talking about: /project1/superlong/path/to/the/folder/I/want/to/exclude/in/my/gitignore/file/ /project2/superlong/path/to/the/folder/I/want/to/exclude/in/my/gitignore/file/ /project3/superlong/path/to/the/folder/I/want/to/exclude/in/my/gitignore/file/

etc

So in my .gitignore I basically want a singel simple line, like this - with only the two last folder that I know will always be unique in the entire folder structure:

gitignore/file/

I've tried that in my .gitignore file but it doesn't work. Am I missing an asterisk or something? I've consulted the manual pages but that manual is super not intuitive with superbad examples.

Update: Apparently the current version of GitHub for Windows uses an older version of git that lacks support for **. See comments below for more info.

PussInBoots
  • 11,028
  • 9
  • 52
  • 84
  • 1
    `**/gitignore/file/` should work – 1615903 May 21 '13 at 11:56
  • @user1615903: `**/gitignore/file/` did not work for me. – PussInBoots May 21 '13 at 12:00
  • Are the files already added to git? Gitignore does not work for files that are already added. – 1615903 May 21 '13 at 12:01
  • @user1615903: They are not added. Because when I enter the full path in .gitignore and I switch back to Github for Windows the it excludes the files. – PussInBoots May 21 '13 at 12:02
  • Do these folders have any files in them? – levelnis May 21 '13 at 12:14
  • @levelnis: Yes, I have 14 files in this folder. Three of them are .log files and the rest have an autogenerated file extension (date) example: somefile.log.20120410 – PussInBoots May 21 '13 at 12:17
  • I removed everything from .gitignore file except `**/gitignore/file/` so that the other entries don't interfere in any way. Still does not work. – PussInBoots May 21 '13 at 12:19
  • 2
    I haven't read all the answers to this question yet involving `**`, but I would just like to point out that the double asterisk syntax only works in Git version 1.8.2+, so if you're using an earlier version of Git, that syntax won't work. See the [1.8.2 release notes](https://github.com/git/git/blob/master/Documentation/RelNotes/1.8.2.txt). –  May 21 '13 at 14:13

3 Answers3

1

Looking at other posts, it seems as though ** is a bash-specific pattern. If you always have the same depth of folders you could use this:

*/*/*/*/*/*/*/*/*/*/*/*/gitignore/file/

I've tested this in the git source control plugin for VS and it seems to work. It's not pretty though! (I'm basing this on your example of 12 levels of path above /gitignore)

This article states that .gitignore doesn't support ** directly. It advocates placing a .gitignore file into (in your case) each of the gitignore folders, matching file/. Not ideal either but possibly slightly nicer than the pattern above.

Update:

As long as you're only matching on a single folder, this should work, regardless of where it is in the folder structure:

gitignore/
Community
  • 1
  • 1
levelnis
  • 7,665
  • 6
  • 37
  • 61
  • This seems to work but as you mentioned yourself, it is not pretty and it is not what I want. There has to be a more elegant solution without the x number of `/*`? – PussInBoots May 21 '13 at 12:47
  • And also, the number of folder levels could vary from project to project in this particular case. The number of folder levels is also set by different developers who themselves decide how many levels is appropriate for that particular project. – PussInBoots May 21 '13 at 12:52
  • What if the folder `gitignore` exist in some other part of my folder structure (outside of the superlong path from my question) that I do not want excluded? If that is the case then `gitignore/` will exclude this folder structure as well and will therefore not serve as the correct answer. – PussInBoots May 21 '13 at 13:19
  • In that case I would have to question why you would have a folder named `gitignore` in a path that isn't supposed to be ignored? It isn't a great leap to make a team decision to name a folder within a deep structure that needs to be ignored `gitignore` and not use that anywhere else. – levelnis May 21 '13 at 13:25
  • Well say you have two different paths: 1. `/a/b/c/d/e/foo/bar` 2. `/1/2/foo/bar/3/4/5` and I only want to exclude the first path. Won't `bar/` in `.gitignore` exclude both path'? – PussInBoots May 21 '13 at 14:13
  • Or what if I have: 1. `/a/b/c/gitignore` 2. `/1/2/3/gitignore` and I only want to exclude the first path? `gitignore/` will exclude both path'. – PussInBoots May 21 '13 at 14:16
  • @ColdHawaiian What git version comes with Github for Windows? In that case that would explain why ** didn't work for me if Github for Windows comes with some older git version. – PussInBoots May 21 '13 at 14:19
  • I checked `git.exe` in `C:\Users\\AppData\Local\GitHub\PortableGit_93e8418133eb85e81a81e5e19c272776524496c6\bin` and it says: `File version: 1.8.0.0` and `Product version: 1.8.0.msysgit.0` so that means the current `Github for Windows Version 1.0.36.0` does not support the `**`? – PussInBoots May 21 '13 at 14:27
  • @PussInBoots that would seem to be the case. You can also check your Git version with `git --version` at the command line, assuming that your shell/terminal/console uses the same Git exe as GitHub for Windows. –  May 21 '13 at 18:23
  • Had to confirm with GitHub so I sent them an email; here's what they said (relevant parts): "We ship the latest official Git for Windows release (you can see them all at https://code.google.com/p/msysgit/downloads/list?q=full+installer+official+git) with GitHub for Windows. The current version of GitHub for Windows (1.0.47) ships Git 1.8.1.2. When there is an official 1.8.2 release available, we'll update it along with GitHub for Windows. Unfortunately, I don't know how long it will be before there's an official 1.8.2 Git for Windows. I'm sorry I don't have a better answer." – PussInBoots May 21 '13 at 21:36
0

You can use :

**/gitignore/file

And then commit your .gitignore file.

However this post might help you

Community
  • 1
  • 1
cexbrayat
  • 17,772
  • 4
  • 27
  • 22
  • `**/` excludes the files. But doesn't this excludes everything? That's not what I want. – PussInBoots May 21 '13 at 12:07
  • `**/gitignore/file` will exclude andy files in the directory matching the expression. So it should exclude /project1/superlong/path/to/the/folder/I/want/to/exclude/in/my/gitignore/file/ and /a/b/gitignore/file as well. – cexbrayat May 21 '13 at 12:12
  • Nope, does not work. Tried with and without the trailing slash. – PussInBoots May 21 '13 at 12:21
  • Do I need to commit the .gitignore file, sync changes to github and then test the excluded folder for GitHub for Windows to recognize the changes? Is this a Github for Windows thing? GfW detects other changes I make to .gitignore file in real time so it seems strange that it wouldn't like this particular pattern? – PussInBoots May 21 '13 at 12:24
  • Yeah you should commit the .gitignore file with your changes to make them effective, but there is no need to sync with Github. – cexbrayat May 21 '13 at 12:33
  • 1
    No, you do not need to commit the `.gitignore` for it to work. – poke May 21 '13 at 12:38
  • I now have this in my .gitignore file: `**/gitignore/file` and I have commited just the .gitignore file but Github for Windows still shows me the .log files and tells me that they need to be committed. – PussInBoots May 21 '13 at 12:40
  • @poke: I think you are right because whenever I make other changes in .gitignore it immediately reflect the changes in Github for Windows. It seems that it's just `**/gitignore/file` that Github for Windows doesn't like. – PussInBoots May 21 '13 at 12:42
  • If you want to ignore .log files, you should use **/gitignore/file/*.log – cexbrayat May 21 '13 at 13:07
  • For this particular question I want to exclude everything that is in the aforementioned folderpath. The folder contains other files with other file extensions as well. Not just .log files. – PussInBoots May 21 '13 at 13:13
0

At the end of the day it is all about people. And since I always tend to go towards the most intuitive solutions I crown this answer as the winner.

/project1/superlong/path/to/the/folder/I/want/to/exclude/in/my/gitignore/file /project2/superlong/path/to/the/folder/I/want/to/exclude/in/my/gitignore/file /project3/superlong/path/to/the/folder/I/want/to/exclude/in/my/gitignore/file

And if someone comes up with a better, more elegant solution and that works with Github for Windows I would be more than happy to change the accepted answer.

PussInBoots
  • 11,028
  • 9
  • 52
  • 84