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.
In this guide, we’re going to discuss what this error means and its cause. We’ll walk through an example so you can learn how to fix it in your program.
Cannot open .git/FETCH_HEAD: Permission denied
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.
This folder contains various pieces of metadata about a repository. It tracks your project-specific configuration options, the references in your project, your current HEAD, among other crucial pieces of information about your repository.
Git needs read and write access to this folder. This is because its contents will change as you run commands like git config and git pull.
An Example Scenario
We’re going to clone 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/Career-Karma-Tutorials/ck-git
The contents of our README.md file are currently:
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:
# Career Karma Git Demo
A repository with demo files for Career Karma’s Git tutorials.
That’s better. Our file more accurately describes the purpose of our Git repository. Now, let’s add this change to the staging area so we can create a commit:
sudo git add README.md
Git knows that we want to add README.md into our next commit. To make our changes show up in our remote repository, we have to add them to a commit:
sudo git commit -m "docs: Make README.md more descriptive"
Our commit is now ready to be pushed to our remote repository. Before we push our code, we’re going to pull the remote version of our repository. This will let us make sure that we are up to date with any changes that have been made since we cloned the repository:
This command returns:
error: cannot open .git/FETCH_HEAD: Permission denied
This error tells us that Git cannot access one of its configuration files, FETCH_HEAD.
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. This is because our standard user account does not have permissions to modify the files in the folder.
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 .
This command changes the ownership details of all the files and folders in our repository, including the .git/ folder. We are now the owner of the directory and have full access to the project folder. We should now be able to pull our code:
This command executes successfully and returns:
Already up to date.
We now know that no changes have been made to our remote repository since we last pulled our code. We last pulled code when we created the repository.
The Git “Cannot open .git/FETCH_HEAD: Permission denied” error occurs when you try to pull code from a remote repository when the .git/ directory in your project folder is inaccessible to your current user.
To solve this error, make sure that your current user has read-write privileges to the Git repository with which you are working.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more