Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring directories in Git repositories on Windows

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

like image 519
sf. Avatar asked Dec 05 '08 12:12

sf.


People also ask

How do I ignore files while pushing to git repository?

Excluding local files without creating a . Use your favorite text editor to open the file called . git/info/exclude within the root of your Git repository. Any rule you add here will not be checked in, and will only ignore files for your local repository.

Should you ignore .GIT folder?

The entire vendor folder should be ignored, not just the . git sub-directories. Which packages are used are stored in composer.


8 Answers

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.

like image 180
stew Avatar answered Oct 04 '22 05:10

stew


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"
like image 22
brainwavedave Avatar answered Oct 04 '22 04:10

brainwavedave


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.

like image 38
Vairis Avatar answered Oct 04 '22 04:10

Vairis


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 ☺️

like image 45
J-Dizzle Avatar answered Oct 04 '22 03:10

J-Dizzle


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.

like image 38
Mahes Avatar answered Oct 04 '22 05:10

Mahes


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

like image 42
sf. Avatar answered Oct 04 '22 04:10

sf.


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
like image 20
sensorario Avatar answered Oct 04 '22 03:10

sensorario


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

*
!.gitignore

It works for me.

like image 25
calraiden Avatar answered Oct 04 '22 05:10

calraiden