A command-line tool that intelligently merges multiple .gitignore files on a per-section basis.
go install github.com/taku-o/gitignore-merge/cmd/gitignore-merge@latestOr build from source:
git clone <repository-url>
cd gitignore-merge
make buildgitignore-merge <file1> <file2> [file3...]- Specify two or more
.gitignorefile paths as arguments - The merge result is printed to stdout
- Use redirection to write to a file
gitignore-merge project.gitignore template.gitignore > .gitignoreComment lines starting with # are recognized as section headers. Section names are compared by stripping all leading # characters and spaces from the first line of the header.
The following are all treated as the same section name "Node":
# Node
## Node
#Node
The first file serves as the base, and subsequent files are merged into it in order.
- Same-name sections: Patterns from subsequent files are added to the matching section in the base
- New sections: Sections not present in the base are appended to the end
Exactly matching patterns within the same section are deduplicated.
When a pattern and its negation conflict (e.g., path and !path), the base file (first file) takes priority.
Examples:
- If the base has
*.log, a subsequent!*.logis ignored - If the base has
!*.log, a subsequent*.logis ignored
file1.gitignore (base):
# Node
node_modules/
dist/
# Logs
*.log
!important.log
# OS
.DS_Store
Thumbs.dbfile2.gitignore:
# Node
node_modules/
build/
# Logs
*.log
!debug.log
# IDE
.vscode/
.idea/gitignore-merge file1.gitignore file2.gitignore# Node
node_modules/
dist/
build/
# Logs
*.log
!important.log
!debug.log
# OS
.DS_Store
Thumbs.db
# IDE
.vscode/
.idea/In this output:
# Nodesection:node_modules/was deduplicated,build/was added# Logssection:*.logwas deduplicated,!debug.logwas added# OSsection: exists only in the base, kept as-is# IDEsection: exists only in file2, appended to the end