Steps To Ignore Files Using Gitignore 📥📤 In Details

Introduction

 
In my previous article, we have discussed steps for merging git branches using Git Bash and GitHub and steps to create pull requests. Also, we discussed steps to check the log history of branches using GitHub and Git Bash and the difference between the merging process using GitHub and Git Bash.
 
In this article, I will describe the below-mentioned points in detail,
  1. What is Gitignore
  2. Steps to ignore files without committing to git or Github
  3. Steps to use .gitignore file
  4. Customize the .gitignore file of the GitHub repo
  5. Steps to add .gitignore file manually to GitHub
  6. Why should I use .gitignore
Note
Before going through this session, please visit my below-mentioned session related to Git and GitHub,
  1. Introduction To Git
  2. Steps For Configuring Git With Details
  3. Let's Know About Git Add, Git Commit, Git Status And Git Push In Details
  4. Let's Understand How Git Maintains History Of Version Or Commit Ids With Details
  5. Steps To Initialize A Git Repository And Push The Changes To GitHub In Details
  6. Let's Know About Git Branches And How To Implement It
  7. Let's Understand About Git Branches And Its Real Time Uses
  8. Let's Learn About Merging The Git Branches And Create Pull Request In Details

What is Gitignore And Why We Need It

 
The .gitignore file is a text file that commands Git which files or folders to ignore in a project. Let's understand with an example as mentioned below,
 
If I am working on a .NET project and after compilation of code some system-generated class files will be generated. So, These systems generated class files I don't want to commit to the GitHub repository. All the developers connected to this repository should not commit these class files to the GitHub repository.
 
Let's understand with an example,
 
Here I modified a file called MyFile2.cs as shown below,
 
 
Here I will create a new file in the same directory using extension .log called SystemGen.log as shown below,
 
 
Now I will check the status as shown below. You can see the modified file called MyFile2.cs and a new file called SystemGen.log file,
 
 
Here we can see the SystemGen.log file under the untracked file but I don't want to commit this file to the local git repo. So, I want to create another file with extension .gitignore called SystemGen.gitignore because if I put the SystemGen.log file inside SystemGen.gitignore then the git doesn't allow us to commit this SystemGen.log file. Also in git status, this file called SystemGen.log will not be shown. So, I don't want to commit these files whose extensions are .log then in .gitignore file we have to add *.log . Like this .NET project, there is some file we don't want to commit to git. In such cases, we no need to add all these unwanted files to .gitignore. Automatically the GitHub will generate that .gitignore file at the time of repository creation itself.
 

New GitHub Repository With .Gitignore 

 
Here I created a new GitHub repo called SystemGenIgnoreRepo with a proper selection of other options as shown below. Here we need to add .gitignore and the template should be chosen as Java,
 
 
Then after the successful creation of the GitHub repository, the interface is shown below,
 
 
Now go to this .gitignore details as shown below. Here we can see some extensions to which the .gitignore will block those to commit,
 
 
Here I created a new GitHub repo called SystemGenIgnoreVSRepo with a proper selection of other options as shown below. Here we need to add .gitignore and the template should be chosen as VisualStudio,
 
 
Now go to this .gitignore details as shown below. Here we can see some extensions to which the .gitignore will block those to commit,
 
 
Now if I clone this SystemGenIgnoreVSRepo whose .gitignore template is chosen as VisualStudio and I try to commit any changes to this, I will not be able to commit any of these files with extensions as shown above e.g. .log extension file can't be committed to GitHub repository. Let's clone this repository as shown below,
 
 
Now check the .gitignore in SystemGenIgnoreVSRepo git repo using Git Bash as shown below. Here the .gitignore is a hidden file. To show hidden files we use a command called ls -la instead of ls using Git Bash,
 
 
Now I created two files called First.cs and SystemGen.log as shown below. Here if I check status then only First.cs file is shown because .gitignore file doesn't allow this SystemGen.log file is ready to commit status but in the list of files details the SystemGen.log file is shown but not for commit purpose,
 
 
So, here all the developers who are connected to this SystemGenIgnoreVSRepo repo can't commit the log files. Like this log file, we can restrict the file extensions as shown in the above .gitignore VisualStudio template file.
 

Steps To Customize .Gitignore File

 
Here I will customize the .gitignore file that I want to commit all .cs extension files except First.cs file as I created earlier. Here I will add this First.cs file in our .gitignore file and then commit and push its GitHub repository. So, who will pull these changes will get .gitignore file and they can't commit this First.cs to the GitHub repository. Here I added First.cs in .gitignore as shown below,
 
 
Now I will print that .gitignore file content using the command cat .gitignore in Git Bash as shown below. Here we can see the First.cs in .gitignore file,
 
 
Now I check the status. Here we can't see the First.cs and SystemGen.log files as the .log extension and First.cs are added in .gitignore file. So, here only we can see the .gitignore file for commit state,
 
 
Let me create another .cs file called Second.cs and here if we chack status then .gitignore and Second.cs file are allowed for commit to GitHub as shown below,
 
 
Like this, if we want to restrict or ignore specific file extensions called .log, .cs and .txt, etc then those extensions should be added to .gitignore file. Also if we want to ignore one file with a name e.g. First.cs then we need to add this file name in .gitignore file. So that these file names and file extensions are not allowed by .gitignore for commit to GitHub. In case of specific file extension added as *.rsuser and incase of file name added as an only file name as First.cs as shown below,
 
 
In this case, as of now the .gitignore doesn't allow me to commit the First.cs and SystemGen.log file in my local but I want all developers who are connected to this SystemGenIgnoreVSRepo repo also can't commit those files to the GitHub repo. So, for this, I need to commit and push this .gitignore file from my local git to the GitHub repository as shown below. So that when they pull these changes from GitHub then the updated .gitignore file will be available in their local git and they can't commit those files as mentioned in .gitignore file.
 
 
Now commit and push these changes as shown below,
 
 
Now refresh the GitHub repo as check the changes as shown below. Now the .gitignore and Second.cs file is added to GitHub repo.
 
 
Let's check the .gitignore file, there I added one file called First.cs to ignore for further commit by each developer who is connected to this repo as shown below,
 
 
Here we get all Visual Studio-related extensions and file names added in .gitignore file to ignore the further commit to the GitHub repo.
 

Steps To Add .Gitignore To My GitHub Repo Manually 

 
For example, I have created one GitHub repository without adding .gitignore option as shown earlier. But in my local, I need to add .gitignore file so that all Visual Studio-related extensions and file names added in .gitignore file to ignore the further commit to the GitHub repo. Let's do use one URL as shown below,
 
 
Here we can see the VisualStudio, Csharp, and DotnetCore related extensions and file name added for making our own .gitignore file in local as shown below,
 
For VisualStudio,
 
 
For Csharp,
 
 
For DotnetCore,
 
By using the above site URL we can generate extension and file names to create .gitignore file for different programming languages, IDEs. So, we just need to copy this content and create one .gitignore file in our local and paste this content as shown above in .gitignore file. The same content we will get as we got the .gitignore file during the creation of the GitHub repository. So, If we compare both contents then we get the same in both ways. After adding this .gitignore file to local then commit and push these changes to GitHub repository for getting reflected this .gitignore file in GitHub repository. So, those other developers who are connected to this repo can pull these changes and work on the same .gitignore file. This .gitignore file will decide which file extensions or file names should be allowed for commit or not to GitHub repo.
 

Summary

 
In this write-up, we have learned the below details,
  • What is Gitignore
  • Steps to ignore files without committing to git or Github
  • Steps to use .gitignore file
  • Customize the .gitignore file of the GitHub repo
  • Steps to add .gitignore file manually to GitHub
  • Why should I use .gitignore
Thank You & Stay Tuned For More