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.
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
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.
Result
We can now perform Git operations like pull.
Reference

comment