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"