×

Git Ignore

Git Ignore: In general terms, a gitignore file is used to specify the intentionally untracked files that Git shout ignore. It doesn't affect the other files as they are already tracked by Git.

Git Ignore

Every line in a gitignore file possesses some specific pattern. When Git tried to figure out which path to ignore, it closely checks the pattern present in the gitignore file from various sources with some order of precedence. For example, high to low or from first to last depending on the matching patterns that come into view.

There are some consequences when we don't want to send files to version control systems like GitHub. Thus, we expect those files to get ignored by GitHub and we specify those files as gitignore.

There are three types of files associated with gitignore. They are:

*Tracked: Tracked files are such files that are previously staged or committed.

*Untracked: Untracked files are such files that are not previously staged or committed.

*Ignored: Ignored files are such files that are explicitly ignored by git. We have to tell git to ignore such files.

As we have already discussed that gitignore works by recognizing certain patterns. Therefore, let’s discover what patterns prevail:

* Patterns are figured out through the command line that supports them.

Pattern also seeks a match related to the gitignore files' location because our repository always includes gitignore files containing several patterns prevailing from the last build.

* Patterns are also read from the configuration of the core.excludesFiles.

* While cloning, patterns go into the gitignore files in a distributed version control system.

Types of rules for gitignore

Personal gitignore rules

There is a specified location in the file where personal ignore patterns are recognized. The file may have the location .git/info/exclude. These files are neither versioned nor distributed. Therefore, this location is considered beneficial to produce certain patterns that can be recognized later. For instance, consider that we have a custom logging setup. We can easily add .git/info/exclude files in the working directory and prevent them from being accidentally committed to the repository.

Global gitignore rules

As discussed, patterns in gitignore files can be defined globally for all the existing repositories on your local setup by just setting up the core.excludesFile property. We need to manually create these files and configure them by the following command:

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

Shared gitignore rules

Usually, gitignore rules are defined in the root of our repository along with definitions of multiple gitignore files. These files contain the relative data of their respective working directory. Thus, there is a simple convention of defining a single gitignore file in the root and check whether it is versioned like any other files present in our working repository. After it is checked, it can be shared by pushing the files as this will benefit other users who have been working on the same workspace as one team.

Listing the ignored files

To enlist the gitignore files, we have some unique commands. Those commands are:

$ git ls-files -i --exclude-standard  

Or

$ git ls-files --ignore --exclude-standard 

These commands will list out all the ignored files. Here, --ignore or -i mean the same and the --exclude-standard defines the pattern present in the gitignore file. The ls command is used to list in the standard presentable way. The ignored files will be enlisted something like this:

$ git ls-files -i --exclude-standard 

newfile.txt

newfile.txt

newfile.txt

Stashing gitignore files

We have come across the concept of stashing, which means that stashing only changes the files that are tracked by Git. Stashing is also used to reshelving and reverting the changes.  Stashing all the files associated with gitignore can be done with the command:

$ git stash --ignore--all

Debugging the gitignore files

It is quite difficult to understand gitignore patterns manually since they are very complicated and spread across different files which make the user tough to track down and debug. Therefore, before debugging, we check the pattern which is creating errors by the command:

$ git check-ignore -v  (or --verbose).

And for debugging, we type:

$ git check-ignore -v debug.log .gitignore:3:*.log debug.log

Committing the gitignore files

Committing is a familiar term that we might have come across. There may arise a situation with gitignore files that we need to forcefully commit them. To do that, we use the command:

$ cat .gitignore *.log $ git add -f debug.log $ git commit -m "For adding debug.log"


Related Topics

Git Upstream and Downstream

Git Upstream and Downstream Upstream and downstream in Git are usually defined with the context of a repository. In general terms, upstream refers to the location through which we clone the...

4 minutes read.

Git diff

Git diff Overview The diff command in Git is generally a function that inputs two data sets and rolls out the changes made between them. It is a multitasking tool that...

3 minutes read.

Git Prune

Git Prune: The prune command in Git is an integrated utility tool that is primarily used for cleaning up the unreachable or "orphaned" objects present in Git. The concept of...

4 minutes read.

Git logs

Git logs: The main function of logs in Git is to show commit logs. It is generally used to list commits that are associated with the parent links from the...

4 minutes read.

How to Install Git on Windows?

How to download Git and Installation Guides for Windows step by Step? To carry out operations with Git, we need to install it on our computer. Even if we have already...

3 minutes read.

wxpython - Major Classes

wxpython - Major Classes The original wxWidgets is written in the C++ programming language and it has a huge library for classes. The classes are used for building GUI using wxWidgets...

9 minutes read.

Git submodule

Git submodule Overview Git submodules allow you to keep a git repository as a subdirectory of another git repository. Git submodules are simply a reference to another repository at a particular...

4 minutes read.

Python wx module

Python wx module Introduction Python provides us a wxpython module which we usually call as Python wx module. The wxpython module is allow us to create a very high functional GUI...

4 minutes read.

Git gc

Git gc Overview The abbreviation of gc is 'Garbage Collection'. In Git, there is a command used to maintain the repository which is git gc. When you execute this command, you...

4 minutes read.

Git Version Control Comparison

Version Control Comparison: Choosing the best version control framework is quite a difficult task for most of the users because of available plenty options. While choosing the right version control...

4 minutes read.

Git Workflow

Git Workflow: Being one of the best version control systems for your projects, Git is not just about handling projects but also to collaborate and release management. It gives you...

3 minutes read.

Git reflog

Git reflog: The Git reflog or Reference Logs is a rare command that is used to record logs when certain branches along with references were once updated in the local...

4 minutes read.

Git Tutorial

Git tutorial Overview: Git was created by Linus Torvalds in 2005. Today, Git is a Microsoft-owned company after the approval of the CEO of the EU. In this tutorial, we will...

4 minutes read.

Git Cherry-Pick

Git Cherry-Pick In Git, the cherry-pick command is used to make the target commit changes and place them on the HEAD of the currently checked-out branch. It is done to either...

3 minutes read.

Git Bash

Git Bash In a version control system, Git can be considered as a bunch of unique command-line service provider which has the power to serve almost every requirement to your project...

3 minutes read.

Git Squash

Git Squash How to Squash? The squash command in Git is used to suppress or subdue the previous commits into one unit. It is quite interesting to note that squash is not...

3 minutes read.

Git Reset

Git Reset: The reset command in Git is quite complex and a great tool to undo changes. There are some strict invocations or forms of undoing changes in Git. These...

4 minutes read.

Git Ignore

Git Ignore: In general terms, a gitignore file is used to specify the intentionally untracked files that Git shout ignore. It doesn't affect the other files as they are already...

3 minutes read.

Terminologies in Git

Terminologies in Git Git is a tool that covered vast terminology and jargon, which can often be difficult for new users or learners. Let's have a look at the commonly used...

4 minutes read.

Git daemon

Git daemon Overview In a version control system, the daemon is a very simple server for the Git repository. In technical terms, it is a simple TCP command that listens to...

4 minutes read.