How To Add Files To .gitignore

What Does .gitignore Do?

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected; see the NOTES below for details.

Each line in a gitignore file specifies a pattern. When deciding whether to ignore a path, Git normally checks gitignore patterns from multiple sources, with the following order of precedence, from highest to lowest (within one level of precedence, the last matching pattern decides the outcome):

  • Patterns read from the command line for those commands that support them.
  • Patterns read from a .gitignore file in the same directory as the path, or in any parent directory, with patterns in the higher level files (up to the toplevel of the work tree) being overridden by those in lower level files down to the directory containing the file. These patterns match relative to the location of the .gitignore file. A project normally includes such .gitignore files in its repository, containing patterns for files generated as part of the project build.
  • Patterns read from $GITDIR/info/exclude.
  • Patterns read from the file specified by the configuration variable core.excludesFile.

Which file to place a pattern in depends on how the pattern is meant to be used.

  • Patterns which should be version-controlled and distributed to other repositories via clone (i.e.,

files that all developers will want to ignore) should go into a .gitignore file.

  • Patterns which are specific to a particular repository but which do not need to be shared with

other related repositories (e.g., auxiliary files that live inside the repository but are specific

to one user’s workflow) should go into the $GITDIR/info/exclude file.

  • Patterns which a user wants Git to ignore in all situations (e.g., backup or temporary files

generated by the user’s editor of choice) generally go into a file specified by core.excludesFile

in the user’s /.gitconfig. Its default value is $XDGCONFIGHOME/git/ignore. If $XDGCONFIGHOME

is either not set or empty, $HOME/.config/git/ignore is used instead.

Example of How to do it!

In this example, I have a file named nat.db. Since this is an Sqlite file and will be different on my local computer that I am developing on compared the production system that updates the database on a more regular basis.

  • Open a terminal window
  • change to the directory that is has the local project files in it and go into the git directory
    • cd youprojectdirecty/.git/
      				
  • edit or create the file
    • nano .gitignore
      				
  • Enter the name of the file or maybe every the types by using * as the variable
    • nat.db
      				*.db
      				
  • press ctrl + x
  • press ‘y’ to save
  • press enter to accept the default file name

Verify the File is No Longer Being Tracked

  • In a terminal window, in the project base directory
  • enter git status
    • $ git status
      				On branch master
      				Your branch is up to date with 'origin/master'.
      				....
      
      				Untracked files:
      				  (use "git add <file>..." to include in what will be committed)
      
      					.DS_Store
      					nat.db
      					test.py
      
      				
  • Now you see that the file nat.db is not longer being tracked

From git help ignore

If you want to read from the true source…

  • open a terminal window and type
  • git help ignore
EXAMPLES
               $ git status
               [...]
               # Untracked files:
               [...]
               #       Documentation/foo.html
               #       Documentation/gitignore.html
               #       file.o
               #       lib.a
               #       src/internal.o
               [...]
               $ cat .git/info/exclude
               # ignore objects and archives, anywhere in the tree.
               *.[oa]
               $ cat Documentation/.gitignore
               # ignore generated html files,
               *.html
               # except foo.html which is maintained by hand
               !foo.html
               $ git status
               [...]
               # Untracked files:
               [...]
               #       Documentation/foo.html
               [...]

       Another example:

               $ cat .gitignore
               vmlinux*
               $ ls arch/foo/kernel/vm*
               arch/foo/kernel/vmlinux.lds.S
               $ echo '!/vmlinux*' >arch/foo/kernel/.gitignore

       The second .gitignore prevents Git from ignoring arch/foo/kernel/vmlinux.lds.S.

       Example to exclude everything except a specific directory foo/bar (note the /* - without the slash,
       the wildcard would also exclude everything within foo/bar):

               $ cat .gitignore
               # exclude everything except directory foo/bar
               /*
               !/foo
               /foo/*
               !/foo/bar

About Daniel Fredrick

Technology enthusiast, Programmer, Network Engineer CCIE# 17094

View all posts by Daniel Fredrick →

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.