I am trying to set up SSH and GPG commit verification on my work Mac laptop. I have two identities I use for git
, one for work and another for personal account.
I follow steps in my repo that I created exactly for these purposes: https://github.com/rnag/Mac-Quickstart
Now the issue that I have find (after setting up SSH and GPG successfully) is that when I run git push
on personal projects, I get this error:
$ git push
ERROR: Permission to rnag/Mac-Quickstart.git denied to <other user>.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
This is rather ironic since this repo is supposed to help me set up SSH correctly in the first place.
However, that aside, it took me a long time to debug this issue itself.
I figured it out, that what is happening is that I am using HTTP remote URLs already for clone and push purposes.
That is, my current remote url looks like this:
$ grep url .git/config
url = https://github.com/rnag/Mac-Quickstart.git
Due to the git config the script have help me set up, when I run git remote -v
it shows me SSH url now:
$ git remote -v
origin [email protected]:rnag/Mac-Quickstart.git (fetch)
origin [email protected]:rnag/Mac-Quickstart.git (push)
But that's not exactly how I'd want SSH url to look. If we take a look at my merged git config, these are the entries it has for insteadOf
:
[url "[email protected]:"]
insteadOf = https://github.com/
[url "[email protected]"]
insteadOf = [email protected]
[url "git://"]
insteadOf = https://
So we have SSH urls replaced correctly -- [email protected]/<user>/<repo>
becomes [email protected]/<user>/<repo>
, as I'd want it to.
But with HTTPS urls, the insteadOf
set up does not chain and work as expected. For example, it replaces https://github.com/<user>/<repo>
to [email protected]/<user>/<repo>
as expected, but it doesn't do the second replacement in my git config as I expect it to, and replace [email protected]/<user>/<repo>
with [email protected]/<user>/<repo>
.
Of course, this poses a problem because in my ~/.ssh/config
file, it is looking for a host to be like github.com-<user>
format. It sees github.com
and because there is no entry, I assume it uses whatever default SSH identity file that it wants to.
So there is issue with insteadOf
due to me having set up multiple identities with git
.
I searched online for long time after spending a long time debugging the root cause of this issue which turn out because I was using an HTTPS url for git, but I couldn't find anything that mentioned it.
I am not too familiar with git docs on insteadOf
, or why it would not chain or work as expected in this scenario. If any seasoned git expert has tips on this, it would be great to know about it. Thanks!
Update. For now -- unless someone else can offer a better solution -- the workaround I have in place is to:
Remove all instances of insteadOf
everywhere, including the global git config at ~/.gitconfig
.
It might be helpful to run the following command to find all occurrences of insteadOf
and discover the source of it:
git config -l --show-origin | grep insteadof
Add the following to your user-specific git config. This is not your global git config at ~/.gitconfig
but your github-username- or project- scoped config, e.g. for your Git project.
For me, that was located at ~/.dotfiles/Personal-github.gitconfig
. Then I opened it in a text editor, and replaced all usages of insteadOf
with these lines:
Replace
<user>
with your GitHub username.
[url "[email protected]<user>:"]
insteadOf = [email protected]:
insteadOf = https://github.com/
If all is good, you should receive a success message when running git push
. You can try that out when you have no local changes and nothing staged, and it should now not print that error message from before:
$ git push
Everything up-to-date