Git

How to Ignore a File in Git

Any local Git repository contains three types of files. These are tracked, untracked and ignore files. The files which have been committed before are called tracked files. The files which have not been committed yet are called untracked files. The files which are ignored explicitly are called ignore file. Mainly machine-generated files are ignored files those should not be committed, such as files of compiled code, hidden system file, file with sensitive information, files of output directories, config files, etc. The ignored files are identified by a special file named .gitignore. This file requires editing and commit manually to ignore any file. The files can be ignored from the repository by using the patterns that are stored in the .gitignore file. The way of ignoring files from the repository by creating and using a .gitignore file as shown in this tutorial.

Prerequisites

1. Install GitHub Desktop
GitHub Desktop helps the git user to perform the git-related tasks graphically. You can easily download the latest installer of this application for Ubuntu from github.com. You have to install and configure this application after download to use it. You can also check the tutorial for installing GitHub Desktop on Ubuntu to know the installation process properly.

2. Create a GitHub account
You will require to create a GitHub account to check the output of the commands used in this tutorial.

3. Create a local repository
You have to create a local repository to check the commands used in this tutorial.

Create custom .gitignore patterns

.gitignore file contains the patterns for ignoring the file from the repository. Any repository can contain one or more ignore files on different directories. If the .gitignore file is not created before, go to the local repository folder named send-email and run the following command to create the file.

$ nano .gitignore

Add the following content into the file. Here, /temp/* pattern will ignore all files from the temp folder, /test/* pattern will ignore all files from the test folder, *.docx pattern will ignore all files with the extension *.docx from the repository location, and *.txt pattern will ignore all files with the extension *.txt.

/temp/*
/test/*
*.docx
*.txt

Close the nano editor after saving the file. Run the following command to get the current status information of the git repository.

$ git status

The following output shows that .gitignore is an untracked file of the repository.

Run the following commands to add the .gitignore file in the repository and check the status again.

$ git add .gitignore
$ git status

The following output shows that a .gitignore file has been added to the repository but has not been committed yet.

Run the following command to commit the task done before with a commit message.

$ git commit -m "ignored files and folders created."

The output shows that one file is changed, and some insertions have been done.

Run the following command to find out the pattern of the .gitignore file that will ignore the test.txt file.

$ git check-ignore -v test.txt

The following output shows that the test.txt file will be ignored for the pattern defined in line number 4 of the .gitignore file.

Create a folder named temp in the current repository folder and add a file named temporary.py under the temp folder. Now, run the following command to find out the pattern of the .gitignore file that will ignore the temporary.py file.

$ git check-ignore -v temp/*

The following output shows that temp/temporary.py will be ignored for the pattern defined in line number 1 of the .gitignore file.

Global .gitignore patterns

If you want to apply some ignore patterns for all the local drive repositories, you have to define the patterns in a global ~/.gitignore file. Run the following command to add a setting for the global ~/.gitignore file.

$ git config --global core.excludesFile ~/.gitignore

The following output will appear if the above command executes properly.

Open ~/.gitignore file using any editor to add global patterns for all repositories of the local drive. Here, the nano editor is used.  Run the following command to open the file.

$ nano ~/.gitignore

Add the following lines to the files, save and close the file. According to these patterns, all files with the name, test with any extension will be ignored, and all files with the extension .log will be ignored.

test.*
*.log

test.py, test.txt, sys.log, data.log, and index.php files have been created in the local repository named read-file. Only the index.php file can be tracked according to the patterns defined in the ~/.gitignore file. Run the following command to check the status of the repository.

$ git status

The following output will appear after executing the above command. The output shows that there is only one untracked file, and the other four files are ignored based on the patterns.

Run the following command to find out the ~/.gitignore file pattern that has ignored the  data.log file.

$ git check-ignore -v data.log

The following output shows that the file has ignored the pattern defined in line number 2 of the ~/.gitignore file, and the pattern is *.log. The sys.log file has been ignored for the same pattern.

Run the following command to find out the ~/.gitignore file pattern that has ignored the  test.py file.

$ git check-ignore -v test.py

The following output shows that the file has ignored the pattern defined in line number 1 of the ~/.gitignore file, and the pattern is test.*. The test.py file has been ignored for the same pattern.

Conclusion

The way of defining patterns in the global ~/.gitignore file for ignoring files of all repositories of the local drive and the way of defining patterns in the .gitignore file for ignoring files of the particular repository have shown in this tutorial by using two demo repositories. The uses of the GitHub desktop have not shown here. You can use this application if you want to do the above tasks using the graphical user interface.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.