Ignoring directories in Git repositories on Windows
Solution 1
Create a file named .gitignore
in your project's directory. Ignore directories by entering the directory name into the file (with a slash appended):
dir_to_ignore/
More information is here.
Solution 2
By default, Windows Explorer will display .gitignore
when in fact the file name is .gitignore.txt
.
Git will not use .gitignore.txt
And you can't rename the file to .gitignore
, because Windows Explorer thinks it's a file of type gitignore without a name.
Non command line solution:
You can rename a file to ".gitignore.", and it will create ".gitignore"
Solution 3
It seems that for ignoring files and directories there are two main ways:
-
.gitignore
- Placing
.gitignore
file into the root of your repository besides the.git
folder (in Windows, make sure you see the true file extension and then make.gitignore.
(with the point at the end to make an empty file extension)) - Making the global configuration
~/.gitignore_global
and runninggit config --global core.excludesfile ~/.gitignore_global
to add this to your Git configuration
Note: files tracked before can be untracked by running
git rm --cached filename
- Placing
Repository exclude - For local files that do not need to be shared, you just add the file pattern or directory to the file
.git/info/exclude
. Theses rules are not committed, so they are not seen by other users. More information is here.
To make exceptions in the list of ignored files, see this question.
Solution 4
To ignore an entire directory in Git, the easiest way is to include a .gitignore file within the target directory which simply contains "*".
An illustrative example,
Example System
/root/
.gitignore
/dirA/
someFile1.txt
someFile2.txt
/dirB/
.gitignore
someFile3.txt
someFile4.txt
Goal
- ignore the contents of /dirB/
Top Level (/root/.gitignore)
- This is where your standard gitignore info goes (e.g. a “~Untitled.docx”, some private dirs, etc.). “dirB/“ can certainly be placed here, if needed
Ignored Directory (/root/dirB/.gitignore)
Git watches for gitignore at every step of the file system so anytime you have ignore specifications to apply then toss it in, generating a new gitignore for that dir
dirB/.gitignore then just reads as “*” and all contents are ignored completely, itself and all files!
And it's that simple :)
Solution 5
To instruct Git to ignore certain files or folders, you have to create .gitignore
file.
But in Windows Explorer you have to provide a name for the file. You just cannot create file with just an extension. The trick is that create a empty text file and go to command prompt and change the name of the file to .gitignore
:
ren "New Text Document.txt" .gitignore
Now open the file with your favorite text editor and add the file/folder names you wish you ignore. You can also use wildcards like this: *.txt
.
sf.
Updated on August 03, 2022Comments
-
sf. almost 2 years
How can I ignore directories or folders in Git using msysgit on Windows?
-
Gareth almost 12 yearsDo you want the cache folder to be excluded from the repository completely, or just its contents?
-
Mark Longair almost 12 yearsI'm guessing that the OP has multiple cache directories, whose contents should be ignored, but wants to make sure that those directories are created for anyone who clones the repository.
-
eckes almost 12 years@Gareth: since empty folders aren't tracked with git, if the content is ignored, the folder also will be, won't it?
-
Hailwood almost 12 yearsExactly, hence the index.html files. that way the folders are not empty.
-
Gareth almost 12 yearsI only mentioned that because some people use hidden files (commonly
.gitkeep
) to indicate a directory that should be kept -
Charlie Parker over 7 yearsis the
/
at the end what makes it know its a directory it should ignore?
-
-
Oliver almost 14 yearsOr just: echo dir_to_ignore/ > .gitignore
-
Arrowmaster over 13 yearsThat works? I've always just told people to open notepad and in the Save As dialog type the filename surrounded by doublequotes, so for example
".gitignore"
and it saves it without automatically adding an extension. -
Joey Green about 13 yearsIn a windows cmd prompt you can either use 'edit .gitignore' or 'notepad .gitignore' to create the correct file.
-
Triynko over 12 yearsNeat. Including a trailing period does work. Explorer strips off the last period, resulting in a file named ".gitignore". I think the quotes method is cleaner though and less likely to create surprises.
-
SGB over 12 yearsOr you can also use 'touch .gitignore' from within the windows git bash command prompt and that will create the correctly named file which can then in turn be edited by notepad or the like...
-
P. Galbraith about 12 yearsOr just create a file named .gitignore. with explorer and edit it with notepad (the trailing dot will be removed). That way you don't have to use command prompt.
-
MaurerPower almost 12 yearsgood job pointing out the
git rm --cached <filename>
. Absolutely critical for repos that existed BEFORE you created the.gitignore
-
andersop almost 12 yearsYes, git on windows is really finicky about .gitignore - the local exclude file does what I need though. Thanks!
-
Dave Everitt over 11 yearsYou probably do want to commit .gitignore especially as you're likely to want to track changes to it, and so is your team (if you're working with one). See stackoverflow.com/a/767213/123033
-
Mark Longair over 11 years@Dave Everitt: That's exactly why i said "Obviously you do want to commit .gitignore as well".
-
MeatFlavourDev about 11 yearsgit rm --cached <filename> fixed the problem I was having with .gitignore :)
-
deed02392 almost 11 years@P.Galbraith For me (Windows XP), I get an error trying to create a file with Explorer called
.gitignore.
:You must type a file name.
-
MollyCat almost 11 yearsOr you could vim .gitignore from the terminal. :)
-
Potaito over 10 yearsYeah git rm --cached filename is absolutely crucial. It was driving me crazy as git was still staging files I clearly stated to be ignored (created gitignore after initial commit). Thank you very much!
-
Godsmith over 10 yearsOr, which surprisingly has not been mentioned even though it is the fastest way, just type "echo folder_to_ignore>> .gitignore" in the console.
-
OsakaWebbie about 10 yearsOr, in the Save As dialog, change the file type to "All Files (.)" - then Windows will not append any extension.
-
Rayjax almost 10 yearsor in git bash just type "touch .gitignore" in your folder
-
Leon Gaban over 9 years@Godsmith your solution worked! :D thanks, I tried adding my folder
bower_components/
directly into the gitignore, but it still showed up -
verystrongjoe about 9 yearseven if i add the foder in .gitignore file, git is trying indexing the folder. how can I skip this? whenever I command git add index, it takes so long.
-
Castro Roy about 9 yearsOr
type nul > .gitignore
to create an empty file -
Vairis about 9 yearsOr just call the file
.gitignore.
when the file extensions are not hidden in your Windows explorer -
Jayant over 8 yearsSolved my problem. Git was tracking vendor folder in my laravel repo even though I had added vendor folder in gitignore. Thanks alot.
-
Mr Rubix over 8 yearsOn windows 7 simply create a new text document named .gitignore. and it will become .gitignore
-
Obmerk Kronen about 8 years
".gitignore."
- This is great trick ! I always had trouble with.htaccess
as well... -
Nitin... almost 8 yearsThis is the best answer, it also brings added benefits in project deployment and maintenance.
-
Oleksii Nezhyborets almost 8 yearsThis will recursively ignore all subfolders named
dir_to_ignore
, for example bothroot_dir/dir_to_ignore
androot_dir/some_dir/dir_to_ignore
would be ignored -
Laura over 7 yearsDouble check the file encoding that results from "echo folder_to_ignore >> .gitignore" on a Windows machine. Make sure git doesn't treat it as a binary file.
-
Charlie Parker over 7 yearsis the
/
at the end what makes it know its a directory it should ignore? -
Shimmy Weitzhandler about 7 yearsI like this approach, I just can't get along with the path in there, I want to ignore a folder in the main repo, how to?
-
Miss.Saturn about 7 yearsRemember to run git reset folder/
-
leerssej about 6 yearsor just add an extra
.
at the end so explorer stops thinking.gitignore
is the extension. Then on entry that trailing dot with no extension just gets eaten and you are left with.gitignore
TL;DR: try to name it.gitignore.
=> you end up with.gitignore
-
Putu De over 5 yearsI am using Atom on Windows 10. It's working! Thanks dude!
-
Xedret over 5 yearsI tried all the others with my current configuration and this was my ultimate definitive answer.
-
Peter Mortensen over 4 yearsBut aren't you supposed to leave files in .git alone (manipulate the content through official means)?
-
Peter Mortensen over 4 yearsThe link appears to be broken (it first redirects to the 'https' version): "Secure Connection Failed. An error occurred during a connection to archive.robwilkerson.org. PR_END_OF_FILE_ERROR"
-
Paiman Roointan over 4 yearsyou can also use this command in cmd:
copy con .gitignore
then type yourdir_to_ignore
then press ctrl+z followed by Enter! (learned it when I was a child and my dad was learning DOS) -
Vairis about 4 years@Rayjax I think bash does not go to the group "non command line solutions" :)
-
Rayjax about 4 yearsindeed- still useful to some apparently :)
-
skdhfgeq2134 almost 4 yearsWhat is the standard gitignore info? Can you give an example? What do you mean when you say "the file just reads as * ", this doesn't make any sense? I don't think I understood anything from this answer and couldn't find it at all simple.
-
skdhfgeq2134 almost 4 yearsWhere should I add this expression?
-
skdhfgeq2134 almost 4 yearsIs there a specific way we should add the files? Like I will just write the files separated by space and it will search the entire root folder for these files. Should I add the path of the files to this gitignore file? Where should I create this .gitignore file, in the root directory?
-
J-Dizzle almost 4 yearsThe file is one character, a single ‘*’. I will review and share further detail in a bit
-
mr_azad almost 4 yearsSometimes I have to run
git rm --cached -r .
to make the gitignore work properly.... -
D. Kermott over 3 yearsmy builds folder was being tracked. It must have been added to .gitignore after a build. Anyway, the command to untrack the folder is this: git rm --cached builds -r (where builds is the folder you want to untrack and -r is recursive meaning all files and sub-folders will be untracked)
-
Diego Favero over 3 yearsI needed to also add /.gitgnore to also ignore the file .gitignore itself
-
itsfarseen over 3 yearsI feel this is cleaner because it doesn't clutter your main .gitignore file
-
Frank Z. over 3 years@skdhfgeq2134 in English, when you say "X reads as Y" it means "Y is written on/in X"
-
J-Dizzle over 3 yearsElaborate please - I am not sure what you are saying! Share more detail and I will update
-
James Mwase almost 3 yearsFor me it was node_modules so had to run
git rm --cached node_modules -r
-
Matt Lemmon over 2 years@skdhfgeq2134: In programming, the asterisk * is short for "all" or "everything." So in this case "*" means everything in the directory.
-
Matt Lemmon over 2 yearsWhy !.gitignore with an exclamation point? The docs say the ! symbol negates instructions. Simply including * and .gitignore should work. More related details: stackoverflow.com/questions/10176875/add-gitignore-to-gitignore/10177000
-
Tarynn over 2 yearsWhat would that line actually ignore?
-
Tarynn over 2 years"Then just add information.." You brushed over a relatively important step there.
-
DINA TAKLIT over 2 yearsfrontend/node_modules folder; if u have backend and frontend for your app you can specify the ignore of node modules by typing the name of the front end folder /node_modules :)
-
gen over 2 yearsfyi this no longer applies to latest windows versions - you can now create files with a leading period