Ignoring directories in Git repositories on Windows

1,483,784

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:

  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.

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.

Share:
1,483,784
sf.
Author by

sf.

Updated on August 03, 2022

Comments

  • sf.
    sf. almost 2 years

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

    • Gareth
      Gareth almost 12 years
      Do you want the cache folder to be excluded from the repository completely, or just its contents?
    • Mark Longair
      Mark Longair almost 12 years
      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.
    • eckes
      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
      Hailwood almost 12 years
      Exactly, hence the index.html files. that way the folders are not empty.
    • Gareth
      Gareth almost 12 years
      I only mentioned that because some people use hidden files (commonly .gitkeep) to indicate a directory that should be kept
    • Charlie Parker
      Charlie Parker over 7 years
      is the / at the end what makes it know its a directory it should ignore?
  • Oliver
    Oliver almost 14 years
    Or just: echo dir_to_ignore/ > .gitignore
  • Arrowmaster
    Arrowmaster over 13 years
    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.
  • Joey Green
    Joey Green about 13 years
    In a windows cmd prompt you can either use 'edit .gitignore' or 'notepad .gitignore' to create the correct file.
  • Triynko
    Triynko over 12 years
    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.
  • SGB
    SGB over 12 years
    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...
  • P. Galbraith
    P. Galbraith about 12 years
    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.
  • MaurerPower
    MaurerPower almost 12 years
    good job pointing out the git rm --cached <filename>. Absolutely critical for repos that existed BEFORE you created the .gitignore
  • andersop
    andersop almost 12 years
    Yes, git on windows is really finicky about .gitignore - the local exclude file does what I need though. Thanks!
  • Dave Everitt
    Dave Everitt over 11 years
    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 stackoverflow.com/a/767213/123033
  • Mark Longair
    Mark Longair over 11 years
    @Dave Everitt: That's exactly why i said "Obviously you do want to commit .gitignore as well".
  • MeatFlavourDev
    MeatFlavourDev about 11 years
    git rm --cached <filename> fixed the problem I was having with .gitignore :)
  • deed02392
    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
    MollyCat almost 11 years
    Or you could vim .gitignore from the terminal. :)
  • Potaito
    Potaito over 10 years
    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!
  • Godsmith
    Godsmith over 10 years
    Or, which surprisingly has not been mentioned even though it is the fastest way, just type "echo folder_to_ignore>> .gitignore" in the console.
  • OsakaWebbie
    OsakaWebbie about 10 years
    Or, in the Save As dialog, change the file type to "All Files (.)" - then Windows will not append any extension.
  • Rayjax
    Rayjax almost 10 years
    or in git bash just type "touch .gitignore" in your folder
  • Leon Gaban
    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
    verystrongjoe about 9 years
    even 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
    Castro Roy about 9 years
    Or type nul > .gitignore to create an empty file
  • Vairis
    Vairis about 9 years
    Or just call the file .gitignore. when the file extensions are not hidden in your Windows explorer
  • Jayant
    Jayant over 8 years
    Solved my problem. Git was tracking vendor folder in my laravel repo even though I had added vendor folder in gitignore. Thanks alot.
  • Mr Rubix
    Mr Rubix over 8 years
    On windows 7 simply create a new text document named .gitignore. and it will become .gitignore
  • Obmerk Kronen
    Obmerk Kronen about 8 years
    ".gitignore." - This is great trick ! I always had trouble with .htaccess as well...
  • Nitin...
    Nitin... almost 8 years
    This is the best answer, it also brings added benefits in project deployment and maintenance.
  • Oleksii Nezhyborets
    Oleksii Nezhyborets almost 8 years
    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
  • Laura
    Laura over 7 years
    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.
  • Charlie Parker
    Charlie Parker over 7 years
    is the / at the end what makes it know its a directory it should ignore?
  • Shimmy Weitzhandler
    Shimmy Weitzhandler about 7 years
    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?
  • Miss.Saturn
    Miss.Saturn about 7 years
    Remember to run git reset folder/
  • leerssej
    leerssej about 6 years
    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
  • Putu De
    Putu De over 5 years
    I am using Atom on Windows 10. It's working! Thanks dude!
  • Xedret
    Xedret over 5 years
    I tried all the others with my current configuration and this was my ultimate definitive answer.
  • Peter Mortensen
    Peter Mortensen over 4 years
    But aren't you supposed to leave files in .git alone (manipulate the content through official means)?
  • Peter Mortensen
    Peter Mortensen over 4 years
    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"
  • Paiman Roointan
    Paiman Roointan over 4 years
    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)
  • Vairis
    Vairis about 4 years
    @Rayjax I think bash does not go to the group "non command line solutions" :)
  • Rayjax
    Rayjax about 4 years
    indeed- still useful to some apparently :)
  • skdhfgeq2134
    skdhfgeq2134 almost 4 years
    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
    skdhfgeq2134 almost 4 years
    Where should I add this expression?
  • skdhfgeq2134
    skdhfgeq2134 almost 4 years
    Is 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
    J-Dizzle almost 4 years
    The file is one character, a single ‘*’. I will review and share further detail in a bit
  • mr_azad
    mr_azad almost 4 years
    Sometimes I have to run git rm --cached -r . to make the gitignore work properly....
  • D. Kermott
    D. Kermott over 3 years
    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)
  • Diego Favero
    Diego Favero over 3 years
    I needed to also add /.gitgnore to also ignore the file .gitignore itself
  • itsfarseen
    itsfarseen over 3 years
    I feel this is cleaner because it doesn't clutter your main .gitignore file
  • Frank Z.
    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
    J-Dizzle over 3 years
    Elaborate please - I am not sure what you are saying! Share more detail and I will update
  • James Mwase
    James Mwase almost 3 years
    For me it was node_modules so had to run git rm --cached node_modules -r
  • Matt Lemmon
    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
    Matt Lemmon over 2 years
    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-gitign‌​ore/10177000
  • Tarynn
    Tarynn over 2 years
    What would that line actually ignore?
  • Tarynn
    Tarynn over 2 years
    "Then just add information.." You brushed over a relatively important step there.
  • DINA TAKLIT
    DINA TAKLIT over 2 years
    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 :)
  • gen
    gen over 2 years
    fyi this no longer applies to latest windows versions - you can now create files with a leading period