Git

How Do I Use the “.gitignore” File?

While working with Git repositories, there are certain files that we do not wish to commit whenever we make any changes to our repository. It is because these files may be related to certain configurations or libraries that we do not want to mess up or change. Also, by recommitting such files, you might end up messing up your whole project repository. This is why Git introduced the concept of the .gitignore file. It is a file in Git in which you can add all those files of your project repository that you do not want to recommit, or in other words, the files that you want to ignore while committing. Therefore, today we will explain to you the method of using the .gitignore file in Ubuntu 20.04.

Method of using the “.gitignore” File in Ubuntu 20.04

To use the .gitignore file in Ubuntu 20.04, we will walk you through the following nine steps:

Step 1: Acquire the Test Repository

Instead of creating our own project repository, we have used a sample repository available at GitHub. You need to acquire this repository by running the command mentioned below:

git clone https://github.com/schacon/simplegit-progit

Once the specified repository has been cloned to your Ubuntu 20.04 system, it will display the following status on the terminal:

Step 2: Create a Sample File to be ignored

Now we need to create a sample file that we want to be ignored in our project directory. For that, we first need to go to our project directory by running this command:

cd /home/kbuzdar/simplegit-progit

Here, you need to provide the path where you have cloned the test repository.

Once you are within the test repository, you can create a sample file in it by running the following command:

sudo nano abc.txt

When this file opens up with the nano editor, you can write any random text in it, as shown in the image below, after which you can save this file.

Step 3: Create the .gitignore File

Once we have created a file that we want to ignore in our next commit, we will create the .gitignore file in our project repository by running the following command:

sudo nano .gitignore

Step 4: Add the Files to be ignored in the .gitignore File

When the .gitignore file opens with the nano editor, you have to add the name of the file that you want to ignore to the .gitignore file. In our case, it is the abc.txt. After adding this file to the .gitignore file, we will simply save it. You can add as many files as you want to ignore in the .gitignore file in separate lines.

Step 5: Reinitialize Git

Once we have made the desired changes, we have to reinitialize Git by running the following command:

git init

If this command manages to reinitialize Git successfully, then you will be able to see the message shown in the image below.

Step 6: Add the Newly Made Changes to your Repository

The next step is to add the newly made changes to our project repository by running the following command:

git add .

If the changes are successfully added to your project repository, then the system will not display any messages on the terminal, as shown in the image below.

Step 7: Check the Current Status of Git

Now before committing these changes to Git, we will check the current status of Git with the following command:

git status

The current status of our Git project repository is shown in the image below.

Step 8: Commit all the Changes

Now we will commit these changes by running the following command:

git commit –m “Message to be Displayed”

Here, you can replace “Message to be Displayed” with any message of your choice that you want to be displayed with your recent commit.

The results of our recent commit are shown in the image below.

You can easily see from the image shown above that only the .gitignore file has been committed and not the abc.txt file since we wanted to ignore it in our commit. It means that Git has successfully managed to ignore the file that we mentioned in our .gitignore file while committing the changes.

Conclusion

By following today’s tutorial, you can easily add as many files as you want to ignore while doing a commit in your .gitignore files. In this way, you will not only save these files from getting messed up accidentally, but it will also save your resources that will be spent on committing irrelevant files.

About the author

Karim Buzdar

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.