NeuroAgent

Git Ignore Chmod Changes: Complete Guide

Learn how to configure Git to ignore file permission changes during development. Complete guide using .gitattributes, core.fileMode settings, and workarounds.

Question

How do I make Git ignore file mode (chmod) changes?

I have a project where I need to change file permissions to 777 during development, but these changes shouldn’t be tracked in the main repository. When I run chmod -R 777 ., Git detects all files as modified. Is there a way to configure Git to ignore permission changes to files?

NeuroAgent

You can configure Git to ignore file mode (chmod) changes in several ways. The most common methods include using the .gitattributes file or adjusting Git’s core configuration settings. Here are the effective solutions to prevent Git from tracking permission changes while keeping your development workflow intact.

Contents

Understanding Git’s File Mode Tracking

Git tracks executable permissions (file modes) as part of version control. When you use chmod -R 777 ., Git detects these changes because executable permissions are considered part of the file’s content signature. This behavior is by design - Git needs to know when a file should be executable in different environments.

The issue occurs because Git considers executable permissions (mode 755 vs 644) as significant changes that should be tracked across different systems. However, for development purposes, you often want to standardize permissions without committing them to the repository.

Solution 1: Using .gitattributes File

The most robust solution is to create or modify your .gitattributes file to tell Git to treat all files as text or to ignore mode changes:

bash
# Add to .gitattributes file
* text=auto eol=lf -diff -chmod

Or for more selective approach:

bash
# Ignore executable bit for all files
* -chmod

# Or specific patterns
*.js -chmod
*.py -chmod
*.sh -chmod

The -chmod attribute specifically tells Git to ignore mode changes. You can also use:

bash
# Treat all files as text (ignores modes)
* text

To apply this configuration:

  1. Create or edit .gitattributes in your repository root
  2. Add the above patterns
  3. Commit the .gitattributes file to the repository
  4. Run git add . -u to update the index with the new attributes
  5. Run git checkout . to apply the changes to your working directory

Note: The .gitattributes approach is repository-specific and will be shared across all developers on the project.

Solution 2: Adjusting Git Core Configuration

You can configure Git to ignore file mode changes globally or per-repository:

Global Configuration:

bash
git config --global core.fileMode false

Repository-Specific Configuration:

bash
git config core.fileMode false

The core.fileMode setting tells Git whether to care about file mode changes:

  • false = ignore mode changes (only track content changes)
  • true = track mode changes (default behavior)

Important Considerations:

  • This setting is case-sensitive on case-insensitive filesystems (like macOS by default)
  • It affects all files in the repository
  • Other developers will need the same setting if they work on your repository

For case-insensitive filesystems (macOS, Windows), you might also need:

bash
git config --global core.ignorecase true

Solution 3: Using .gitignore Patterns

While .gitignore typically ignores untracked files, you can use it in combination with Git attributes to handle tracked files:

bash
# .gitignore - doesn't work for mode changes alone, but combined with attributes
# Use with .gitattributes for best results

.gitignore patterns don’t directly affect mode changes for already tracked files, which is why combining it with .gitattributes (as shown in Solution 1) is more effective.

Solution 4: Working Around with Git Commands

If you need to temporarily ignore mode changes without permanent configuration:

Using git update-index:

bash
# Ignore mode changes for all files
git update-index --chmod=+x *

# Or specific files
git update-index --chmod=+x script.sh

Using git add with --chmod option:

bash
# Add files without considering mode changes
git add --chmod=-x .

Using git diff with --ignore-space-at-eol and other options:

bash
# See only content changes, ignore mode changes
git diff --ignore-space-at-eol

These methods are temporary and need to be reapplied after certain operations.

Best Practices and Recommendations

For Development Environments:

  1. Repository-wide approach: Use .gitattributes with -chmod attribute
  2. Global approach: Set core.fileMode false in your global Git config
  3. Environment-specific: Use .gitignore for development-specific files

For Shared Repositories:

bash
# Add to .gitattributes in repository
* text=auto eol=lf -chmod

Security Considerations:

  • Be cautious with 777 permissions - they’re generally insecure
  • Consider 755 for directories and 644 for files as safer alternatives
  • Document your permission strategy in the project README

Example .gitattributes File:

# Set default behavior
* text=auto eol=lf -chmod

# Specific handling
*.sh text eol=lf
*.py text eol=lf
*.js text eol=lf -chmod

Troubleshooting Common Issues

“Mode changes still appear”:

  • Verify your .gitattributes file is committed to the repository
  • Check if core.fileMode is still set to true
  • Run git add . -u and git checkout . after changing attributes

“Case sensitivity issues on macOS”:

bash
git config --global core.ignorecase true
git config --global core.precomposeunicode true

“Mixed line endings”:

bash
# .gitattributes
* text=auto eol=lf

“Other developers see mode changes”:

  • Ensure everyone has the same .gitattributes file
  • Or configure their Git with the same core.fileMode setting

The most reliable approach for shared repositories is using the .gitattributes file, while the global configuration works best for personal development environments. Choose the method that best fits your team’s workflow and security requirements.