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.
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.
/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.
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 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.
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.
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.
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.
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.
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.
*.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.
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.
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.
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.