GIT IGNORE

Hemashree S

Git Ignore

  • In Git, the term "ignore" refers to marking specific untracked files that Git should disregard, without affecting files that are already being tracked. 
  • Sometimes, you may not want to upload certain files to a Git service like GitHub. In such cases, you can configure Git to ignore those files.
Git's file system is divided into three categories.
  • Tracked
Tracked files are those that have been previously staged or committed.
  • Untracked
Untracked files are files that have not been previously staged or committed.
  • Ignored
Ignored files are those that Git is explicitly instructed to ignore. We need to specify which files Git should disregard.
Ignored files are typically artifacts or machine-generated files that can be derived from the repository's source or should not be committed. Common examples of ignored files include:
  • Dependency caches
  • Compiled code
  •  Build output directories (e.g., /bin, /out, or /target)
  •  Runtime-generated files (e.g., .log,. lock, or .tmp)
  •  Hidden system files (e.g., Thumbs.db or .DS_Store)
  •  Personal IDE configuration files (e.g., .idea/workspace.xml)

Git Ignore files

A '.gitignore' file is used to specify which files or folders Git should ignore. Developers use this file to exclude unnecessary files from being tracked, while Git itself often generates and ignores certain system files, usually hidden. There are various methods to define ignored files, but no explicit command is needed; simply listing the files or patterns in the '.gitignore' file at the repository's root is sufficient.

There is no explicit git ignore command; instead, the .gitignore file must be edited and committed by hand when you have new files that you wish to ignore. The .gitignore files hold patterns that are matched against file names in your repository to determine whether or not they should be ignored.

How to Ignore files Manually

Git does not have a command for ignoring files; instead, there are several methods to specify ignored files, with one of the most common being the '.gitignore' file. Let's explore this with an example.

The .gitignore.files:

The rules for ignoring files are defined in the .gitignore file, which contains patterns and file formats specifying which files should be ignored. You can create multiple .gitignore files in different directories. Let's see how this works with an example:
Step1:
If you don't already have a '.gitignore' file in your directory, create one using the 'touch' or 'cat' command. Here's how you can do it:

Syntax:

$ touch  .gitignore
or
$ cat .gitignore
If you don't already have a '.gitignore' file in your directory, create one using the 'touch' or 'cat' command. Here's how you can do it:

Git Ignore

The command above will create a file named '.gitignore,, which you can then track in your repository. Refer to the image below for more details:


As shown in the image above, a '.gitignore' file has been successfully created for my repository.
Step2: 
To ignore specific files and directories, open the '.gitignore' file and add the names of the files, directories, or patterns you want to exclude. Refer to the image below for an example:


In the file above, the format '*.txt' will ignore all text files in the repository, while 'newfolder/*' will ignore the 'newfolder' directory and its contents. You can also specify individual file names to ignore.
Step3:
To share the '.gitignore' file on Git, you need to commit it. Currently, the '.gitignore' file is in the staging area, and you can verify this by using the `git status` command. 

Output:


Git Ignore

To stage and commit the '.gitignore`'file, use the command below:

Syntax:

$ git add.gitignore
$ git commit -m"ignored directory created."
The command above will commit the '.gitignore' file to Git.

Output:

Git Ignore

Git Ignore


We have now configured Git to ignore a specific pattern of files and a directory.

Rules for putting the pattern in .gitignore file:

The rules for patterns that can be included in the '.gitignore' file are as follows:
  • Git ignores blank lines and lines that start with '#'.
  • Only standard glob patterns are used, and they are applied recursively throughout the entire working tree.
  • Patterns that start with a forward slash (/) are not applied recursively.
  • Patterns that end with a forward slash (/) are used to specify a directory.
  • Patterns can be negated by beginning them with an exclamation point (!).

Global .gitignore:

While multiple '.gitignore' files can be created for a project, Git also supports a global '.gitignore' file that applies to the entire project. To create a global '.gitignore', run the following command in the terminal:

Syntax:

$ git config --globla core.excludesfile ~/.gitignore_global
The command above will create a global '.gitignore' file for the repository.

How to List the Ignored Files?

In Git, you can list ignored files using various commands, with the 'ls command' being one of the most common methods. To list the ignored files, run the 'ls command' as follows:

Syntax:

$ git Is-file -i --exclude-standard
Or
$ git Is-files --ignore --exclude-standard
The command above will list all the ignored files in the repository. In this command, the '-I' option specifies which files to ignore, and the '--exclude-standard' option defines the standard exclude patterns. Refer to the output below:

Git Ignore

The output shows that the 'ls' command lists the ignored files in the repository.






Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send