Arthur Yakovlev
Arthur Yakovlev

Reputation: 9309

Trying to git pull with error: cannot open .git/FETCH_HEAD: Permission denied

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.

Upvotes: 319

Views: 503519

Answers (30)

shoulong tan
shoulong tan

Reputation: 1

First you delete the FETCH_HEAD file in the .git folder, then run command git fetch, the FETCH_HEAD file will be recreated. Lastly, try git pull, it would most likely work again.

Upvotes: 0

rakesh rake
rakesh rake

Reputation: 1

Git hub email, and pc account email id should be same. Otherwise Add the user(email id) (( which you are using in git bash/git and give all permission read and write. File - right click - give access to - specific people... -click down arrow mark - create a new user -manage another account -add new user in pc settings - add someone else/add family member - enter the mail id. And try to run git commands.

Upvotes: 0

Suvajit Chakraborty
Suvajit Chakraborty

Reputation: 91

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

chmod a+rw .git/FETCH_HEAD

Upvotes: 9

narwanimonish
narwanimonish

Reputation: 1712

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)

Upvotes: 2

Uday
Uday

Reputation: 21

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

Upvotes: 1

Jonas Oliveira
Jonas Oliveira

Reputation: 9

My problem was with the Ubuntu command line.

I managed to solve it, after logging in as root.

sudo su
git pull

Upvotes: -3

Osman Ordu
Osman Ordu

Reputation: 21

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

Upvotes: 2

Elijah
Elijah

Reputation: 2203

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

Upvotes: 7

Saravanan DS
Saravanan DS

Reputation: 288

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/

Upvotes: 4

mfaani
mfaani

Reputation: 36297

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.

Upvotes: 0

Trideb Chapagai
Trideb Chapagai

Reputation: 59

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.

Upvotes: 4

Robin
Robin

Reputation: 187

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!

Upvotes: 1

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

sudo npm run deploy

Upvotes: -1

rony36
rony36

Reputation: 3339

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

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

Upvotes: 1

In my case work fine after it:

rm -f .git/FETCH_HEAD

git branch -u

Upvotes: 35

Johannes Thoma
Johannes Thoma

Reputation: 1076

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.

Upvotes: 34

ysvet
ysvet

Reputation: 163

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.

Upvotes: 1

Vince
Vince

Reputation: 970

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

Upvotes: 1

Pablo Papalardo
Pablo Papalardo

Reputation: 1312

This will resolve all permissions in folder

sudo chown -R $(whoami) ./

Upvotes: 21

Shivam Kohli
Shivam Kohli

Reputation: 489

Set permission to your current user by running the command

$ sudo chown -R <username> .git/

Upvotes: 12

Nakshtra Pradhan
Nakshtra Pradhan

Reputation: 111

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

Upvotes: 3

Won Jun Bae
Won Jun Bae

Reputation: 5389

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 

Upvotes: 60

aaron
aaron

Reputation: 2056

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

Upvotes: 0

Shahbaz
Shahbaz

Reputation: 3463

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

Upvotes: 2

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38502

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/

Upvotes: 37

Rob Bowman
Rob Bowman

Reputation: 8721

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

Upvotes: 1

Sentry.co
Sentry.co

Reputation: 5569

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

Upvotes: 3

Oleg Dmitriev
Oleg Dmitriev

Reputation: 21

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.

Upvotes: 2

hobs
hobs

Reputation: 19259

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.

Upvotes: 11

Yuseferi
Yuseferi

Reputation: 8670

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

Upvotes: 2

Related Questions