Git Cannot open .git/FETCH_HEAD: Permission denied Solution

Did you face Cannot open .git/FETCH_HEAD: Permission denied when you try to run a git pull command on your site root folder hosted on a linux server running ubuntu? If you do you are at right place for a permanent solution!

Normally, Git needs write permissions on the files in a directory called .git/ inside your project folder. If the Git command line does not have access to this folder, you’ll encounter an error like Cannot open .git/FETCH_HEAD: Permission denied when you try to pull.

In this guide, we are going to discuss what this error means and the cause behind it. We’ll walk through an example so that you can learn how to fix it.

Solution:

This example is showing you how to give ubuntu user the privilege to edit and add files in the /var/www folder.

Fix Cannot open .git/FETCH_HEAD: Permission denied permission issues by following these steps.

Adding ubuntu user to www-data group

Add ubuntu user to www-data group

sudo usermod -a -G www-data ubuntu

Optional – if user (user with any other name) does not exist do this additional step to add user:

sudo adduser [username] www-data

Important step: Make sure all files are owned by the www-data group and user (refreshes the ownership settings)

sudo chown -R www-data:www-data /var/www

Enable all members of the www-data group to read and write files:

sudo chmod -R g+rw /var/www

Important: Upto this step, you should be all set to git pull as user ubuntu. But, probably, you would want all files created in this directory in future to listen to the current ownership settings. If you do not do it, you will have to set file ownership manually everytime you create new file or folder under your project root.

To enable automated permission granting, run the following:

sudo chmod -R g+rws /var/www

Logout and login back as user ubuntu to see new permissions in effect.

Conclusion

In this simple tutorial we have learned how to fix the file the write permission error Git Cannot open .git/FETCH_HEAD: Permission denied occurring due to the mismatch between name and permissions of current user ubuntu and web server group www-data.

Leave a Reply