Git “fatal: detected dubious ownership in repository”

I keep running into an ownership error after migrating to a new computer. `sudo chown -R $(whoami) .` fixes it for me.

Due to the way our IT department handles new computers, I had to copy my files from my older computer to my new computer in a really roundabout way. One of the unfortunate consequences of that is that I keep coming across permissions issues on the copied files.

For git repositories I haven’t worked with on the new computer yet, I get the following error:

% git status
fatal: detected dubious ownership in repository at '/path/to/current/folder'
To add an exception for this directory, call:

        git config --global --add safe.directory /path/to/current/folder

It’s happens infrequently enough that I keep having to look up the fix, but often enough that I figured I’d record that fix here for reference.

This superuser.com answer does the trick for me:

% sudo chown -R $(whoami) .

I don’t know why, but setting permissions in Finder does not work.

Here's everything for context:

% git status
fatal: detected dubious ownership in repository at '/path/to/current/folder'
To add an exception for this directory, call:

        git config --global --add safe.directory /path/to/current/folder
% sudo chown -R $(whoami) .
Password:
% git status
On branch sprint
Your branch is up to date with 'origin/sprint'.

nothing to commit, working tree clean