While working with git repositories there are some files which we don't want to push or pull even though it has changes which are not staged/committed for example files like database configuration file (which can be different for your local setup), properties file or any other configuration file.
There are 3 different ways in which we can specify files for Git to ignore, they are:
By adding a local .gitignore file
By creating a global .gitignore file for all of your local repositories
By specifying explicit repository exclude rules
We will cover all the 3 approaches one by one while explaining which one is the right approach for which situation along with some samples.
You can define a .gitignore file in the root of your repository or can have directory level .gitignore file for every directory in your repository. You can push your .gitignore file to your branch and the same file can be pulled by your teammates too. Generally files like db configuration (which can be different on your local setup), properties file or other configuration files should be included in .gitignore file.
Also, always try to use patterns while defining rules for ignoring files in your .gitignore file.
Creating a simple .gitignore file is very easy. Create a new file and name it .gitignore and then add the fully qualified name(along with the path) of the files that you wish to ignore.
For example, in the below code, I have ignored some configuration files:
# provide path of the file conf/db.yaml # we can add multiple files in it conf/someotherfile.py # we can also specify pattern # like if I want to ignore all files # in the conf folder conf/*
You can define rules for all your local git repositories if you work with multiple git repositories on your local system, using the GIT
core.excludesFile property. You can specify this property in a .gitignore file which you can keep in the folder where all your git repositories are. Once you have created the .gitignore file and defined the rules in it which you want to be applied for all your local git repositories to not commit/stage/push changes made in some files (as per .gitignore file), you need to run the following command to set your .gitignore file as the global .gitignore file for all your local repositories.
git config --global core.excludesFile <PATH TO .GITGNORE FILE>
You must use the global .gitignore file with extra care. Generally, it is suggested to keep OS specific files, temporary created files by IDE etc in global .gitignore file.
If you want to define some rules for a repository and do not even want to include those patterns in the shared .gitignore file which is also version controlled as part of the repository, in that case, you can specify explicit exclude rules for the repository. You can set these patterns for ignoring certain files in the .git/info/exclude file in the repository. These patterns/rules are not version controlled or shared with your team.
You can keep some local settings that you use while development like logger settings or may be configuration for your working directory etc.
So these are the 3 different ways in which you can define rules/patterns to not include certain files matching these rules while committing/staging/pushing changes to the repository.
Once a file is committed it becomes a part of the repository and will be cached and cannot be ignored. So to ignore such files you first need to remove them from the repository only then they will not get picked up for commit/staging/push to the repository. Now there are multiple ways to do that. Simplest one being, delete the file from repository, commit and push the changes. Then create a new rule for ignoring that file locally in your .gitignore file and then again create the file in your working directory.
Well, there is a simple way too, run the following command:
git rm --cached <FILE NAME>
Running the above command will remove the File with name <FILE NAME> from your repository but the file will remain in your working directory.
If you had a file ignored earlier and now wish to commit it, you can do so by using the force option with git add command. Let's take an example to understand this, if we have a file say xyz.txt which is included in .gitignore file and now has to be removed from it and has to be commited to the repository, then you can runt he following command:
git add -f xyz.txt git commit -m "Force adding xyz.txt"
Running the above command will forcefully add the ignored file back to your repository. After doing so, you should also remove the ignoring pattern/rule from your .gitignore file.
The .gitignore files are useful and must be utilized for avoiding local system specific changes to get pushed to the repository which can lead to errors or misconfiguration for other team members or may be the live production code. Feel free to ask any question, if you have any.