When you make commits in a git repository, you choose which files to stage and commit by using git add FILENAME
.You can also use git add .
to stage all files in the current directory, and then git commit
.
But what if there are some files that you never want to commit?
That’s where a .gitignore
file comes in handy. It lets Git know that it should ignore certain files or folders and not track them.
Ignored files are tracked in a special file named .gitignore
.
There is no explicit git ignore command: instead the .gitignore
file must be edited and committed by hand when you have new files that you wish to ignore.
What is a .gitignore
file?
A .gitignore
file is a plain text file where each line contains a pattern for files/directories to ignore.
Generally, A .gitignore
file is placed in the root folder of the repository. The patterns in the files are relative to the location of the .gitignore
file.
For example. Here is the file tree of my git repository.
git repository
- .DS_Store
- data/
- data1.txt
- data2.txt
- file1.py
- file2.py
- log1.log
- log2.log
- .git/
- .gitignore
And here is the content of a .gitignore
file.
.DS_Store
/Data/
*.log
Patterns of ignoring files/folders.
.gitignore
files contain patterns that are matched against file names in your repository to determine whether or not they should be ignored.
1. Ignore a single file
The easiest pattern is a literal file name, for example:
.DS_Store
2. Ignore an entire directory
You can ignore entire directories, just by including their paths and putting a /
on the end:
Data/
3. Ignore a file type
The *
matches 0 or more characters (except the /
). So, for example, *.log
matches any file ending with the .log
extension.
*.log
4. Add an exception
You can add an exception (using !
) to the preceding rule to track a specific
file !package.json
Ref:
.gitignore file — ignoring files in Git | Atlassian Git Tutorial
https://www.atlassian.com/git/tutorials/saving-changes/gitignore
Ignore files in your Git repo — Azure Repos | Microsoft Docs
https://docs.microsoft.com/en-us/azure/devops/repos/git/ignore-files?view=azure-devops&tabs=command-line
https://www.pluralsight.com/guides/how-to-use-gitignore-file