1505

How can I ignore directories or folders in Git using msysgit on Windows?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sf.
  • 24,512
  • 13
  • 53
  • 58
  • Do you want the cache folder to be excluded from the repository completely, or just its contents? – Gareth Jun 29 '12 at 10:09
  • 1
    I'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. – Mark Longair Jun 29 '12 at 10:19
  • @Gareth: since empty folders aren't tracked with git, if the content is ignored, the folder also will be, won't it? – eckes Jun 29 '12 at 10:34
  • Exactly, hence the index.html files. that way the folders are not empty. – Hailwood Jun 29 '12 at 10:53
  • 3
    I only mentioned that because some people use hidden files (commonly `.gitkeep`) to indicate a directory that should be kept – Gareth Jun 29 '12 at 18:17
  • 4
    is the `/` at the end what makes it know its a directory it should ignore? – Charlie Parker Jan 18 '17 at 21:59

20 Answers20

2138

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
stew
  • 21,640
  • 2
  • 18
  • 9
  • 151
    In a windows cmd prompt you can either use 'edit .gitignore' or 'notepad .gitignore' to create the correct file. – Joey Green May 09 '11 at 16:08
  • 53
    Or 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
  • 42
    Or 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 00:01
  • 6
    Or you could vim .gitignore from the terminal. :) – MollyCat Aug 19 '13 at 23:33
  • 44
    Or, 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 09 '14 at 06:49
  • 3
    This will recursively ignore all subfolders named `dir_to_ignore`, for example both `root_dir/dir_to_ignore` and `root_dir/some_dir/dir_to_ignore` would be ignored – Oleksii Nezhyborets Aug 16 '16 at 13:42
  • Double 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. – Laura Sep 07 '16 at 15:49
  • 1
    Remember to run git reset folder/ – Miss.Saturn May 20 '17 at 19:42
  • 1
    you can also use this command in cmd: `copy con .gitignore` then type your `dir_to_ignore` then press ctrl+z followed by Enter! (learned it when I was a child and my dad was learning DOS) – Paiman Roointan Dec 18 '19 at 11:24
  • 1
    Sometimes I have to run `git rm --cached -r .` to make the gitignore work properly.... – raz Jul 11 '20 at 10:24
  • Or you could create/find a good .gitignore file and copy and paste it every time you need it. That's why I'm here... mine is not working :) – als0052 Nov 30 '22 at 14:13
236

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"
Vairis
  • 1,758
  • 1
  • 13
  • 23
brainwavedave
  • 2,393
  • 1
  • 13
  • 3
  • 57
    That 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
  • 13
    Neat. 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 08 '11 at 22:29
  • 8
    Or, in the Save As dialog, change the file type to "All Files (*.*)" - then Windows will not append any extension. – OsakaWebbie Mar 26 '14 at 02:20
  • 7
    or in git bash just type "touch .gitignore" in your folder – Rayjax May 27 '14 at 12:25
  • 6
    `".gitignore."` - This is great trick ! I always had trouble with `.htaccess` as well... – Obmerk Kronen Apr 19 '16 at 07:07
  • 1
    fyi this no longer applies to latest windows versions - you can now create files with a leading period – gen Nov 17 '21 at 13:57
142

It seems that for ignoring files and directories there are two main ways:

  1. .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 running git 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

  2. 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vairis
  • 1,758
  • 1
  • 13
  • 23
  • 49
    good job pointing out the `git rm --cached `. Absolutely critical for repos that existed BEFORE you created the `.gitignore` – MaurerPower Jun 03 '12 at 01:06
  • 2
    git rm --cached fixed the problem I was having with .gitignore :) – MeatFlavourDev Mar 17 '13 at 23:27
  • 1
    Yeah 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! – Potaito Oct 08 '13 at 16:55
  • 1
    Solved 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 05:50
  • my 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) – D. Kermott Nov 09 '20 at 15:39
  • For me it was node_modules so had to run `git rm --cached node_modules -r` – James Mwase May 30 '21 at 12:07
66

To ignore an entire directory place a .gitignore of “*” there.

For 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)

  • You could just “dirB/“ here

Ignored Directory (/root/dirB/.gitignore)

  • Or you could “*” here

Git watches for gitignore at every step of the file system. So here I choose dirB/.gitignore as “*” to ignore dirB/, including all files and subdirs within.

Done ☺️

J-Dizzle
  • 4,861
  • 4
  • 40
  • 50
  • 4
    What 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 Jun 25 '20 at 04:38
  • 1
    The file is one character, a single ‘*’. I will review and share further detail in a bit – J-Dizzle Jun 25 '20 at 06:37
  • 1
    I feel this is cleaner because it doesn't clutter your main .gitignore file – itsfarseen Dec 29 '20 at 06:34
  • @skdhfgeq2134 in English, when you say "X reads as Y" it means "Y is written on/in X" – Frank Z. Jan 31 '21 at 10:56
  • 1
    Elaborate please - I am not sure what you are saying! Share more detail and I will update – J-Dizzle Jan 31 '21 at 19:04
  • 1
    @skdhfgeq2134: In programming, the asterisk * is short for "all" or "everything." So in this case "*" means everything in the directory. – Matt Lemmon Sep 14 '21 at 17:29
47

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mahes
  • 3,938
  • 1
  • 34
  • 39
45

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".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sf.
  • 24,512
  • 13
  • 53
  • 58
  • 2
    or 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
31

If you want to maintain a folder and not the files inside it, just put a ".gitignore" file in the folder with "*" as the content. This file will make Git ignore all content from the repository. But .gitignore will be included in your repository.

$ git add path/to/folder/.gitignore

If you add an empty folder, you receive this message (.gitignore is a 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sensorario
  • 20,262
  • 30
  • 97
  • 159
27

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 asterisks signify directory contents.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
wortwart
  • 3,139
  • 27
  • 31
26

You can create the ".gitignore" file with the contents:

*
!.gitignore

It works for me.

calraiden
  • 1,686
  • 1
  • 27
  • 37
  • 4
    This is the best answer, it also brings added benefits in project deployment and maintenance. – Nitin... Jun 20 '16 at 09:24
  • 1
    I needed to also add /.gitgnore to also ignore the file .gitignore itself – Diego Favero Dec 21 '20 at 02:42
  • Why !.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 – Matt Lemmon Sep 14 '21 at 18:18
24

Just in case you need to exclude sub folders you can use the ** wildcard to exclude any level of sub directory.

**/build/output/Debug/
Eat at Joes
  • 4,937
  • 1
  • 40
  • 40
17

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.

1ac0
  • 2,875
  • 3
  • 33
  • 47
Si3
  • 171
  • 1
  • 2
12

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.

Xedret
  • 1,823
  • 18
  • 25
8

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 information to this .gitignore file (using Notepad++ for example) which files or folders should be ignored. Save your changes. That's it :)

More information: .gitignore

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fvolodimir
  • 445
  • 5
  • 3
8

I've had some problems getting Git to pick up 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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jason
  • 564
  • 6
  • 12
  • Yes, git on windows is really finicky about .gitignore - the local exclude file does what I need though. Thanks! – andersop Jul 26 '12 at 00:39
7

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 it 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.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • 1
    You 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 http://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
6

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 it's a file named data.txt:

echo data.txt >> .gitignore

If it's a path like "Data/passwords.txt"

echo "Data/passwords.txt" >> .gitignore. 
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Olusola Omosola
  • 847
  • 9
  • 9
2

I had similar issues. I work on a Windows tool chain with a shared repository with Linux guys, and they happily create files with the same (except for case) names in a given folder.

The effect is that I can clone the repository and immediately 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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • The 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"* – Peter Mortensen Nov 14 '19 at 14:23
1

Just create .gitignore file in your project folder Then add the name of the folder in it for ex:

frontend/node_modules
DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74
  • What would that line actually ignore? – Tarynn Sep 22 '21 at 14:38
  • 1
    frontend/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 :) – DINA TAKLIT Sep 22 '21 at 18:48
1

This might be extremely obvious for some, but I did understand this from the other answers.

Making a .gitignore file in a directory does nothing by itself. You have to open the .gitignore as a text file and write the files/directories you want it to ignore, each on its own line.

so cd to the Git repository directory

touch .gitignore
nano .gitignore

and then write the names of the files and or directories that you want to be ignored and their extensions if relevant.

Also, .gitignore is a hidden file on some OS (Macs for example) so you need ls -a to see it, not just ls.

ACB_prgm
  • 57
  • 7
1

Temporarily ignore a directory/file that was already in git:

I have a lot of projects in a multi-project gradle project and they can take a long time to delete them, and they're all pretty much the same but different. From time to time I want to remove those from the gradle build by deleting them altogether. git can get them back after all. However I don't want them showing up in git status either. So I use the following simple procedure;

  1. delete files and folders I don't want.
  2. verify build still works
  3. tell git to ignore the deleted files for a bit (we can get them back)


git ls-files --deleted -z | git update-index --assume-unchanged -z --stdin

  1. go about life without the dirs until you want them back. Then run the same command as before but switch out assume-unchanged for no-assume-unchanged


git ls-files --deleted -z | git update-index --no-assume-unchanged -z --stdin

Calvin Taylor
  • 664
  • 4
  • 15