[FIXED] error: cannot open .git/fetch_head: permission denied


Written By - Steve Alila
Advertisement

The cause of the error, "error: cannot open .git/fetch_head: permission denied" is doing a git pull on a directory with a user that lacks a write w permission on the directory.

The solution is to grant the current (operating system) user permission to write to the directory.

sudo chmod u+w <operand> -R

Where chmod, full for change mode, lets you add or remove the permission to the specified directory. For example, u+w means add + the write w permission to the user u. The -R option means the permission extends to all subdirectories and files of the specified directory operand.

Alternatively, you can give the user or its group ownership of the directory, as shown in the following sections of this directory. Before doing that, it would help to understand ownership and permissions in Unix-related operating systems.

Read on to learn more.

 

Ownership and Permission in Linux

Linux maintains file privacy and security through ownership and permissions. The (user) creator of the file becomes its default owner. This ownership can be extended to the user's group.

  • r read
  • w write
  • x execute
  • - no permission

Read r permission lets the user view the file contents. You can inspect the ownership using the ls command with the -l and -a options.

ls -la

Write w permission enables the reader to modify the file and its contents, whereas execute x enables the user to run the file. The hyphen - denotes the user lacks any of the three permissions: r, w, or x.

Advertisement

Here is an example.

-rw-r--r-- 1 steve steve 44 Jan 30 14:24 file.py

The first hyphen denotes that the resource is a regular (not directory) file.

The user called steve can read and write but not execute the file rw-.

The group steve can only read but not write or execute the file r--.

Other users can only read but not write or execute the file r--.

You can modify the ownership or permissions on a file using the chown and chmod commands, respectively.

 

Change ownership

The general structure of the chown command is

sudo chown <option> <new owner> <file>

The most used option is -R which stands for recursive (directory) file ownership change. The new owner portion can be the user only or user and group (separated by a colon). The file portion can be a regular or directory file.

Advertisement

Additionally, you can change only the group ownership using the chgrp command. You can check the current user using the whoami command. Similarly, you can list the user's groups using the groups command or change a file's group using the newgrp command.

sudo newgrp root

root is the current file's new group. The file's new group (owner) is root because only one group can own a file at a time.

 

Change permissions

You can change file permissions using the absolute or symbolic mode.

The absolute mode uses a digit to represent the three permission types. For example, 7 equates to all the permissions: read + write + execute.

Here is a summary of the permissions.

Number Permission Type Symbol
0 No permission ---
1 Execute --x
2 Write -w-
3 Write + Execute -wx
4 Read r--
5 Read + Execute r-x
6 Read + Write rw-
7 Read + Write + Execute rwx

So, a 764 permission means the user can read + write + execute; the group can read + write, whereas the rest of the system users can only read the file.

Advertisement

The symbolic mode is more minimalistic as it enables you to target a specific portion at a time. It uses Mathematical operators to represent permissions

Operator Description
+ Add permission
- Remove permission
= Set permission or override the previously set permission

and letters to represent owners.

Letter Reference
u user
g group
o others
a all

For example, g+w gives the group g a write w permission. Similarly, o=rwx means other o users will henceforth have read r, write w, and x permissions.

Now that you know how to change ownership and permissions on a file, let's practice the skills by solving the error, "error: cannot open .git/fetch_head: permission denied."

 

Lab environment setup

Launch your terminal and create a working directory. I am creating one called workingDir on Ubuntu 22.10 machine before navigating into the directory.

mkdir workingDir
cd workingDir

Next, let's clone this repository into the directory.

git clone https://github.com/Stevealila/Python.git pyFiles

Then, prevent the user from writing to any file in the directory.

sudo chmod u-w pyFiles -R

Now let's attempt a git pull on the pyFiles directory.

[FIXED] error: cannot open .git/fetch_head: permission denied

 

Practical example

On doing a git pull on the cloned repository, you get the error error: cannot open .git/fetch_head: permission denied.

Input

Advertisement
cd pyFiles
git pull

Output

error: cannot open '.git/FETCH_HEAD': Permission denied

Let's apply one of the solutions we just learned to solve the error, "error: cannot open .git/fetch_head: permission denied."

For example, we can grant the user permission to the pyFiles directory or .git directory. Then, retry pulling the remote files.

Input

sudo chmod u+w .git -R
git pull

Output

Already up to date.

Now, the version control detects that the remote repository is in sync with its local version.

error: cannot open .git/fetch_head: permission denied

 

Key takeaway

The main cause of the error message error: cannot open .git/fetch_head: permission denied is doing a git pull on a directory where the current user lacks ownership or write permission.

As shown in this tutorial, you can solve the error by making the user the new owner of the .git directory or permitting the user or its group to write to the directory.

 

Categories GIT

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment