Exceptions in .gitignore

GitGitignore

Git Problem Overview


How can I add an exception to .gitignore, like "ignore all the .dll files BUT myfile.dll"?

Git Solutions


Solution 1 - Git

Use ! to negate the pattern:

*.dll
!myfile.dll

Solution 2 - Git

If you want to ignore whole folder, except some specific files, then write:

MyFolder/*
!MyFolder/CoolFile.txt

This won't work:

MyFolder/
!MyFolder/CoolFile.txt

Solution 3 - Git

You can also ignore folders like

!src/main/resources/archetype-resources/**/*

you can also ignore nested folder with patterns like

!/src/test/resources//*

For quick creation of .gitignore file try gitignore.io

Solution 4 - Git

You can have several .gitignore files working together in a hierarchical manner to achieve your goal. At the root level you may have:

root

*.dll

inside the folder having the myfile.dll you can add another .gitignore file like so:

root/lib/folderwithMyFiledll

!myfile.dll

more info here

> An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. Put a backslash ("") in front of the first "!" for patterns that begin with a literal "!", for example, "!important!.txt". It is possible to re-include a file if a parent directory of that file is excluded if certain conditions are met. See section NOTES for detail.

Solution 5 - Git

I did this because I have a folder called /modules that I want to ignore, except everything in the /modules/custom folder. This worked for me. Now my custom modules will get synced to GitHub.

/modules/*
!/modules/custom/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpistacchioView Question on Stackoverflow
Solution 1 - GitBen JamesView Answer on Stackoverflow
Solution 2 - Gitcubuspl42View Answer on Stackoverflow
Solution 3 - GitAnver SadhatView Answer on Stackoverflow
Solution 4 - GitkamaydView Answer on Stackoverflow
Solution 5 - GitRobbiegodView Answer on Stackoverflow