How can I ignore directories or folders in Git using msysgit on Windows?
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 info here.
-
117In a windows cmd prompt you can either use 'edit .gitignore' or 'notepad .gitignore' to create the correct file. – Joey Green May 9 '11 at 16:08
-
42Or 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... – SGB Nov 22 '11 at 21:48
-
37Or 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. – P. Galbraith Mar 30 '12 at 0:01
-
5
-
29Or, which surprisingly has not been mentioned even though it is the fastest way, just type "echo folder_to_ignore>> .gitignore" in the console. – Godsmith Feb 9 '14 at 6:49
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 explorer thinks its a file of type gitignore with no name.
Non command line solution:
You can rename a file to ".gitignore." and it will create ".gitignore"
-
40That 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. – Arrowmaster Feb 10 '11 at 17:44 -
8Neat. 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. – Triynko Sep 8 '11 at 22:29
-
5
-
2Or, in the Save As dialog, change the file type to "All Files (.)" - then Windows will not append any extension. – OsakaWebbie Mar 26 '14 at 2:20
-
4
It seems that for ignoring files and directories there are two main ways:
.gitignore
- Placing
.gitignore
file into the root of your repo besides.git
folder (in Windows make sure you see the true file extension and then make.gitignore.
(with the point at the end to make empty file extension) ) - Making global configuration
~/.gitignore_global
and runninggit config --global core.excludesfile ~/.gitignore_global
to add this to your git config
note: files tracked before can be untracked by running
git rm --cached filename
- Placing
Repo 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 commited, so are not seen by other users more info here
[updated] To make exceptions in list of ignored files, see this question.
-
28good job pointing out the
git rm --cached <filename>
. Absolutely critical for repos that existed BEFORE you created the.gitignore
– MaurerPower Jun 3 '12 at 1:06 -
2git rm --cached <filename> fixed the problem I was having with .gitignore :) – JeremyFelix Mar 17 '13 at 23:27
-
1Yeah 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! – Cat Oct 8 '13 at 16:55
-
1Solved my problem. Git was tracking vendor folder in my laravel repo even though I had added vendor folder in gitignore. Thanks alot. – Jayant Aug 31 '15 at 5:50
I had some issues creating a file in windows explorer with a . at the beginning.
a workaround was to go into the commandshell and create a new file using "edit"
-
1or 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
– leerssej Feb 25 '18 at 20:13
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 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
hope it answers you question
-
13
-
-
2
-
Or just call the file
.gitignore.
when the file extensions are not hidden in your Windows explorer – Vairis May 6 '15 at 11:43
If you want to maintain a folder and not the files inside it, just put a ".gitignore" file in the folder with "*" as content. This file will ignore all content from repository. But .gitignore
will be include in your repo.
$ git add path/to/folder/.gitignore
If you add empty folder, you receive this message (.gitignore is hidden file)
The following paths are ignored by one of your .gitignore files:
path/to/folder/.gitignore
Use -f if you really want to add them.
fatal: no files added
So, use "-f" to force add:
$ git add path/to/folder/.gitignore -f
In Windows there's an extra catch with slashes. Excluding a single directory in .gitignore with
dir_to_exclude/
will possibly work, but excluding all directories with
/
causes problems when you have file names with spaces (like my file.txt
) in your directory: Git bash escapes these spaces with a backslash (like my\ file.txt
) and Git for Windows doesn't distinguish between /
and \
.
To exclude all directories better use:
**/
Two consecutive asteriscs signify directory contents.
Also in your \.git\info
projects directory there is an exclude file that is effectively the same thing as .gitignore
(I think). You can add files and directories to ignore in that.
-
I 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? – Shimmy May 18 '17 at 6:50
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 .gitignore (/root/.gitignore)
- This is where your standard gitignore info goes
Ignored Directory .gitignore (/root/dirB.gitignore)
- This file just reads as '*' and the directory is ignored completely, itself and all files!
and it's that simple :)
I've had some problems getting git to pickup the .gitignore file on Windows. The $GIT_DIR/info/exclude file always seems to work though. The downside of this approach, however, is that the files in the $GIT_DIR directory are not included in the check-in, and therefore not shared.
( p.s. $GIT_DIR is usually the hidden folder named .git )
-
Yes, git on windows is really finicky about .gitignore - the local exclude file does what I need though. Thanks! – andersop Jul 26 '12 at 0:39
You can create the ".gitignore" file with the contents:
*
!.gitignore
It works for me and simples.
-
2This is the best answer, it also brings added benefits in project deployment and maintenance. – Nitin... Jun 20 '16 at 9:24
I assume the problem is that your working tree is like:
a-cache/foo
a-cache/index.html
b-cache/bar
b-cache/foo
b-cache/index.html
.gitignore
... with the .gitignore
you describe. This will give you git status
output like:
$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore
# a-cache/
# b-cache/
... if the index.html
files have not yet been added to the repository. (git sees that there are unignored files in the cache directories, but only reports the directories.) To fix this, make sure that you have added and committed the index.html
files:
git add *cache/index.html
git commit -m "Adding index.html files to the cache directories"
... and your git status
will then look like:
$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore
nothing added to commit but untracked files present (use "git add" to track)
(Obviously you do want to commit .gitignore
as well, I was just being lazy with this test case.)
-
1You 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 – Dave Everitt Jan 15 '13 at 14:24
-
2@Dave Everitt: That's exactly why i said "Obviously you do want to commit .gitignore as well". – Mark Longair Jan 15 '13 at 15:16
-
3
Just in case you need to exclude sub folders you can use the **
wildcard to exclude any level of sub directory.
**/build/output/Debug/
When everything else fails try editing the file
/.git/info/exclude
and adding the directories you want to the end of the file, like this:
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
assets/
compiled/
I added the folders "assets" and "compiled" to the list of files and directories to ignore.
-
-
I tried all the others with my current configuration and this was my ultimate definitive answer. – Xedret Nov 14 '18 at 19:59
On Unix:
touch .gitignore
On Windows:
echo > .gitignore
These commands executed in a terminal will create a .gitignore
file in the current location.
Then just add info to this .gitignore
file (using Notepad++ for example) which files or folders should be ignored. Save your changes. That's it :)
More information: https://www.atlassian.com/git/tutorials/saving-changes/gitignore
On windows and Mac, if you want to ignore a folder named Flower_Data_Folder in the current directory, you can do:
echo Flower_Data_Folder >> .gitignore
If its a file named data.txt
echo data.txt >> .gitignore
if its a path like "Data/passwords.txt"
echo "Data/passwords.txt" >> .gitignore.
I had similar issues, I work on a windows tool chain with a shared repo with linux guys, they happlily create files with the same [except for case] names in a given folder.
The effect is that I can clone the repo and immediatly have dozens of 'modified' files that if I checked in would create havoc.
I have windows set to case sensitive and git to not ignore case but it still fails (in the win32 api calls apparently).
If I gitignore the files then I have to remember to not track the .gitignore file.
But I found a good answer here http://archive.robwilkerson.org/2010/03/02/git-tip-ignore-changes-to-tracked-files/index.html
Chris
protected by Shankar Damodaran Sep 26 '14 at 7:53
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
.gitkeep
) to indicate a directory that should be kept – Gareth Jun 29 '12 at 18:17/
at the end what makes it know its a directory it should ignore? – Charlie Parker Jan 18 '17 at 21:59