This error occurs when Git cannot create a directory due to insufficient filesystem permissions. The fix involves checking directory ownership, adjusting permissions, or initializing the repository in a location where you have write access.
When you run `git init`, Git attempts to create a `.git` directory (with subdirectories for objects, refs, hooks, etc.) in your specified location. If the operating system denies this directory creation, you get the "cannot mkdir: Permission denied" error. This error is entirely about filesystem permissions—Git itself has no special permission system. It relies on the underlying OS to grant or deny access to directories. The error means your current user account doesn't have write permission to the parent directory where you're trying to initialize the repository. Common scenarios include trying to create a repository in system directories (like `/var`, `/etc`, or `C:\Program Files`), directories owned by root or another user, or locations protected by security policies like SELinux or Windows UAC.
First, verify who owns the directory and what permissions are set:
On Linux/macOS:
ls -la .Look at the output—the third and fourth columns show owner and group:
drwxr-xr-x 2 root root 4096 Jan 15 10:00 .If it shows root root and you're not root, that's your problem.
On Windows (Git Bash):
pwdIf you're at / or /c/Program Files, navigate elsewhere:
cd ~The simplest solution is to create your repository in your home directory or a subdirectory you own:
Linux/macOS:
cd ~
mkdir -p projects/my-repo
cd projects/my-repo
git initWindows (Git Bash):
cd /c/Users/$USERNAME
mkdir -p projects/my-repo
cd projects/my-repo
git initWindows (Command Prompt/PowerShell):
cd %USERPROFILE%
mkdir projects\my-repo
cd projects\my-repo
git initIf you need to use a specific directory and you have sudo access, change ownership:
sudo chown -R $USER:$USER /path/to/directoryFor example, to take ownership of a directory in /var:
sudo chown -R $USER:$USER /var/www/my-projectVerify the change:
ls -la /var/www/my-projectNow you should be able to run git init without sudo.
If changing ownership isn't appropriate (shared directories), modify permissions instead:
Grant write access to your group:
sudo chmod g+w /path/to/directory
sudo usermod -aG groupname $USEROr set more permissive permissions (use cautiously):
sudo chmod 775 /path/to/directoryAfter adding yourself to a group, log out and back in, or use:
newgrp groupnameIf you must initialize a repository in a protected Windows location:
1. Right-click on Git Bash and select "Run as administrator"
2. Navigate to your target directory
3. Run git init
Better alternative: Use a directory under your user profile:
cd /c/Users/YourUsername/Documents
mkdir my-repo && cd my-repo
git initAvoid running Git as administrator unless absolutely necessary—it can create files owned by the Administrator account that your normal user can't modify later.
After successfully running git init, verify the repository:
ls -la .gitYou should see the Git directory structure:
drwxr-xr-x 7 youruser youruser 4096 Jan 15 10:00 .
-rw-r--r-- 1 youruser youruser 23 Jan 15 10:00 HEAD
drwxr-xr-x 2 youruser youruser 4096 Jan 15 10:00 branches
-rw-r--r-- 1 youruser youruser 92 Jan 15 10:00 config
...Also verify Git recognizes it:
git status### SELinux Considerations (RHEL/CentOS/Fedora)
If you've fixed file permissions but still get "Permission denied," SELinux might be blocking access:
# Check if SELinux is enforcing
getenforce
# Check for recent SELinux denials
sudo ausearch -m avc -ts recent | grep git
# Temporarily set to permissive to test
sudo setenforce 0
git init
sudo setenforce 1If it works in permissive mode, you need to set the correct SELinux context:
# Restore default SELinux context
sudo restorecon -Rv /path/to/directory### ACL (Access Control Lists)
Some systems use ACLs for fine-grained permissions. Check with:
getfacl /path/to/directoryIf ACLs are blocking you, modify them:
sudo setfacl -m u:$USER:rwx /path/to/directory### Network/Mounted Filesystems
If the directory is on an NFS mount, CIFS/SMB share, or other network filesystem:
- Permissions might be controlled by the server, not local settings
- Root squashing on NFS can prevent even sudo from working
- Check mount options: mount | grep /path/to/mount
### umask Settings
Your umask affects default permissions for new files and directories:
# Check current umask
umask
# Temporarily set more permissive umask
umask 002
git init### Avoid sudo git init
Running sudo git init is generally a bad idea because:
1. The .git directory will be owned by root
2. Subsequent git add and git commit commands will fail without sudo
3. It creates a maintenance burden
Instead, fix the underlying permission issue or use a different directory.
### Docker/Container Environments
In containers, permission issues often stem from:
- UID/GID mismatches between container and host
- Volume mounts with incorrect permissions
- Non-root container users without write access
Solutions:
# Run container with your user's UID
docker run -u $(id -u):$(id -g) -v $(pwd):/app image
# Or fix permissions on the mounted volume
docker run -v $(pwd):/app image chown -R node:node /appkex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server
fatal: unable to access: Proxy auto-configuration failed
How to fix 'Proxy auto-configuration failed' in Git
fatal: unable to access: Authentication failed (proxy requires basic auth)
How to fix 'Authentication failed (proxy requires basic auth)' in Git
fatal: unable to access: no_proxy configuration not working
How to fix 'no_proxy configuration not working' in Git
fatal: unable to read tree object in treeless clone
How to fix 'unable to read tree object in treeless clone' in Git