[contains bat]bat for when git says execute “git config –global –add safe.directory”

This article can be read in about 3 minutes.
PR

The purpose 

Immediately after cloning a Git repository, you may sometimes see a message from Git that prevents you from pulling or performing other operations.

Could not open repository.
libgit2 returned: repository path 'X:\XXXX' is not owned by current user

To add an exception for this directory call:
git config --global --add safe.directory 'X:\XXXX'

It would be easy enough to tweak the command shown and run it, but it’s a hassle and I always forget the rules, so I made a batch script.

To manually execute the command as shown above, you need to modify and then run the displayed command.

Specifically, you need to remove the double quotes surrounding the folder path.

PR

Create a bat

Create a batch file (e.g., add_safedir.bat) with any name you like in the root folder of your repository.

Open the created batch file with a text editor such as Notepad, paste the following batch script into it, and save it.


set "current_dir=%~dp0"
set "m_dir=%current_dir:\=/%"
set "m_dir2=%m_dir:~0,-1%"

git config --global --add safe.directory %m_dir2%

pause
PR

Execute bat

Double-click the saved batch file to run it.

A message will appear saying “Press any key to continue…”, so press any key to close it.

Folders with spaces in their names may not work. 

PR

Result

We can now perform Git operations like pull. 

PR

Reference

git config --global --add safe.directory '...' が出たとき - Qiita
はじめにUSBメモリ上のディレクトリに対してリモートリポジトリ設定を行おうとすると,中々うまくいきませんでした.かなり手こずってしまったので,私の場合の解決方法を残しておきます.環境Wind…
BATファイルで文字列の切り出し

comment

Copied title and URL