[Solved] I cannot add the parent directory to *safe.directory* in Git

We Are Going To Discuss About I cannot add the parent directory to *safe.directory* in Git. So lets Start this Github Article.

I cannot add the parent directory to *safe.directory* in Git

  1. I cannot add the parent directory to *safe.directory* in Git

    to solve I cannot add the parent directory to *safe.directory* in Git I think it has less to do with your email, and more with the owner of the directories on your filesystem.
    Is the user you're currently logged in with also the owner of the folder?
    How about the parent folder?
    Also, are you invoking git from within the repository directory?
    The update is only a few hours old, so I guess things are still in flux.
    For now, as the message from git suggests, execute
    git config --global --add safe.directory F:/GitHub/my-project

  2. How to solve I cannot add the parent directory to *safe.directory* in Git

    to solve I cannot add the parent directory to *safe.directory* in Git I think it has less to do with your email, and more with the owner of the directories on your filesystem.
    Is the user you're currently logged in with also the owner of the folder?
    How about the parent folder?
    Also, are you invoking git from within the repository directory?
    The update is only a few hours old, so I guess things are still in flux.
    For now, as the message from git suggests, execute
    git config --global --add safe.directory F:/GitHub/my-project

Solution 1

Answer

This seems to be related to this announcement of a vulnerability:
https://github.blog/2022-04-12-git-security-vulnerability-announced/

I think it has less to do with your email, and more with the owner of the directories on your filesystem.
Is the user you’re currently logged in with also the owner of the folder?
How about the parent folder?
Also, are you invoking git from within the repository directory?
The update is only a few hours old, so I guess things are still in flux.

For now, as the message from git suggests, execute

git config --global --add safe.directory F:/GitHub/my-project

and make sure you are calling git from within F:/GitHub/my-project for now.

EDIT: As we found out in the comments below, the owner of the parent of the directory containing the .git folder (the git repository) is the issue.
Cloning the project anew is a platform independent way to make sure you are the owner.

Sidenote

I ran into the same problem using flutter on Linux, which on my distro was installed in /opt/flutter. I am not working as root, thus I run into the same problem.
Running git config --global --add safe.directory /opt/flutter did indeed fix the problem for me.

Longer edit: Clarification

Going through the post about the vulnerability again after a good night’s sleep, I think a bit of clarification is in order. I’ll leave the rest of the answer as is.
Lets look at the following simple directory structure.

/home/
├─ tommy/
│  ├─ .git/
│  ├─ rental_space/
│  │  ├─ mary/
│  │  │  ├─ projects/
│  │  │  │  ├─ phone_app/
│  │  │  │  │  ├─ .git/
│  │  ├─ anthony/

In this case, the user tommy owns his own directory under /home, but (for some reason) rents out space to other users, in this case mary and anthony.
If mary, by mistake, where to execute git in her directory, but outside of her phone_app project, then old git would go up the directory tree to search a .git repository. The first it finds is the one from /home/tommy/.git. This is a security risk, because another user, in this case anyone that can act as tommy, can affect mary‘s execution of git and possibly cause trouble.
From Git v2.35.2 and onward, the traversal will stop as soon as the directory entered belongs to a user other than mary. Say mary executed git in /home/tommy/rental_space/mary/projects, then git will check in projects, but find no .git. It will go up one directory, check in mary, but again find no .git. Then it will again go up, but rental_space belongs to tommy, not mary. The new git version will stop here and print the message we saw in the question.
Adding the directory /home/tommy/rental_space to the safe.directory variable would allow git to proceed but is, as explained, a security risk.

I used a Linux-like directory directory structure here, but the same is true on Windows.

Original Author derpda Of This Content

Solution 2

As derpda said, it’s related to a Git security vulnerability that has been fixed.

On Linux at least, you can fix the problem by ensuring that the parent folder of the Git repository is owned by you. There isn’t any need to add to the safe.directory configuration setting if you can set the folder ownership appropriately.

If you need different ownership (e.g., working on a service that runs as a different user) then add your folder to the multi-value safe.directory configuration setting:

git config --global --add safe.directory /path/to/project

Original Author webaware Of This Content

Solution 3

I found the same issue on windows after upgrading to version 2.35.2.windows.1.
I was able to fix it by taking ownership of the folder containing the .git folder and all the files in it.
This is the command assuming you already are in the repo folder.

takeown.exe /f . /r

Note: if you have multiple repo folders inside a work folder you may want to take ownership of the work folder and its sub-folders recursively. It takes more time to execute but you only need to to this once.

The command would look like this under cmd.exe:

takeown.exe /f C:\Users\%USERNAME%\Work /r

Or like this under powershell.exe or pwsh.exe:

takeown.exe /f $HOME\Work /r

Original Author Camilo Estevez Of This Content

Solution 4

I run Visual Studio sometimes as Administrator and sometimes as normal user. This lead to the problem.

Symptoms 1

In Visual Studio – when opening a local folder – it seems as if the code is no longer added to source control.

Opening a solution in Visaul Studio 2019: The above messages appear in the Git output window for each project.
Opening a solution in Visual Studio 2022: It just says One or more errors occurred..

Symptoms 2

When opening a normal command prompt in the parent folder of .git executing git log shows the message

fatal: unsafe repository ('C:/GIT/MyProject' is owned by someone else)
To add an exception for this directory, call:

    git config --global --add safe.directory C:/GIT/MyProject

There are no errors when executiong any command in an Administrator command prompt.

Solution

Change the owner of the MyProject folder to the normal user. Afterwards everything works fine as normal user and also as Administrator.

Original Author huha Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment