For every repository, there are usually certain types of files you don't want tracked in the repository. The files can be configuration files, build output, or just backup files created by the editor when editing the file. To avoid these files showing up in the untracked files section of the git status
output, it is possible to add them to a file called .gitignore
. Entries in this file that match files in the working directory will not be considered by git status
.
Clone the cookbook-tips-tricks
repository and check out the ignore
branch:
$ git clone https://github.com/dvaske/cookbook-tips-tricks.git $ cd cookbook-tips-tricks $ git checkout ignore
First, we'll create some files and directories:
$ echo "Testing" > test.txt $ echo "Testing" > test.txt.bak $ mkdir bin $ touch bin/foobar $ touch bin/frotz
Let's see the output of git status
:
$ git status On branch ignore Your branch is up-to-date with 'origin/ignore'. Untracked files: (use "git add <file>..." to include in what will be committed) test.txt nothing added to commit but untracked files present (use "git add" to track)
Only the test.txt
file showed up in the output. This is because the rest of the files are ignored by Git. We can check the content of .gitignore
to see how this happened:
cat .gitignore *.config *.bak # Java files *.class bin/
This means that *.bak
, *.class
, *.config
, and everything in the bin
directory are being ignored by Git.
If we try to add files in a path ignored by Git, for example bin
, it will complain:
$ git add bin/frotz The following paths are ignored by one of your .gitignore files: bin/frotz Use -f if you really want to add them.
But, it also tells us an option to use if we really want to add it, -f
:
$ git add -f bin/frotz $ git status On branch ignore Your branch is up-to-date with 'origin/ignore'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: bin/frotz Untracked files: (use "git add <file>..." to include in what will be committed) test.txt
If we ignore the foo
file, which is already tracked, and modify it, it still shows up in the status since tracked files are not ignored:
$ echo "foo" >> .gitignore $ echo "more testing" >> foo $ git status On branch ignore Your branch is up-to-date with 'origin/ignore'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: bin/frotz Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: .gitignore modified: foo Untracked files: (use "git add <file>..." to include in what will be committed) test.txt
Let's add and commit foo, .gitignore and the content of the current staging area:
$ git add foo .gitignore $ git commit -m 'Add bin/frotz with force, foo & .gitignore' [ignore fc60b44] Add bin/frotz with force, foo & .gitignore 3 files changed, 2 insertions(+) create mode 100644 bin/frotz
It is also possible to ignore files of a repository without the .gitignore
files. You can put your ignored files in a global ignore file, for example ~/.gitignore_global
, and globally configure Git to also consider entries in this file to be ignored:
$ git config --global core.excludesfile ~/.gitignore_global
You can also do it per repository in the .git/info/exclude
file. If you use either of these options, you won't be able to easily share the ignored file; they can't be added to the repository as they are stored outside it. Sharing the .gitignore
files is much easier; you just add and commit it to Git. But, let's see how the other options work:
$ echo "*.test" > .git/info/exclude $ touch test.test $ git status On branch ignore Your branch is ahead of 'origin/ignore' by 1 commit. (use "git push" to publish your local commits) Untracked files: (use "git add <file>..." to include in what will be committed) test.txt nothing added to commit but untracked files present (use "git add" to track) $ ls bar bin foo test.test test.txt test.txt.bak
We can see that the .test
file didn't show up in the status
output and that the ignored files exists in the working directory.
There is a wide range of files ignored commonly, for example, to avoid accidentally adding text editor backup files, *.swp
, *~.
and *.bak
are commonly ignored. If you are working on a Java project, you might add *.class
, *.jar
, *.war
to your .gitignore
and *.o
, *.elf
, *.lib
if you are working on a C project. Github has a repository dedicated to collect Git ignore files for different programming languages and editors/IDEs. You can find it at https://github.com/github/gitignore.