As the name implies, the .gitignore file is used to instruct git on what files and directories to be ignored from the git initialized directory. When you work on any projects, some files and directories might be created just for testing or for your own reference. The project directory might also have passwords, keys, and critical information that should not be visible in the centralized repository. Using the .gitignore file you can instruct git to skip tracking the given file so it will not be committed and pushed to the centralized repository.
Normally the .gitignore file is created under the project top-level directory(root directory). It is still possible to define multiple .gitignore files within the same project under different sub-directories.
I am creating a new project directory named git_test and initializing it.
$ mkdir git_test; cd git_test $ git init . Initialized empty Git repository in /home/nixzie/Documents/testing_ground/git/git_test/.git/
Under the project directory, I have two markdown files and one python script.
$ ls README.md scrapper.py TODO.md
The markdown file TODO.md is created for my personal reference and it is of no use for the project so it should be added to the .gitignore file.
$ nano .gitignore TODO.md
Now when I stage the changes, the TODO.md file is not tracked by the git.
On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) .gitignore README.md scrapper.py nothing added to commit but untracked files present (use "git add" to track)
To commit the changes run the following command.
$ git add * $ git commit -m "My initial project setup"
Ignore Warning Message
You may get a hint message stating that there are files to be ignored from the .gitignore file.
The following paths are ignored by one of your .gitignore files: TODO.md hint: Use -f if you really want to add them. hint: Turn this message off by running hint: "git config advice.addIgnoredFile false"
To turn off this message run the following command.
$ git config advice.addIgnoredFile false
Alternatively, you can add the following property to the project config file to suppress the message.
[advice] addIgnoredFile = false
Force Commit An Ignored File
If you wish to commit the ignored files use the -f flag when you run the git add command. This will force the file to be added to the staging directory even though the file is present in the .gitignore file and further commit to the repository.
$ git add -f TODO.md $ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: TODO.md
Gitignore vs Exclude file
As you know .gitignore files are versioned like any other file in the repository and pushed to the central repository. This means you cannot place your personal exclude patterns inside the .gitignore file. The patterns should align with the project and all the collaborators.
Alternatively, you can place your personal exclude patterns in the .git/info/exclude file which will not be versioned by git and not be pushed to the central repository. So whatever exclude patterns you create under exclude file will be available only to you on your machine.
In my case placing the TODO.md under the exclude file will make sense since it is only for my use.
$ ls -l .git/info/ total 4 -rw-rw-r-- 1 nixzie nixzie 240 Jun 13 22:18 exclude
Add Committed File To Ignore List
To ignore a file or directory that is already versioned by git you need to first remove it from the repository so that there is no versioned copy of the file or directory.
Run the following command to remove the file from the repository. If you skip the –cached flag, the file will be removed from the local project directory as well as the git repository.
$ git rm --cached TODO.md rm 'TODO.md' $ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) deleted: TODO.md
Commit the change again to the repository.
$ git add . $ git commit -m "Removed TODO.md" $ git status On branch master nothing to commit, working tree clean
Now you can add the file or pattern to the .gitignore file and the git will not track this file.
Patterns In GitIgnore File
Till now my examples are based on ignoring a single file. Patterns are also supported where you can exclude more than one file are directories.
1. To create a comment, use the # symbol.
# Ignore TODO.md as this is for my personal reference TODO.md
2. To match one or more characters as a pattern use the * symbol.
# Any file that ends with .md will be ignored *.md
3. To match exactly one character use ? symbol.
# ‘?’ just matches one character TO?O.md
4. Using the ! symbol you can include a file to be committed. For example, I can skip all the files with .md but add README.md alone by prefixing it with ! symbol.
# Exclude all .md files from the project directory *.md # Include README.md file !README.md
5. To ignore an entire directory including its files and subdirectory add the directory name with a slash.
# Ignore directory tree. metrics_info_local_test/
Configure Global Gitignore File
You can also create a gitignore file globally which will be applied to all the repositories. When you have multiple git repositories and any similar patterns across the projects can be grouped here globally.
You should manually create a file and add it to the global configuration file for git to know that this file is used as a global ignore file. You can create the file with any name and in any directory but the home directory is the perfect place to create it.
$ mkdir ~/.global_gitignore
Run the following command to configure this file as a global ignore file.
$ git config --global core.excludesFile ~/.global_gitignore
Alternatively, you can also add the property manually in the .gitconfig file under the home directory.
$ nano ~/.gitconfig [core] excludesFile = /home/nixzie/.global_gitignore
Wrap Up
If you have any feedback or tips about this article, you can share them with us through the comment section.