Steps To Ignore Files Using Gitignore πŸ“₯πŸ“€ In Details

Introduction

In my previous article, we 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 Their Real-Time Uses
  8. Let's Learn About Merging The Git Branches And Creating Pull Requests 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,

 MyFile2

Here I will create a new file in the same directory using the extension .log called SystemGen.log as shown below,

SystemGen

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,

Status

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 the 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. git ignore file we have to add *.log. Like this .NET project, there are some files we don't want to commit to Git. In such cases, we do not need to add all these unwanted files. git ignore. Automatically GitHub will generate that. git ignore 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,

GitHub repo

Then after the successful creation of the GitHub repository, the interface is shown below.

 GitHub repository

Now go to this .gitignore details as shown below. Here we can see some extensions to which the .gitignore will block those from committing,

Gitignore details

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.

SystemGenIgnore

Now go to this .gitignore details as shown below. Here we can see some extensions to which the .gitignore will block those from committing.

Extensions

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.

Gitignore template

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.

Git Bash

Now I created two files called First. cs and SystemGen.log as shown below. Here if I check the status then only First. cs file is shown because the .gitignore file doesn't allow this SystemGen.log file to be ready to commit status but in the list of file details the SystemGen.log file is shown but not for commit purposes.

Files details

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 the 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, whoever pulls these changes will get a .gitignore file and they can't commit this First. cs to the GitHub repository. Here I added First. cs in .gitignore as shown below,

Gitignore File

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 the .gitignore file.

Gitignore file content

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 the .gitignore file. So, here only we can see the .gitignore file for the commit state.

Commit state

Let me create another .cs file called Second. cs and here if we check the status then .gitignore and Second. cs file are allowed for commit to GitHub as shown below.

 GitHub

For this, if we want to restrict or ignore specific file extensions called .log, .cs .txt, etc then those extensions should be added to the .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. git ignore file. So these file names and file extensions are not allowed by .gitignore for committing to GitHub. In the case of a specific file extension added as *.rsuser and in case of a file name added as only the file name as First. cs as shown below.

Ignore specific file

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 the .gitignore file.

Local git

Now commit and push these changes as shown below.

Now commit

Now refresh the GitHub repo and check the changes as shown below. Now the .gitignore and Second. cs file is added to the GitHub repo.

Now refresh

Let's check the .gitignore file, there I added one file called First. cs to ignore further commits by each developer who is connected to this repo as shown below.

Further commit

Here we get all Visual Studio-related extensions and file names added in the .gitignore file to ignore further commits to the GitHub repo.

Steps To Add.Gitignore To My GitHub Repo Manually

For example, I have created one GitHub repository without adding the .gitignore option as shown earlier. But in my local, I need to add a .gitignore file so that all Visual Studio-related extensions and file names are added in the .gitignore file to ignore further commits to the GitHub repo. Let's use one URL as shown below.

GitHub Repo Manually

Here we can see the VisualStudio, Csharp, and DotnetCore-related extensions and file names added for making our own .gitignore file locally as shown below.

For VisualStudio

VisualStudio

For Csharp

Csharp

For DotnetCore

 DotnetCore

By using the above site URL we can generate extensions and file names to create .gitignore files for different programming languages, IDEs. So, we just need to copy this content and create one .gitignore file locally and paste this content as shown above in the .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 the GitHub repository to reflect this .gitignore file in the 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 the 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


Similar Articles