How to Create a .Gitignore File
Creating a .gitignore
file is an essential step in managing which files and directories Git should ignore in your repository. Here’s a detailed guide on how to create and configure a .gitignore
file:
Steps to Create a .gitignore
File
Create the
.gitignore
FileYou can create a
.gitignore
file using any text editor or from the command line.Using Command Line:
touch .gitignore
This command will create an empty
.gitignore
file in the current directory.Using Text Editor:
- Open your preferred text editor (e.g., Notepad, VS Code, Sublime Text).
- Create a new file.
- Save the file as `.gitignore` in the root directory of your Git repository.
Add Patterns to the
.gitignore
FileOpen the
.gitignore
file in your text editor and add the file patterns you want Git to ignore. Each pattern should be on a new line.Basic Patterns:
- Ignore all files with a specific extension:
```
*.log
```
- Ignore a specific file:
```
secret.txt
```
- Ignore a specific directory:
```
temp/
```
**Advanced Patterns**:
- Ignore all files in a directory but not a specific file within that directory:
```
temp/*
!temp/important.txt
```
- Ignore files and directories at any level:
```
**/logs/
```
- Ignore files with specific names in any subdirectory:
```
**/debug.log
```
Save and Add the
.gitignore
File to Your RepositoryOnce you’ve added the necessary patterns, save the
.gitignore
file. Then, add it to your Git repository and commit the changes.git add .gitignore git commit -m "Add .gitignore file"
Check the Status
Verify that the files specified in the
.gitignore
file are not being tracked:git status
Files listed in
.gitignore
should not appear in the output if they are not being tracked.
Example .gitignore
File
Here’s an example .gitignore
file that ignores log files, temporary directories, and specific files:
# Ignore log files
*.log
# Ignore temporary directories
temp/
cache/
# Ignore OS-specific files
.DS_Store
Thumbs.db
# Ignore compiled files
*.o
*.pyc
*.class
# Ignore specific files
secret.txt
Special Considerations
Already Tracked Files: If a file was already tracked by Git before adding it to
.gitignore
, you need to remove it from the index. Use:git rm --cached <file>
For example:
git rm --cached temp/important.txt
Global
.gitignore
: You can also create a global.gitignore
file to apply ignore rules across all repositories for your user. This is often used for IDE or OS-specific files:git config --global core.excludesFile ~/.gitignore_global
Then, create
~/.gitignore_global
and add patterns there.
Summary
To create a .gitignore
file:
- Create the
.gitignore
file in your repository’s root directory. - Add patterns to the file to specify which files and directories Git should ignore.
- Save the file, add it to your repository, and commit the changes.
- Verify that the files are correctly ignored using
git status
.
This helps keep your repository clean by ensuring that unnecessary files are not tracked by Git.
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github