Root Cause and Permanent Solution for Permission denied Error on git commit in macOS
While developing on macOS, I ran git commit
and suddenly encountered the following error message:
$ git commit
error: could not create temporary file: Permission denied
fatal: failed to write commit object
This error indicates that Git failed to create a temporary file for writing commit messages and other data due to insufficient permissions.
Actually, I’ve previously written about this issue in Causes and Workaround Solutions for git commit Permission denied on macOS, but this article is a follow-up. I was able to identify the root cause and implement a permanent solution, which I’d like to share with you.
After further investigation, I found that the root cause was in the TMPDIR
environment variable.
Due to some operation (likely executing a command with sudo
), the TMPDIR
used throughout the Terminal session, and consequently the entire GUI session, had been changed to point to root user’s temporary directory instead of the regular user’s directory.
TMPDIR
is a critical environment variable that many applications reference for storing temporary files. When this path is owned by root, Git running with regular user permissions cannot write to it, resulting in the Permission denied
error.
The solution is to reset this TMPDIR
to the correct path for the current login user. The procedure is quite simple.
First, let’s reset the environment variable used across the entire GUI session. Execute the following command in Terminal:
launchctl setenv TMPDIR "$(getconf DARWIN_USER_TEMP_DIR)"
Let’s break down what this command does:
getconf DARWIN_USER_TEMP_DIR
: A macOS standard utility that retrieves the correct path to the temporary directory that the current user should use.launchctl setenv <variable> <value>
: A command that sets environment variables in a broader scope, including macOS GUI applications.In essence, this single line performs “resetting the TMPDIR
environment variable for the entire GUI session to the correct path for the current user.”
To apply the environment variable set with launchctl
to the currently open Terminal app, you need to completely quit Terminal (Cmd + Q
) and restart it.
After restarting, it’s a good idea to verify that TMPDIR
is set to the correct path with the following command:
echo $TMPDIR
You should likely see a user-specific path like /var/folders/...
.
With this setup, running git commit
should now complete normally without any errors.
The Permission denied
error in git commit
can be caused by unintended changes to the TMPDIR
environment variable. If this occurs after operations using sudo
, it’s worth suspecting this as the cause.
This solution addresses the root cause rather than being a temporary workaround. I hope this helps others struggling with similar issues.
That’s all from the Gemba, where we’ve just solved the root cause of the Permission denied issue when running git commit on macOS.