231

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.

Burgi
  • 401
  • 7
  • 23
Arthur Yakovlev
  • 7,421
  • 7
  • 30
  • 44
  • Have you added your public key to github account? – madhead Nov 02 '12 at 13:09
  • 3
    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). – Benjamin Bannier Nov 02 '12 at 13:09
  • I had this problem and solved it by giving my user Modify & Full Access permissions to that folder (Windows) – Frank Dec 12 '19 at 09:55

31 Answers31

221

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

abyx
  • 61,118
  • 16
  • 86
  • 113
  • I am facing the similar issue and I am not sure if I can give my `www-data` user the permission to update the `.git` directory. I am calling the shell script using php over http in browser. Details [here](http://stackoverflow.com/questions/32116917/executing-git-commands-via-php-over-http) – KillABug Aug 21 '15 at 08:58
  • 28
    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 .` in that directory works magic! – Ray Jul 07 '17 at 12:59
  • @Ray has the answer! Worked perfectly for me. – drewkiimon Mar 09 '21 at 22:32
  • works like magic but how come my directory's permission changed? – Aayush Neupane Apr 20 '21 at 06:01
161

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/
René Höhle
  • 24,401
  • 22
  • 66
  • 73
51

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 
shilovk
  • 7,603
  • 15
  • 54
  • 63
Won Jun Bae
  • 4,379
  • 5
  • 39
  • 48
32

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.

Adriano
  • 2,862
  • 4
  • 25
  • 37
Johannes Thoma
  • 891
  • 8
  • 17
  • Exactly this! I came across this problem because I had to do `sudo clone` in order to accept XCode's new license agreement. The answer is to accept the agreement, then clone with no sudo. – Barnaby Oct 29 '15 at 23:12
28

In my case work fine after it:

rm -f .git/FETCH_HEAD

git branch -u
shilovk
  • 7,603
  • 15
  • 54
  • 63
23

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.

shilovk
  • 7,603
  • 15
  • 54
  • 63
pg2286
  • 831
  • 9
  • 20
21

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/
Always Sunny
  • 29,081
  • 6
  • 43
  • 74
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.

Jim
  • 11,270
  • 21
  • 86
  • 134
9

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).

Nicholas Westby
  • 847
  • 10
  • 26
9

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.

hobs
  • 15,252
  • 8
  • 75
  • 93
9

Set permission to your current user by running the command

$ sudo chown -R <username> .git/

Shivam Kohli
  • 319
  • 3
  • 4
8

This will resolve all permissions in folder

sudo chown -R $(whoami) ./
Pablo Papalardo
  • 956
  • 9
  • 7
  • 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 Jan 19 '21 at 19:37
6

Simply go to your root folder and run this command:

chmod a+rw .git/FETCH_HEAD
Rishabh Agarwal
  • 1,663
  • 12
  • 29
5

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.

jdf
  • 670
  • 9
  • 20
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.

Sentry.co
  • 4,132
  • 38
  • 30
3

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.
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 ;)

Yuseferi
  • 5,610
  • 10
  • 54
  • 83
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
Shahbaz
  • 3,153
  • 1
  • 24
  • 41
1

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

Rob Bowman
  • 5,548
  • 19
  • 66
  • 137
1

In my case,

sudo chmod ug+wx .git -R

this command works.

narwanimonish
  • 489
  • 6
  • 8
1

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..!!

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...

Vince
  • 769
  • 2
  • 10
  • 24
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.

ysvet
  • 143
  • 1
  • 1
  • 10
0

I got this because I had more than 1 user account on my box. I was logged in as user A and was in a directory for user B. User A didn't have permission to user B's stuff. Once I realized I wasn't where I thought I was in the file system, this error made sense.

thebiggestlebowski
  • 2,292
  • 1
  • 24
  • 25
0

if you find the same problem in windows server, then you need to run the command line with enough permission, such as administrator permission.

aaron
  • 1,569
  • 3
  • 20
  • 35
0

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

sudo chown -R $(whoami) $(brew --prefix)/*
rony36
  • 3,073
  • 1
  • 27
  • 40
0

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!

Robin
  • 158
  • 1
  • 7
0

When I was doing bundle install I got:

Following files may not be writable, so sudo is needed:
  /Library/Ruby/Gems/2.6.0
  /Library/Ruby/Gems/2.6.0/bin
  /Library/Ruby/Gems/2.6.0/build_info
  /Library/Ruby/Gems/2.6.0/bundler
  /Library/Ruby/Gems/2.6.0/cache
  /Library/Ruby/Gems/2.6.0/doc
  /Library/Ruby/Gems/2.6.0/extensions
  /Library/Ruby/Gems/2.6.0/gems
  /Library/Ruby/Gems/2.6.0/specifications
Fetching gem metadata from https://rubygems.org/.........
error: cannot open .git/FETCH_HEAD: Permission denied

I had followed the steps from here and updated my bash_profile to add a rbenv shim.

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

But for some unknown reason I had them commented out. Once I uncommented I was able to run bundle install successfully.

Honey
  • 24,125
  • 14
  • 123
  • 212
-1

In my case work that: I just wrote sudo before the command :

sudo npm run deploy
Rostislav Matl
  • 3,781
  • 4
  • 23
  • 50
-2

TL;DR: In Dual Boot systems, turn off Fast Startup for Windows

I was having this issue in my dual boot Ubuntu/Windows system. I couldn't write anything in any of the partitions that are shared with Windows (NTFS partitions).

I recently reinstalled both and forgot to turn of the "Fast Startup" in Windows. Everything went back to normal after I rebooted in windows and turned that feature off and restarted again.

numan947
  • 49
  • 9