Git is a version control tool commonly used by programmers. When using Git for development, we may generate some temporary files or compilation result files, and these files do not need to be managed by Git. At this time, you need to use the ignore file to filter out these files to avoid including unnecessary files into version control.
In Git, the ignore file is a text file used to record files or folders filtered by Git. One file or folder per line, wildcards can be used for matching. When executing a Git command, Git will check the gitignore file and automatically filter out the files or folders. This can also prevent these files from being included in version control when submitting code.
The following describes how to use gitignore:
- Use the system default ignore file
Git comes with a default global ignore file, which can be found in Use the following command in Git Bash to view:
$ git config --global core.excludesfile
You can see the path of the default ignore file.
We can use the following command to specify an ignore file in the global configuration.
$ git config --global core.excludesfile ~/.gitignore
Here we specify a file named .gitignore as the Git global ignore file.
- Create project-specific ignore files
In addition to using the global ignore file, we can also specify the ignore file separately for each project, just create .gitignore in the project Just file. We can create an ignore file in the project through the following command:
$ touch .gitignore
After the creation is completed, you can open the .gitignore file with an editor and add the files or folders that need to be filtered to the .gitignore file.
- Write ignore rules
In the .gitignore file, use # to annotate the rules, one rule per line, and support the use of wildcards, including *,?, etc. The following are some common rules:
- Ignore a file or folder: Just write the name of the file or folder.
- Ignore a certain type of file: Use the file suffix name as a rule, such as *.log to ignore all .log files.
- Ignore a folder and the files below it: You can use the folder name and wildcard characters to indicate.
- Exclusion rules: Use ! to indicate that a file or folder is not ignored.
The following is a simple .gitignore file:
#忽略备份文件 *~ #忽略编译结果 /build #忽略特定的文件 .sass-cache/ #不忽略doc目录下的README.md文件 !doc/README.md
- Usage of .gitignore file
In the .gitignore file, if there is For the new ignore rules, you need to execute the following command to tell Git to update the ignore rules.
$ git rm -r --cached . $ git add . $ git commit -m 'update ignore rule'
The function of this command is:
- Delete all information in the local Git repository
- Add the ignore rule back to the Git repository
- Submitting updated ignore rules
Setting ignore files in Git is an important skill, which can help us avoid unnecessary files and code from being added to version control. I hope the above content can be helpful to everyone.
The above is the detailed content of How to set ignore in git. For more information, please follow other related articles on the PHP Chinese website!

This article provides a guide to Git management, covering GUI tools (Sourcetree, GitKraken, etc.), essential commands (git init, git clone, git add, git commit, etc.), branch management best practices (feature branches, pull requests), and merge con

This guide explains how to push a single Git commit to a remote branch. It details using a temporary branch to isolate the commit, pushing this branch to the remote, and then optionally deleting the temporary branch. This method avoids conflicts and

This article addresses common Git commit failures. It details troubleshooting steps for issues like untracked files, unstaged changes, merge conflicts, and pre-commit hooks. Solutions and preventative measures are provided to ensure smoother Git wo

This article explains the difference between Git's commit and push commands. git commit saves changes locally, while git push uploads these committed changes to a remote repository. The article highlights the importance of understanding this distin

This article details methods for viewing Git commit content. It focuses on using git show to display commit messages, author info, and changes (diffs), git log -p for multiple commits' diffs, and cautions against directly checking out commits. Alt

This article explains the distinct roles of git add and git commit in Git. git add stages changes, preparing them for inclusion in the next commit, while git commit saves the staged changes to the repository's history. This two-step process enables

This beginner's guide introduces Git, a version control system. It covers basic commands (init, add, commit, status, log, branch, checkout, merge, push, pull) and resolving merge conflicts. Best practices for efficient Git use, including clear comm

This article introduces Git, a distributed version control system. It highlights Git's advantages over centralized systems, such as offline capabilities and efficient branching/merging for enhanced collaboration. The article also details learning r


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
