Git Ignore
The .gitignore
file is a text file that specifies patterns of files and directories that Git should ignore when tracking changes in a repository. It is useful for excluding files that are generated by the build process, or that are specific to your local development environment, so that they don't clutter the repository or cause conflicts when working with other contributors.
Here is a detailed explanation of how to use .gitignore
:
- Create a file in the root directory of your repository called
.gitignore
. You can use any text editor to create the file, or you can use thetouch
command in the terminal:
touch .gitignore
- Add patterns to the
.gitignore
file to specify which files and directories you want Git to ignore. Each pattern should be on a separate line.
For example, to ignore all files with the .o
extension, you would add the following line to the .gitignore
file:
*.o
To ignore all files in a particular directory, you can specify the directory name followed by a /
. For example, to ignore all files in the build/
directory, you would add the following line:
build/
- You can also use
!
to negate a pattern and include a file or directory that would otherwise be ignored. For example, to include all files with the.o
extension in thebuild/
directory, you would add the following line:
!build/*.o
Save the
.gitignore
file and commit it to your repository. From now on, Git will ignore the files and directories that you specified in the.gitignore
file.If you want to ignore files that are already being tracked by Git, you will need to untrack them first using the
git rm --cached
command. For example, to untrack all files with the.o
extension, you would run:
git rm --cached *.o
- If you want to include files that are being ignored by Git, you can use the
git add -f
command to force Git to track them. For example, to include all files with the.o
extension, you would run:
git add -f *.o
Here are a few more things you might want to know about using .gitignore
:
You can use wildcards to match multiple files and directories at once. For example, the
*
character will match any number of characters, and the?
character will match any single character.You can use globbing patterns to specify more complex matching rules. For example, the
**
pattern will match any number of directories, and the[abc]
pattern will match any character in the setabc
.You can specify file patterns in
.gitignore
using either relative or absolute paths. Relative paths are relative to the directory containing the.gitignore
file, while absolute paths are based on the root of the repository.You can use the
git check-ignore
command to check which files are being ignored by.gitignore
. For example, to check which files are being ignored in thesrc/
directory, you would run:
git check-ignore src/*
- If you want to ignore files that are specific to your local development environment, you can use the
.git/info/exclude
file. This file works just like.gitignore
, but it is specific to your local repository and will not be committed to the repository. You can use
.gitignore
to ignore files that are generated by the build process, such as object files, executable files, and compiled libraries. This will prevent these files from being committed to the repository and cluttering it up.You can use
.gitignore
to ignore files that contain sensitive information, such as passwords or API keys. This will prevent this information from being accidentally committed to the repository and exposed to other users.You can use
.gitignore
to ignore files that are specific to your local development environment, such as configuration files or temporary files. This will prevent these files from causing conflicts when working with other contributors.You can use
.gitignore
to ignore directories that contain large files or binary files that are not relevant to the project. This will prevent these files from consuming too much space in the repository and slowing down the clone and fetch operations.You can use
.gitignore
to ignore files that are generated by your text editor or IDE, such as backup files or autosave files. This will prevent these files from being committed to the repository and cluttering it up.