gitignore include a specific path - gitignore

In my .gitignore I have the line:
/storage/*.key
But I don't want to ignore this specific path and all it's content:
/storage/app/public/*
Now, inside that path there's another .gitinore file and it has:
*
!.gitignore
And in the same path there's another .gitignore with:
*
!public/
!.gitignore
I thought that would be for add the path but is not working.
What is that? How can do what I want (add the path of storage/app/public*)?

Simply check which exact .gitignore and which exact rule is ignoring public:
cd /path/to/my/repo
# from the root folder of the repo:
git check-ignore -v /storage/app/public/aFileWithinPublic
You need to test a file, since a folder is not tracked by Git.
The rule is simple:
It is not possible to re-include a file if a parent directory of that file is excluded.
So if the folder public is ignored (by a '*' rule in a parent .gitignore) of if any of the parent folder of public is ignored, no amount of exclusion will be able to make public content not-ignored.
So this is good (from the .gitignore in storage/app/, parent of public/ folder):
*
!public/
But if storage/app/ or storage/ are ignored themselves, this exclusion would be ignored as well.
That is why git check-ignore -v /storage/app/public/aFileWithinPublic is important: it will explain what causes the folder content to still be ignored.

Related

gitignore > some ignored files still added to the repository

I don't understand why ignored files are still added to the repository. Are there some circumstances when files which are in the .gitignore list, can be still added to the repository
I added directory to the .gitignore before I did anything with the project (project is clean, no trucked or untracked files). Then I built the project. During the build process some files were modified. When the build was done, most of the modified files were ignored, but some were added to the repository. I don't understand this git's selectivity.
Usually, files are added to the index when modified if they were tracked before the .gitignore referenced them.
Try:
git rm --cached -r biz/modules/rest-apis/docs/refAddress
Then control that all files are ignored, by selecting one which was added before:
git check-ignore -v -- biz/modules/rest-apis/docs/refAddress/GET_1.0_v4_addresses.json
You should see a .gitignore rule (no output means: the file is not ignored).

Ignore a specific file in resource directory

This is my .gitignore file:
HELP.md
tmp/
target/
!**/src/main/**
!**/src/test/**
How can I exclude only this file:
src/main/resources/application.properties
Thank you
It should be fairly easy:
HELP.md
tmp/
target/
!**/src/main/**
!**/src/test/**
src/main/resources/application.properties
should work.
In case it doesn't, as it appears to be your case1, git allows you to ignore files, but remember this is a local setting and it will not be pushed to any other repository: go to .git/info/exclude and edit the file. It works like .gitignore.
Also remember: .gitignore has an higher priority on your info/exclude (source).
1. this might be due to the fact that you have multiple .gitignore files in your directory, and thus there is overmatching of patterns.

How to use gitignore with asterisks? [duplicate]

I have a directory structure like this:
.git/
.gitignore
main/
...
tools/
...
...
Inside main and tools, and any other directory, at any level, there can be a 'bin' directory, which I want to ignore (and I want to ignore everything under it too). I've tried each of these patterns in .gitignore but none of them work:
/**/bin/**/*
/./**/bin/**/*
./**/bin/**/*
**/bin/**/*
*/bin/**/*
bin/**/*
/**/bin/* #and the others with just * at the end too
Can anyone help me out? The first pattern (the one I think should be working) works just fine if I do this:
/main/**/bin/**/*
But I don't want to have an entry for every top-level directory and I don't want to have to modify .gitignore every time I add a new one.
This is on Windows using the latest msysgit.
EDIT: one more thing, there are files and directories that have the substring 'bin' in their names, I don't want those to be ignored :)
Before version 1.8.2, ** didn't have any special meaning in the .gitignore. As of 1.8.2 git supports ** to mean zero or more sub-directories (see release notes).
The way to ignore all directories called bin anywhere below the current level in a directory tree is with a .gitignore file with the pattern:
bin/
In the man page, there an example of ignoring a directory called foo using an analogous pattern.
Edit:
If you already have any bin folders in your git index which you no longer wish to track then you need to remove them explicitly. Git won't stop tracking paths that are already being tracked just because they now match a new .gitignore pattern. Execute a folder remove (rm) from index only (--cached) recursivelly (-r). Command line example for root bin folder:
git rm -r --cached bin
The .gitignore of your dream seems to be:
bin/
on the top level.
I think it is worth to mention for git beginners:
If you already have a file checked in, and you want to ignore it, Git
will not ignore the file if you add a rule later. In those cases, you
must untrack the file first, by running the following command in your
terminal:
git rm --cached
So if you want add to ignore some directories in your local repository (which already exist) after editing .gitignore you want to run this on your root dir
git rm --cached -r .
git add .
It will basically 'refresh' your local repo and unstage ignored files.
See:
http://git-scm.com/docs/git-rm,
https://help.github.com/articles/ignoring-files/
The ** never properly worked before, but since git 1.8.2 (March, 8th 2013), it seems to be explicitly mentioned and supported:
The patterns in .gitignore and .gitattributes files can have **/, as a pattern that matches 0 or more levels of subdirectory.
E.g. "foo/**/bar" matches "bar" in "foo" itself or in a subdirectory of "foo".
In your case, that means this line might now be supported:
/main/**/bin/
[Bb]in/
matches both upper and lower case
I didn't see it mentioned here, but this appears to be case sensitive. Once I changed to /Bin the files were ignored as expected.
Step 1: Add following content to the file .gitignore.
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
# Visual Studio 2015 cache/options directory
.vs/
Step 2: Make sure take effect
If the issue still exists, that's because settings in .gitignore can only ignore files that were originally not tracked. If some files have already been included in the version control system, then modifying .gitignore is invalid.
To solve this issue completely, you need to open Git Bash or Package Manager Console (see screenshot below) to run following commands in the repository root folder.
git rm -r --cached .
git add .
git commit -m "Update .gitignore"
Then the issue will be completely solved.
[Bb]in will solve the problem, but...
Here a more extensive list of things you should ignore (sample list by GitExtension):
#ignore thumbnails created by windows
Thumbs.db
#Ignore files build by Visual Studio
*.user
*.aps
*.pch
*.vspscc
*_i.c
*_p.c
*.ncb
*.suo
*.bak
*.cache
*.ilk
*.log
[Bb]in
[Dd]ebug*/
*.sbr
obj/
[Rr]elease*/
_ReSharper*/
If you're looking for a great global .gitignore file for any Visual Studio ( .NET ) solution - I recommend you to use this one: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
AFAIK it has the most comprehensive .gitignore for .NET projects.
Literally none of the answers actually worked for me; the only one that worked for me was (on Linux):
**/bin
(yes without the / in the end)
git version 2.18.0
for 2.13.3 and onwards,writing just bin in your .gitignore file should ignore the bin and all its subdirectories and files
bin
As a notice;
If you think about .gitignore does not work in a way (so added foo/* folder in it but git status still showing that folder content(s) as modified or something like this), then you can use this command;
git checkout -- foo/*
Adding **/bin/ to the .gitignore file did the trick for me (Note: bin folder wasn't added to index).
In addition to #CB Bailey's answer:
I tried to remove multiple folders (in subfolders) named et-cache (caching folder from Wordpress divi theme) from the index and from being tracked.
I added
et-cache/
to the .gitignore file. But
git rm -r --cached et-cache
resulted in an error:
fatal: pathspec 'et-cache' did not match any files
So the solution was to use powershell:
Get-ChildItem et-cache -Recurse |% {git rm -r --cached $_.FullName}
This searches for all subfolders named et-cache. Each of the folders path (fullname) is then used to remove it from tracking in git.
If the pattern inside .gitignore ends with a slash /, it will only find a match with a directory.
In other words, bin/ will match a directory bin and paths underneath it, but will not match a regular file or a symbolic link bin.
If the pattern does not contain a slash, like in bin Git treats it as a shell glob pattern (greedy). So best would be to use simple /bin.
bin would not be the best solution for this particular problem.
In my case encoding of gitignore file was problematic, check if it is UTF-8

gitignore misses some binary files (DLLs & PEs)

I use the latest version of Github Desktop. My repo consist of a rather large C# solution with many sub-directories and projects. I'd like to ignore all R#-cache files and compiled binaries using the .gitignore file which resides in the root directory of the local repo directory. There are no other gitignore's anywhere in this repo and none in any parent directories. My current gitignore is this:
*.suo
*.user
_ReSharper.*
bin
obj
packages
*.cache
*.pdb
*.dll
*.exe
*.xml
When I made my changes, recompiled and tested everything, I open Github Desktop. It catches almost all files that should be ignored, only some .dlls, .pdbs and .exes are not ignored and always show up as changed:
Now, there are way more binary files in this repo. Only the specific ones in the screengrab are missed.
Is this fixable, and/or can the gitignore be altered to catch all files that it should catch?
Here's what I tried:
Removed and re-cloned the repository
Removed and manually re-created the gitignore
Right-click->Ignore by file extension from within the GitHub Desktop client. This does not work, worse, it creates duplicate masks in the gitignore
Checked for conflicting gitignore's in directories accessible by Github Desktop
Maybe you have files that were already being tracked by git before you modified the .gitignore? Those files (at least in git 1.9.1) aren't ignored when added to a .gitignore.
For example, I created a "test" directory at the root file one of my repos and put a file in it:
mkdir test
echo "abc" > test/x.txt
I added, committed and pushed it. Then I edited .gitignore at the root and added this line:
x.txt
I also made an edit to test/x.txt AND added a new file:
echo "123" >> test/x.txt
mkdir test2
echo "xyz" > test2/x.txt
Now when I run "git status" I see:
modified: .gitignore
modified: test/x.txt
So test2/x.txt is being properly ignored, but test/x.txt is still being tracked.
To force the file to be ignored, you can delete it, add & commit the deletion together with the .gitignore change, then restore the file.

Removing file from .gitignore in webstorm

I was searching the web for hour now...
I can't seem to find where the gitignore file is located,and it's ruining my life. I have to remove one file from there. I'm using WebStorm 8
Any help?
.gitignore can be in any folder inside your git repository. And the patterns in .gitignore of child folder will overwrite the ones in parent folder.
It may be hidden when you use ls in your Git Bash. Just use ls -a.
From the doc:
Patterns read from a .gitignore file in the same directory as the
path, or in any parent directory, with patterns in the higher level
files (up to the toplevel of the work tree) being overridden by those
in lower level files down to the directory containing the file. These
patterns match relative to the location of the .gitignore file.