314

Help me please, I am trying to run this in my terminal:

asgard@asgard-A7N8X2-0:~/CollegePortal$ git pull
error: cannot open .git/FETCH_HEAD: Permission denied

Then I try this one

asgard@asgard-A7N8X2-0:~/CollegePortal$ sudo git pull
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

Help me, I don't understand this problem.

4
  • Have you added your public key to github account?
    – madhead
    Commented Nov 2, 2012 at 13:09
  • 5
    Do you have write permission for the local directory & files where you try to do pull? Also, probably sudo doesn't help here at all since it needs access to you ssh keys (not root's). Commented Nov 2, 2012 at 13:09
  • I had this problem and solved it by giving my user Modify & Full Access permissions to that folder (Windows)
    – Frank
    Commented Dec 12, 2019 at 9:55
  • I ran cmd on Windows as Administrator and this fixed it for me. Thank you to Benjamin Bannier for showing that it's a permission problem.
    – Gail Foad
    Commented Dec 23, 2022 at 17:11

36 Answers 36

277

It seems like the first one isn't working because your user doesn't have the permissions for changing that directory, and the second because your root user doesn't have the right SSH keys for accessing that git repository.

Depending on what you're trying to do, it might be better to clone the repository to a different directory, or maybe chown the current directory to have full access for your user

3
  • 57
    This often happens when you accidently forget you've run sudo su and your acting as root in a repo. A simple sudo chown -R <username> . in that directory works magic!
    – Ray
    Commented Jul 7, 2017 at 12:59
  • 3
    Just give Read and Write permission to your current directory. thats it! 'sudo chmod 777 amt/ -R'
    – Vishal J
    Commented Aug 5, 2022 at 10:45
  • Some thing similar, My directory location was in c: (windows) and I needed admin rights. Moved to another location and it worked fine. Commented Oct 6, 2022 at 10:20
200

Check if you have enough permissions on the .git/ directory. You should have write permissions. You can set them with the following command.

Go to your project folder:

chown -R youruser:yourgroup .git/
2
  • 59
    It should be chown -R youruser .git/
    – blend
    Commented Jul 26, 2013 at 11:20
  • It should probably be sudo chown -R youruser .git/
    – bobobobo
    Commented Apr 20 at 0:17
60

If you want to give the permission to the group,

sudo chmod g+w .git -R

worked best for me.

For MacOS

sudo chmod -R g+w .git 
0
37

Try like this way,

Step 1: First check who you are? it will return current user name e.g ubuntu

$ whoami 

Step 2: Then set permission to your current user, in that case, ubuntu by

sudo chown -R ubuntu .git/
0
35

In my case work fine after it:

rm -f .git/FETCH_HEAD

git branch -u
0
34

This is a UNIX permission problem. Do not use sudo for cloning the repository. You don't have the same ssh keys as root and you shouldn't work as root anyway. Try ls -la to find the permissions on the files and use chmod (or sudo chown) to fix them. Hope that helps.

0
27

The answer to this issue make sure .git/FETCH_HEAD has write privileges and you will be all set.

I had this issue on Windows and it was resolved by giving write permissions.

In unix one can run chmod a+rw .git/FETCH_HEAD from the project repository after which it should work.

1
  • does't work with .git/FETCH_HEAD alone if permission issue is on .git.
    – Cœur
    Commented Sep 1, 2015 at 6:04
19

This will resolve all permissions in folder

sudo chown -R $(whoami) ./
1
  • This will always work but it can have the effect of trampling access for other users. Be aware that previous answers were taking group access into account where this answer does not.
    – ftrotter
    Commented Jan 19, 2021 at 19:37
12

In my case, I only had read access to the .git/FETCH_HEAD file. I had to do "sudo chmod g+w .git/FETCH_HEAD" in order to be able to do a pull request.

3
  • does't work with .git/FETCH_HEAD alone if permission issue is on .git.
    – Cœur
    Commented Sep 1, 2015 at 6:04
  • I had to allow write access to the git folder for group sudo chmod g+w .git/FETCH_HEAD
    – William
    Commented Apr 21, 2018 at 18:13
  • sudo chmod g+w .git/FETCH_HEAD, perfect solution. Commented Mar 28, 2019 at 10:26
12

I was having the first issue (FETCH_HEAD permission denied) on Windows.

I fixed it by running Git Bash as an administrator (right click, run as administrator).

11

If you haven't added yourself to the group that owns .git/, then you should.

sudo usermod -a -G $(stat -c '%G' .git) $USER
sudo chmod g+u .git -R
sudo chmod g+u .gitignore
su - $USER

What this does:

  1. finds out which group owns .git/ and adds your user to that group.
  2. makes sure group members have the same permissions as the owner for .git/.
  3. repeats this for .gitignore, which you'll probably need
  4. logs you out and back in to refresh your group membership file permissions

If you just recently did something like this (added yourself to the group that owns .git/), then you need to log out and back in before you'll be able to write to .git/FETCH_HEAD during your git pull.

11

Set permission to your current user by running the command

$ sudo chown -R <username> .git/

9

Simply go to your root folder and run this command to add read/write permissions for all users:

chmod a+rw .git/FETCH_HEAD
7

Running Windows 7, when I had this issue it was because I had hidden the .git folder. The permissions were fine, it was just hidden. Showing the folder resolved it.

0
5

sudo chmod -R 777 .git or if you want to add rw permissions for only yourself, you can use sudo chmod -R +rw .git

4

Error: cannot open .git/FETCH_HEAD: Permission denied

This work for me:

  1. By default .git folder is hidden.
  2. Unhide .git folder and its child folders and file and try to pull request.
4

Use this command, its worked for me.

sudo chown -R $USER: .

Reference: https://techoverflow.net/2019/05/07/how-to-fix-git-error-cannot-open-git-fetch_head-permission-denied/

3

This worked for me:

  1. Right click .git folder
  2. click get info
  3. set permission to your user
  4. click the Cog icon and click apply to enclosed items

No more permission denied errors in git. 🎉

3

This issue arises when you don't give sufficient permissions to .git folder. To solve this problem-

  1. First navigate to your working directory.
  2. Enter this command-

    sudo chmod a+rw .git -R

Hope it helps..!!

2

Look at the owner and group of .git directory with (first go to parent directory of .git) ll .git , look at the group and owner of the directory, add your user to group of of the owner with sudo usermod -a -G yourusername groupsofonwner, then logout => login and everything getting work.

So in summeries

  1. go to parent directory of git

    $cd your path
    
  2. find group owner of the .git direcotry

    $ll .git     
    
  3. add your user to that group

    $usermod -a -G yourusername ownergroupofgit
    
  4. Logout and login to system to this change take effect.

  5. Enjoy it ;)

2

Got that issue when .git folder is hidden and all files in it is hidden too. Make only .git folder hidden without recursive files update and it will work.

2

Reasons of this error could be multiples but in my case i updated branch with root then when i tried to update it with normal user it gives me error .

try both solutions one should work for you

1- sudo chmod g+w .git -R

if it doesn't work please try next solution hope it will solve your problem

2 - rm -f .git/FETCH_HEAD
2

Deleting the FETCH_HEAD in the .git file will solve the problem.

1
2

In my case,

sudo chmod ug+wx .git -R

this command works.

This is what the parts of the command do:

  • sudo = as an admin
  • chmod = change permissions
  • ug = for a user and group
  • +wx = add write and execute permissions
  • .git = the folder/file it applies to
  • -R for recursive (all subfolders/files)
1
  • sudo = as an admin, chmod = change permissions, ug = user and group, +wx = add write and execute permissions, .git = the folder/file you're applying it to, -R for recursive (all subfolders/files)
    – Sandra
    Commented Jan 25 at 16:57
1

I had this message when using git extensions for windows. My fix was to simply close git extensions then open again as administrator

1

I had the exact same error but in my case, the problem was the result of having rebuilt Apache after an upgrade to the PHP version. Long story short, I forgot to install the Apache module 'suexec'.

It had nothing to do with group or ownership. That only took me two days to figure out, someone shoot me...

1

In my case I had a dual-boot system (Windows 10 and Linux) with project folder on NTFS disk. It turned out that on another update Windows 10 enabled by itself "fast startup" in its settings. After I've unchecked it in the Windows - the "error: cannot open .git/FETCH_HEAD: Permission denied" in Linux was gone.

1

for MacOS user (if High Sierra or higher version) use this:

sudo chown -R $(whoami) $(brew --prefix)/*
1

Many Answers here, many suggesting doing a chown. For me was much easier to change user to the user owning the folder (in my case tomcat) as the owner was allowed to write: sudo su tomcat and than do a git pull no need to change permissions. I prefere this because I do not have to remember to change permission back after I am done.

To find the user owning the folder do a ls -la

Note: Do not give non-sudo write access to folders that are served!

1

I'm running Windows 10 and I had the same issue. Here are 2 methods I tried.

  1. I deleted the file (.git/FETCH_HEAD) and ran the command. It resolved the issue.

    OR

  2. Usually .git folder will be hidden in the windows machine. In my laptop all the folders and files inside .git was also hidden. That's the reason I faced permission denied issue. After unhiding the files and folders, issue got resolved.

Thanks

-- Uday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.