A lot of the time you would see that you have created some file for your own use while using the Git. This may include a log file which you have kept to keep the log of the major things you have done till now, as an example. You would also want to ignore some files that are created along with time like editor backup files or build products or local configurations etc. These files are important but can be ignored while committing our changes into the repository. For such files, Git has an option of Git Ignore that this tutorial is about. The key points we will be discussing in this post are:
- Purpose of Git Ignore File
- Implementing Git Ignore in Git
- General Patterns used in Git Ignore
- Committing Ignored Files Forcefully in Git
What is Git Ignore?
Git Ignore is a file in Git that helps us ignore the files that we don’t want to commit or don’t want to track. Although a file which has already been tracked by Git cannot be ignored. Through Git ignore, you can assign the temporary or transient files that you do not want to get commit. These files are useless for other developers and your projects like projects build or IDE temp files.
Git-ignore can also include patterns which will tell Git a generalized version of the kind of files to ignore. A simple example would be *.log to ignore all the files with the extension .log.
The following image shows the .gitignore file while working on a project in Java.
As you can notice the pattern, * includes all the file with the extension that follows it. These are the generally ignored file by the developers. These extensions are simple for those who have spent some time in development. For those who are new or learning, the meaning has been described below.
ctxt file: A ctxt file is created by BlueJ. It contains the comment and documentation structure of the class.
nar file: Created by Nokia, it contains the metadata (generally XML format) and sometimes digital photos in compressed form.
ear file: Contains complete enterprise collection of data such as a collection of JAR Files.
.tar.gz: A common UNIX format to contain the source code of the complete file to pack and download.
Git ignore is a simple concept in Git and there are no hidden complexities. In this tutorial, we will create a file and ask Git to ignore it to demonstrate the working of Git Ignore.
How to use Git Ignore in Git.
In this section, we will explore how to use Git ignore to ignore a file in Git that we have created for our own purpose. For this, remember that you might not be having .gitignore file beforehand on your project in Git. So, we will add .gitignore file before proceeding ahead. On the same note, we will follow some simple steps given below.
Before proceeding forward, check the status of your repository. It should be a clean working repository. If not, please commit all your changes before proceeding ahead. You can refer how to commit in Git tutorial for some revision or help.
Create .gitIgnore File
We have created a lot of file during the course. Following the same scene, use the touch command to add .gitignore file.
Add intended file to GitIgnore
Open the file in notepad.
Write the name of the Ignore.txt file in notepad.
Note: Ignore.txt is a file that we will ignore using git-ignore. We are defining it before its creation to demo how it works.
Commit these changes to add .gitignore file to the repository. If you do not add this file to the repository, then Git won’t know that a .gitignore file exists.
Confirm the status of the repository
Once done, create another file Ignore.txt. This file will be used to log some comments and we do not want to commit this file since these comments are for personal use.
Open the file Ignore.txt in the notepad.
Write the following lines in the File (not necessary, you can write anything).
This is a course for Git.
It is aimed to make an individual proficient and expert in Git.
The basic knowledge of Git is not required to start this course.
Save the changes and close the notepad. Now again check the status of your Git repository.
As you can see, it is still a clean repository even though we created a new file and made some changes to it. It shows that our file Ignore.txt has been ignored by the Git. Now onwards, whenever the commit is done, git always ignores this file automatically.
Patterns used in Git Ignore File in Git.
As I mentioned in the intro section, you can use *.log expression to ignore all the files with .log extension. This is called a pattern in a .gitignore file.
In the last section, we directly mentioned a file name inside the .gitignore. It may so happen sometimes that we do not need a particular type of file at all. You can search through the internet some different git-ignore files examples to observe different patterns.
For these situations, we take the option of using patterns. Gitignore finds the pattern in its content and executes it according to the pattern. In this section, I will introduce you to these patterns.
- Blank Line: Works as a separator for readability only. No technical meaning attached to it.
- #<line>: If you start a line with #, it serves as a comment.
- \# – If # is a part of the pattern. For example, if the directory name is #ToolsQA then directly specifying #ToolsQA would be considered as a comment. So \#ToosQA is to be written.
- <name>/ – This will search in the directories only like ToolsQA/ will search for a directory named ToolsQA and not any file by that name.
- **/<name> – This pattern will search for a directory or a file name by the name <name>. For example, **/ToolsQA. It means a match for ToolsQA in all the directories.
- <name>/** – This pattern matches for everything inside the <name> directory. For example, ToolsQA/** will search for all the directories inside ToolsQA.
These patterns are quite useful when you have machine generated temporary files or build artifacts. Actually anything you don’t want to commit. An important point to note here is that .gitignore file works only in the directory in which it has been located. For example, a .gitignore file in ToolsQA directory would work only inside ToolsQA directory. Therefore, it is a good practice to always declare the .gitignore file inside the root to have its effect in all the directories. This will also help you to push that file to your teammates when working on a single project.
GitIgnore Sample Files
GitIgnore is used in all the languages, software, technologies and tools through which you are using Git directly. So, when a developer is working on his project on his IDE/tool, he will create a GitIgnore file to avoid a few things being committed. In this section, we have provided the sample files for you to look and understand how a file looks on a real project.
GitIgnore Java Sample File
The GitIgnore sample file for a project that has been developed in Java can be accessed from here: Sample Java GitIgnore File
GitIgnore Dot Net Sample File
The GitIgnore sample file for a project that has been developed in Dot Net can be accessed from here: Sample Dot Net GitIgnore file
GitIgnore Sample Files (Complete)
The GitIgnore sample files for different software, tools, and languages can also be visited from here. You can view the file related to your known language or tool and it will be easy to understand which files are normally ignored.
How to commit an ignored file?
While you must have understood that GitIgnore is used to ignore the files in a directory or all the directories, there are cases that are exceptions.
Let me give an example. Consider you do not want any file with an extension .log, so you write in .gitignore the pattern *.log. This will exclude all the .log file. But, there is a single file that you would like to commit while ignoring all other .log files simultaneously. For this, we use the forced method to commit the file.
In the following steps, it has been explained using the same ignore.txt file that we ignored in the above section.
To commit an ignored file forcefully we make use of the flag -f.
So, type the following command to execute temp.log even though it has been ignored.
git add -f ignore.txt
git commit -m “Committing Ignored File”
As you can see, 1 file has been changed which is Ignore.txt. It was still committed even though we ignored that file. This is how it works. Honestly, when I came across the .gitignore file, I was not sure whether I will be using it in the future or not. But, when I worked on some big projects, it became one of the most frequently visited files by me.
Therefore, keep practicing on Git using git ignore examples without any purpose from now onwards. It will surely help in the future. We will move on to our next tutorial now.