How to Fix Git 'Permission Denied' error

Git permission error

Why You Get 'Permission Denied' in Git in VS Code on WSL & How to Fix It

🧠 Solution for My Noob and Impatient Users

  • Check if your git credential is correctly set or not

    git config --list
    

    The output should look something like the following screenshot Git check username and password

  • If you just want the fix (no stories, no theory):

    sudo chown -R $USER:$USER .git
    

    Here $USER is your username of ubuntu/ windows.

  • Then retry your commit:

    git commit -m "your message"
    

Boom 💥— you’re done.


🧩 The Real Story Behind It

While working inside WSL (Windows Subsystem for Linux), I ran into this annoying error while trying to commit my Frappe app code:

fatal: could not open '.git/COMMIT_EDITMSG': Permission denied

At first, it looked like a random Git glitch. But it turns out it was me — I had previously run a Git command with sudo, and WSL dutifully created a few .git files owned by root.

That one small mistake locked me out from writing new commit messages.


🧾 Why It Happens in WSL

In WSL, file ownership behaves like a regular Linux system. When you use sudo inside your project folder, all files Git touches (like .git/config, index, COMMIT_EDITMSG) become owned by root. Later, when you try to run normal Git commands as your normal user, WSL politely says:

“Permission denied.”

It’s not Git’s fault — it’s Linux being strict about file ownership.


🛠️ Step-by-Step Fix

1. Check which files are owned by root

ls -l .git

You’ll see the output something like:

-rw-r--r-- 1 root root   52 Nov 12 02:28 COMMIT_EDITMSG
-rw-r--r-- 1 root root  101 Nov 12 22:56 FETCH_HEAD
...

means:

There’s a file named COMMIT_EDITMSG that is owned by root, only root can edit it (others can only read it), and it was last modified on Nov 12 at 2:28 AM.

Here’s what each part means 👇

SectionExampleMeaning
File permission-rw-r--r--This shows who can read or write the file.
r = read, w = write, x = execute.
• The first rw- means the owner can read/write.
• The next r-- means the group can only read.
• The last r-- means others can only read.
Number of links1How many links (copies or references) point to this file. You can ignore this for now.
Owner namerootThe user who owns the file. Here, root = the system’s admin user.
Group namerootThe group that owns the file — also root here.
File size52File size in bytes. (So 52 bytes = a tiny text file.)
Last modified dateNov 12 02:28The date and time when this file was last changed.
File nameCOMMIT_EDITMSGThe actual name of the file.

2. Take ownership back

Run this inside your repo:

Here $USER is your username of ubuntu/ windows.

sudo chown -R $USER:$USER .git

Which means:

“Give ownership of all .git files back to me.”


If you want to fix everything in your bench or project folder:

sudo chown -R $USER:$USER ~/frappe-bench

3. Verify ownership

ls -ld .git

You should see:

drwxrwxr-x  ... sab sab ...

means:

“This is a directory owned by user sab. The owner and their group can read, write, and read it. Other users can only read and open it, but not modify it.”


💡 Quick Legend (for memory)

SymbolMeaning
ddirectory (folder)
-normal file
lsymbolic link
rread
wwrite
xexecute (or enter, for folders)

4. Retry your commit

git commit -m "fix: corrected social link fields and updated b_event script"

✅ It should now work flawlessly.


🚫 Don’t Use sudo git

This is the real lesson. Never use sudo with Git — not even once — unless you absolutely need to modify system-level repositories.

In a dev setup (especially WSL), you should always:

  • Run Git as your normal Linux user
  • Use SSH keys for authentication
  • Avoid running any bench or git command with sudo unless it’s for system-level tasks

💡 Bonus Tip: Fix All Your Git Repos at Once

If you often work in multiple apps inside frappe-bench:

cd ~/frappe-bench/apps
for app in */; do sudo chown -R $USER:$USER "$app/.git"; done

That’ll recursively fix ownership for all apps in one go.


✅ Summary

ProblemCauseFix
fatal: could not open '.git/COMMIT_EDITMSG': Permission deniedYou ran sudo git and .git files became owned by rootsudo chown -R $USER:$USER .git
Can’t push or pull without passwordSSH key not configuredUse ssh-keygen and add to GitHub
Ownership issues across benchMixed sudo operationsFix all with a for-loop or chown bench

🧩 Final Thoughts

WSL is amazing — but it plays by Linux rules. Once you stop mixing sudo with normal Git operations, life gets much smoother.

So next time you see “Permission Denied,” remember:

It’s not Git being stubborn — it’s just protecting your house keys from the wrong user. 🔑

Related posts