Home > Software design >  switching between git users on the same machine (works) but why both have access to the other's
switching between git users on the same machine (works) but why both have access to the other's

Time:10-20

I have 2 github users accounts (private and work), call them Allison and Bob.

I have a bash script, call it: allison_git that does:

ssh-add -D 
eval $(ssh-agent -s)
git config --global credential.helper wincred
git config --global credential.useHttpPath true  
git config --global user.name Allison
git config --global user.email [email protected]
ssh-add ~/.ssh/allison_git_rsa 
ssh -T [email protected] 

if all goes well for Allison, I get: Hi Allison! You've successfully authenticated, but GitHub does not provide shell access.

So I can switch between the users with the script that I have. Great.

However... say that user Bob is for my work and Allison is for my private and I'm working on my work repo but forgot to switch to Bob. My commit would apply as user Allison and not Bob.

I'm not sure why (I guess because they're on the same computer?!) Q: how can I restrict user Allison to use Bobs repo and reverse? Is my approach to all this with the bash allison_git / bob_git causes all this?

Note: I added bob's pub key (bob_git_rsa ) on his github account (settings-->SSH and PGP Keys), and same for allison (allison_git_rsa). The keys are different

CodePudding user response:

You could use a conditional gitconfig directive as done here:

# All work Git repositories are in a subdirectory of ~/work.
# All other Git repositories are outside ~/work.
[includeIf "gitdir:~/work/"]
    path = .gitconfig.work

The user name/email in .gitconfig.work would be Bob's, while the default name/email would be Alice for any other repository outside ~/.work.

Note that git config --global credential.helper wincred applies only for HTTPS URL, and is not used if you are authenticated with SSH.

CodePudding user response:

There are a number of ways to deal with this—for instance, besides VonC's answer, you could just set up two different login accounts on your laptop—but whatever path you choose, remember these items:

  • Git does not do any permissions checking. Git relies on some other software to do any required permissions checking. Git just tries to do whatever you ask, and either the computers allow that, or they don't.

  • Git does not authenticate you to another computer: that's a permissions and access type of thing. Git does have built in support for credential helpers when using https:// URLs, and built in support to invoke ssh when using ssh:// URLs (and the funky user@host:path/to/repo.git abbreviation which is short for ssh://user@host/path/to/repo.git). But that's just so that Git can run these other, external programs that allow for authentication. This goes back to the first point above.

  • The user.name and user.email setting are nearly arbitrary strings (though you have to use valid text here and can't put angle brackets in the user name for instance) that Git merely stuffs into new commits without really looking at them. They are purely there for information. This goes back to the first point above.

To use different ssh configurations, most people I know that do this use ssh's "host alias" tricks. This works well with the conditional include trick mentioned in VonC's answer, especially when you combine it with Git's url.<replacement>.insteadOf configuration trick.

  • Related