X

Error cannot open git/FETCH_HEAD permission denied [Fix]

Error cannot open git/FETCH_HEAD permission denied [Fix]. Git repositories contain a special folder called .git/. You may not have seen this folder because it is hidden. The hidden status of this folder is denoted by the full stop (“.”) that comes at the start of the folder name. Kamerpower.com

This hidden folder contains various pieces of metadata about a repository and Git needs read and write access to this folder. 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 a file.

Solutions to Error Cannot open .git/FETCH_HEAD Permission denied

Since the current logged in user does not have permission to modify the directory. The error will be displayed. 

Modify the current directory permissions using Whoami.

1. Whoami view the current logged in user

xxx$ whoami

2. Modify permissions for Sudo Chown assuming the above query login account as: Adminname

xxx$ sudo chown -R Adminname:admin /Users/work

An Example Scenario for Error

We will be cloning a repository called ck-git from GitHub. This repository contains a file called README.md. To clone this repository, we can use the git clone command:

sudo git clone https://github.com/kamerpower/login-registration-wp-plugin/ck-git

The contents of our README.md file are currently:

# ck-git

We want to change this file to contain a more descriptive README.md. We’re going to open this file in a text editor and change its contents to the following:

# Kamerpower registration login Git Demo
sudo git add README.md
sudo git commit -m “docs: Make README.md more descriptive”
git pull

This git pull command returns: error: cannot open .git/FETCH_HEAD: Permission denied

The Solution on How to Fix Error cannot open .git/FETCH_HEAD permission denied

We cloned the ck-git repository using the “sudo” command. This means our repository was cloned as the root user. Because the root user cloned the repository, the files in the repository are owned by the root user.

We can see this by running the ls -la command:

total 8
drwxr-xr-x 4 root staff 128 Sep 17 07:15 .
drwxr-xr-x+ 90 James staff 2880 Sep 17 07:15 ..
drwxr-xr-x 12 root staff 384 Sep 17 07:15 .git
-rw-r--r-- 1 root staff 1 Sep 17 07:15 README.md

All of the files in our folder are owned by “root”, which is part of the “staff” account. When we try to pull our remote repository without using “sudo”, an error is returned. To fix this issue, we are going to change the ownership of the files in our folder. We can do this using the chown command:

sudo chown -R james:staff .
git pull

This command changes the ownership details of all the files and folders in our repository, including the .git/ folder.

This git pull command executes successfully.

Recommendation

This post was published on Aug 10, 2022 16:54

Kamerpower Content Team: Dedicated to producing the best educational resources, orientation to students, insightful career advice including guides and how-tos. Our team also help students apply to foreign universities, counsels students towards their higher education dream while ensuring they get the best employment guide and career advice to help them after studies.
X

GDPR

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings