A Guide to .git/info/exclude for Local Git Ignore Settings

As a developer, you might often create small scripts or files for local development that assist you in your workflow. However, these files are usually not meant to be part of the main codebase and shouldn't be committed to the repository. This is where Git's .git/info/exclude file comes in handy.

What is the .git/info/exclude File?

The .git/info/exclude file is a local Git configuration file that allows you to specify patterns for files that should be ignored by Git on your local repository. This file is similar to the .gitignore file but with a crucial difference: changes to .git/info/exclude are not tracked by Git. This means you can add your local development scripts or other temporary files to this list without worrying about accidentally committing these ignore rules to the shared repository.

Why Use .git/info/exclude?

  • Local Development Flexibility: It allows you to exclude files specific to your development environment without affecting the main codebase.

  • No Accidental Commits: By excluding files locally, you reduce the risk of accidentally committing these files to the main repository.

  • Personal Configuration: Since this file is not shared, each developer on a project can have their own set of excluded files without impacting others.

How to Use .git/info/exclude

Here's a step-by-step guide on how to use the .git/info/exclude file:

  1. Open the exclude file in your preferred text editor. You will see that it has a format similar to .gitignore.

  2. Add Patterns: Add the patterns of the files or directories you want to exclude. For example:

     # Exclude temporary files
     *.tmp
     *.log
    
     # Exclude local scripts
     local-scripts/
     dev-tools/*.sh
    

Example Use Case

Let's say you have a repository with the following structure:

my-repo/
├── .git/
├── src/
│   ├── main.py
│   └── helper.py
├── tests/
│   └── test_main.py
└── dev-scripts/
    ├── generate-data.sh
    └── clean-data.sh

You want to exclude the dev-scripts directory and any log files from being committed. Here's how you can configure the ./.git/info/exclude file:

  1. Open .git/info/exclude.

  2. Add the following lines:

     # Exclude development scripts
     dev-scripts/
    
     # Exclude log files
     *.log
    

    Now, Git will ignore the dev-scripts directory and any .log files, ensuring that they do not get accidentally committed to the repository.

Did you find this article valuable?

Support Pranav Padmasali by becoming a sponsor. Any amount is appreciated!