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?
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
- Solution 1: Using .gitattributes File
- Solution 2: Adjusting Git Core Configuration
- Solution 3: Using .gitignore Patterns
- Solution 4: Working Around with Git Commands
- Best Practices and Recommendations
- Troubleshooting Common Issues
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:
# Add to .gitattributes file
* text=auto eol=lf -diff -chmod
Or for more selective approach:
# 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:
# Treat all files as text (ignores modes)
* text
To apply this configuration:
- Create or edit
.gitattributesin your repository root - Add the above patterns
- Commit the
.gitattributesfile to the repository - Run
git add . -uto update the index with the new attributes - Run
git checkout .to apply the changes to your working directory
Note: The
.gitattributesapproach 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:
git config --global core.fileMode false
Repository-Specific Configuration:
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:
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:
# .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:
# 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:
# Add files without considering mode changes
git add --chmod=-x .
Using git diff with --ignore-space-at-eol and other options:
# 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:
- Repository-wide approach: Use
.gitattributeswith-chmodattribute - Global approach: Set
core.fileMode falsein your global Git config - Environment-specific: Use
.gitignorefor development-specific files
For Shared Repositories:
# Add to .gitattributes in repository
* text=auto eol=lf -chmod
Security Considerations:
- Be cautious with
777permissions - they’re generally insecure - Consider
755for directories and644for 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
.gitattributesfile is committed to the repository - Check if
core.fileModeis still set totrue - Run
git add . -uandgit checkout .after changing attributes
“Case sensitivity issues on macOS”:
git config --global core.ignorecase true
git config --global core.precomposeunicode true
“Mixed line endings”:
# .gitattributes
* text=auto eol=lf
“Other developers see mode changes”:
- Ensure everyone has the same
.gitattributesfile - Or configure their Git with the same
core.fileModesetting
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.