Home  >  Article  >  Development Tools  >  How to set ignore in git

How to set ignore in git

PHPz
PHPzOriginal
2023-04-03 09:17:414976browse

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
Previous article:What is a git account?Next article:What is a git account?