search
HomeDevelopment ToolsgitHow to set ignore in git
How to set ignore in gitApr 03, 2023 am 09:17 AM

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:

  1. 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.

  1. 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.

  1. 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
  1. 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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to use git management tools for complete usage of git management toolsHow to use git management tools for complete usage of git management toolsMar 06, 2025 pm 01:32 PM

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

How to push the specified commitHow to push the specified commitMar 06, 2025 pm 01:39 PM

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

How to solve the failure of git commit submissionHow to solve the failure of git commit submissionMar 06, 2025 pm 01:38 PM

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

The difference between commit and push of gitThe difference between commit and push of gitMar 06, 2025 pm 01:37 PM

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

How to view commit contentsHow to view commit contentsMar 06, 2025 pm 01:41 PM

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

The difference between add and commit of gitThe difference between add and commit of gitMar 06, 2025 pm 01:35 PM

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

How to use git management tools Tutorial for using git management tools for beginnersHow to use git management tools Tutorial for using git management tools for beginnersMar 06, 2025 pm 01:33 PM

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

What is git code management tool? What is git code management tool?What is git code management tool? What is git code management tool?Mar 06, 2025 pm 01:31 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

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

Dreamweaver Mac version

Visual web development tools