[Git] Error: fatal: unable to create temporary file — Permission denied — How to Fix It
Summary
The Git error fatal: unable to create temporary file: Permission denied indicates that Git cannot write new data to the repository’s internal structures or the system’s temporary directory. This usually stems from restricted permissions, read-only mounts, or full disks. To resolve it, verify your user’s write access to the .git directory and ensure there’s enough disk space or correct ownership of the repository path.
Context
During normal operations like committing, fetching, rebasing, or garbage collecting, Git constantly creates temporary files in both the repository’s .git directory and the system’s temp folder. These files track in-progress data and lock repository states to prevent corruption. When Git fails to write, it immediately halts to avoid inconsistencies. This issue appears across all operating systems — Windows, macOS, Linux, WSL, and Docker — and is common when repositories are stored under protected folders, mounted drives, or shared environments with mismatched ownership or restrictive ACLs. The error can also manifest if leftover index.lock or temporary files block new write attempts.
Probable Cause
- Insufficient write permissions for the repository or its
.gitdirectory. - File system mounted as read-only (e.g., network drives, external media, or container volumes).
- Repository placed under system-protected paths such as
Program Files,/root, or/media. - Temporary directories (
/tmpor%TEMP%) inaccessible or full. - Corrupted or stale
.lockfiles preventing new writes.
Quick Fix
Follow these diagnostic and corrective steps:
- Verify where you’re executing Git. Avoid running commands from inside
.gitor protected folders. - Check directory permissions:
ls -ld .git # Linux/macOS
or inspect “Properties → Security” on Windows.
- Fix permissions (Linux/macOS):
sudo chown -R $(whoami) .git
chmod -R u+rw .git
- Ensure write access in containers or WSL mounts — remount if necessary with user write permissions.
- Check free disk space:
df -h
- Clean up Git temp files and remove stale locks:
git gc --prune=now
rm -f .git/index.lock
- Retry your original command (commit, fetch, or rebase). If the issue persists, clone the repository to a writable location (e.g.,
C:\Users\YourName\repos\project).
Full Example
A developer working in WSL tries to commit changes and receives:
fatal: unable to create temporary file: Permission denied
error: cannot write temporary index file
They check ownership of the repository:
ls -ld .git
drwxr-xr-x 3 root root 4096 Oct 25 .git
Since the folder is owned by root, Git cannot write as the normal user. They fix it by changing ownership:
sudo chown -R $(whoami) .git
chmod -R u+rw .git
git commit -am "Fix permissions and retry commit"
The commit now succeeds, and further operations like git fetch or git gc run normally.
Pitfalls & Debug
- Symptom: Git fails in WSL mounts → Fix: Re-clone into
/home/instead of/mnt/. - Symptom: “Permission denied” in
.git/index.lock→ Fix: Remove it manually if no Git process is active. - Symptom: Git operations fail only on one branch → Fix: Run
git fsckto check repository integrity. - Symptom: Disk full errors → Fix: Free up space or move repo to a larger partition.
- Symptom: Persistent denial after permission fix → Fix: Check filesystem mount options or ACLs.
Validation & Next Steps
Test whether Git can now write temporary files:
touch testfile
git status
If these commands succeed, permissions are fixed. To prevent recurrence, ensure repositories live in user-owned paths and periodically run:
git gc --prune=now
Avoid working from system or root-protected directories to maintain Git’s ability to write metadata safely.
Sources
Git documentation — git
Stack Overflow — “fatal: unable to create temporary file: Permission denied” threads
Atlassian Git tutorials — fixing repository access and lock issues
Git mailing list archives — discussions on file system permission errors