Git ignores the file commit method
There are three ways to ignore files you don’t want to commit in Git.
Define the.gitignore file in your Git project
This approach manages Git commit behavior for files in the current folder by defining a.gitignore file in one of the project folders and defining the corresponding ignore rules in the file.
.gitignore files can be committed to a public repository, which shares a set of defined ignore rules for all developers under the project.
In the.gitingore file, specify an ignore rule on each line, following the appropriate syntax. Such as:
- *.log
- *.temp
- /vendor2\
** Specify the exclusion file ** in your Git project Settings
This is just a temporary way to specify the behavior of the project. You need to edit the.git/info/exclude file under the current project and then write the files to it that you want to ignore the commit.
Note that the root of the ignored file specified in this way is the project root.
** Define Git global.gitignore files **
In addition to defining.gitignore files within your project, you can also set up global gits. gitignore files to manage the behavior of all Git projects. This approach is not shared between project developers and is a Git application-level behavior above the project.
This approach also requires the creation of a corresponding.gitignore file, which can be placed anywhere. Git config –global core.excludesfile ~/.gitignore
Ignore rules in.gitignore files
In the.gitignore file, the syntax for each line of the ignore rule is as follows:
Spaces do not match any file and can be used as delimiters, escaped by backslashes
# Start: Identifies comments that can be escaped with a backslash
! Start: Indicates negative, the file will be included again, if the parent directory of the file is excluded, use! It will not be included again. You can escape using backslashes
/ End: Matches only folders and contents in the folder path, but does not match the file
/ start: Matches the file
If a pattern does not contain a slash, it matches the contents relative to the current.gitignore file path, or relative to the project root directory if the pattern is not in the.gitignore file
** matches multi-level directories, which can be at the beginning, middle, or end
? Universal matches a single character
[] Universal matches a single character list
Common matching examples:
Bin / : Ignores the bin folder in the current path. All contents in this folder are ignored. The bin file is not ignored
/bin: ignores the bin file in the root directory
/*.c: ignore cat.c, do not ignore build/cat.c
Obj: Ignores debug/ IO. Obj and does not ignore debug/common/ IO. Obj and tools/debug/ IO
**/foo: Ignore /foo, a/foo, a/b/foo, etc
A /**/b: ignore a/b, a/x/b, a/x/y/b, etc
! /bin/run.sh: the run.sh file in the bin directory is not ignored
*.log: Ignores all.log files
Config.php: ignores the config.php file in the current path
Rule gitignore does not take effect
.gitignore can only ignore files that were not originally tracked, and will be modified if some files have been versioned. Gitignore is invalid.
Delete local cache (change to untrack state), then commit:
1 git rm -r --cached .
2 git add .
3 git commit -m 'update .gitignore'
Copy the code
Gitignore configuration file
# Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ *.egg-info/ .installed.cfg *.egg MANIFEST # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover .hypothesis/ .pytest_cache/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py db.sqlite3 # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder target/ # Jupyter Notebook .ipynb_checkpoints # pyenv .python-version # celery beat schedule file celerybeat-schedule # SageMath parsed files *.sage.py # Environments .env .venv env/ venv/ ENV/ env.bak/ venv.bak/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/ # add .idea/Copy the code