How can I restore the permissions of files and directories within Git if they have been modified?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Permission drift in a Git repository is confusing because Git tracks only limited mode information. For normal files, Git mainly stores executable-bit changes, while directory permissions are managed by the filesystem and not fully versioned. Restoring permissions correctly requires combining Git commands with OS-level normalization.
What Git Tracks and What It Does Not
Git mode tracking for common file types is narrow:
- regular non-executable file mode
- regular executable file mode
- symlink mode
Git does not preserve detailed directory permission bits in commit history. That means directory mode restoration cannot come from git checkout or git restore alone.
Restore Tracked File Modes from Commit
For tracked files, restore content and executable-bit state from HEAD.
To restore all tracked files in working tree:
Legacy equivalent:
These commands are safe first steps when accidental mode changes are mixed with content edits.
Inspect Permission Changes Explicitly
Before and after restoration, inspect mode-only changes.
Look for mode transitions such as 100644 and 100755. This isolates executable-bit drift without scanning full file diffs.
Set Executable Policy Intentionally
If certain scripts must remain executable, enforce that state in Git index.
Then commit the normalization so future clones inherit expected executable flags.
Normalize Directory Permissions with OS Tools
Because Git does not track directory permission bits, use filesystem commands to reset directories and files.
After broad normalization, reapply executable bits for known scripts:
Always review mode summary before committing.
Practical End-to-End Cleanup Workflow
A repeatable sequence helps avoid mistakes:
This separates Git-tracked mode restoration from filesystem-wide normalization.
Cross-Platform Considerations
On some systems, especially Windows environments, mode behavior may differ depending on filesystem and Git settings. Check:
If set to false, Git may ignore local mode changes. That can reduce noise locally but may hide executable-bit drift that matters for Linux deploy targets.
Team Practices to Prevent Repeat Drift
Preventive policy reduces repeated cleanup work:
- document which files must be executable
- avoid blanket
chmodat repository root unless planned - review mode-only diffs in pull requests
- add CI guard for unexpected mode changes
Example CI guard:
This gives early feedback before mode churn reaches release branches.
Recovery from Mistakes
If you accidentally committed unwanted mode changes, you can still recover by creating a follow-up normalization commit. For shared branches, avoid rewriting history unless team policy allows it.
For local accidental changes, stash or backup branch before bulk permission commands to keep a safe rollback point.
Common Pitfalls
- Expecting Git to restore detailed directory permission bits from history.
- Running broad
chmodcommands and forgetting to reapply executable scripts. - Committing permission noise without checking
git diff --summary. - Using inconsistent executable policy across teams and platforms.
- Assuming
core.fileModebehavior is identical in all environments.
Summary
- Git mainly tracks executable-bit state for files, not full permission metadata.
- Use
git restorefor tracked file-mode restoration. - Use
git update-index --chmodto enforce executable policy explicitly. - Normalize directory permissions with filesystem tools when needed.
- Add team conventions and CI checks to prevent recurring permission drift.
Related reading
- How can I revert a single file to a previous version?
- How can I revert a single file to a previous version?
- How can I revert multiple Git commits?
- How can I revert uncommitted changes including files and folders?
- How can I revert uncommitted changes including files and folders?
- How can I rollback a git repository to a specific commit?
- How can I save username and password in Git?
- How can I search Git branches for a file or directory?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.