Git: How to create and use a .gitignore file on Windows

If you have a project that you have git enabled, but you don’t want git to track all of the files in the project folder (perhaps because you don’t want all files to be uploaded to github), you can tell Git to ignore certain files, file types, or sub-directories by creating a “.gitignore” file in your git repository.

To set up a .gitignore file on a Windows machine, in your command window cd to the git directory and type the command:

run dir > .gitignore

This outputs the entire directory listing to a file named .gitignore in your git directory.

To edit the .gitignore file, open it in Notepad++ and replace the directory listing with a single line that contains the file types you want git to ignore. For example, if you want git to ignore .pyc files:

*.pyc

When you run the git status command in your project folder, Git will read every line of the .gitignore file, where each line is a condition that git should ignore. The types of conditions that can be set in the .gitignore file are:

  • File types
  • individual file names
  • specific directories

Here is a table that shows how to implement each of these condition types:

File types *.pyc
individual file names thisfile.txt
specific directories directory_name/

Notes:

  • Notice that * can used as a wildcard (meaning anytime this pattern is found).
  • Also note that for specific directories, all that is needed is to append the directory name with a forward slash.
  • Pound signs ‘#’ can be used as comment lines

Here is an example of a real .gitignore file:

# .gitignore for Grails 1.2 and 1.3
# Although this should work for most versions of grails, it is
# suggested that you use the "grails integrate-with --git" command
# to generate your .gitignore file.

# web application files
/web-app/WEB-INF/classes

# default HSQL database files for production mode
/prodDb.*

# general HSQL database files
*Db.properties
*Db.script

# logs
/stacktrace.log
/test/reports
/logs

# project release file
/*.war

# plugin release files
/*.zip
/plugin.xml

# older plugin install locations
/plugins
/web-app/plugins

# "temporary" build files
/target

 

There are many other real .gitignore files that can be used as examples here:
https://github.com/github/gitignore

For more information about gitignore, see:
https://help.github.com/articles/ignoring-files/
https://git-scm.com/docs/gitignore