Causes and Workaround Solutions for git commit Permission denied on macOS

Tadashi Shigeoka ·  Wed, September 24, 2025

Have you ever encountered the following error message when trying to execute git commit on macOS?

$ git commit
 
error: could not create temporary file: Permission denied
fatal: failed to write commit object

This indicates that Git is failing to create a temporary file needed to write the commit object due to permission issues.

In this article, I’ll introduce troubleshooting steps to resolve this Permission denied error, starting with the safest approaches.

1. Isolate Whether Git Hooks Are the Cause

First, let’s determine whether the problem is occurring in Git itself or is caused by scripts executed before commit (Git hooks).

Execute the following command to temporarily disable Git hooks and attempt a commit:

git commit --no-verify -m "test commit to check hooks"

If this command successfully commits, the cause lies in the Git hooks.

Check the hook scripts such as pre-commit in your repository’s .git/hooks/ directory. There’s likely a process within the script that generates temporary files (for example, linters or formatting tools creating temporary files), and the permissions for the target directory (/tmp, $TMPDIR, tmp/ within the repository, etc.) may be incorrectly configured. Review the relevant scripts and the configuration of the tools they use.

2. Check macOS Temporary Directory ($TMPDIR)

If the error persists even with --no-verify, the issue may be with the temporary directory that Git uses.

For security reasons, macOS creates unique temporary directories for each user under /var/folders/ and sets the path in the environment variable $TMPDIR. If the permissions for this directory become corrupted for some reason, or if the directory itself becomes damaged, Git cannot create temporary files and will error out.

First, let’s check the current $TMPDIR path and test if we can write to it:

# Check the $TMPDIR path
echo $TMPDIR
# A path like /var/folders/xx/yyyyyyyyzzzzzzzzzzzzzzzz/T/ will be displayed
 
# Test writing
touch "$TMPDIR/testfile"

If you get a Permission denied error with the touch command, it’s highly likely that $TMPDIR is corrupted.

Workaround: env TMPDIR=/private/tmp

As a quick fix, you can explicitly specify /private/tmp (the symbolic link target of /tmp), which is a more common temporary directory, only during Git operations to successfully complete your commit:

env TMPDIR=/private/tmp git commit -m "commit with a temporary fix"

Permanent Solutions

Since typing env command every time is cumbersome, let’s set up a permanent solution for this workaround.

a) Override TMPDIR in Shell Configuration File (Large Impact Range)

Add the following line to your shell’s configuration file (e.g., ~/.zshrc, ~/.bash_profile). This will set the temporary directory used throughout your terminal session to /private/tmp.

export TMPDIR=/private/tmp

After saving the file, run source ~/.zshrc to apply the changes or restart your terminal.

b) Set Up a Git Command Alias (Small Impact Range)

If you want to limit the impact to just Git commands, setting up an alias is recommended. Similarly, add the following to your shell configuration file:

alias git='env TMPDIR=/private/tmp git'

This will automatically prepend env TMPDIR=/private/tmp when you type git.

Summary

While the Permission denied error during git commit can be alarming, it can almost always be resolved by following the steps above. First suspect the hooks, then check the macOS-specific $TMPDIR issue.

That’s all from the Gemba, where we’ve temporarily resolved the git commit Permission denied error.