20

Is it possible to have a folder that contains multiple repos use one set of credentials and have another folder user a different account? For example I would like to have one folder on my mac for work repos and one folder for personal and have the git username and password automatically switch based on which folder I am in.

cbutler
  • 1,111
  • 5
  • 16
  • 32
  • i dont think thats possible for git out of the box, but setting default for each repo is possible. – Bagus Tesa May 31 '18 at 07:38
  • You can set credentials for each individual project in the `.git/config` file, if that helps. – rst-2cv May 31 '18 at 07:40
  • 3
    Possible duplicate of [Multiple github accounts on the same computer?](https://stackoverflow.com/questions/3860112/multiple-github-accounts-on-the-same-computer) – phd May 31 '18 at 08:27

4 Answers4

19

Configuring git config for specific folders is too easy. You can achieve that using just two git commands. Just enter the directory you want git account info to be changed for and do this:

git config --local user.email "yourmail@example.com"

git config --local user.name "yourName"

I think this will help.

Cheers!

Vikas Yadav
  • 3,094
  • 2
  • 20
  • 21
6

I may be wrong but I just create different git config files and define conditions in ~/.gitconfig global file. It looks like this:

[includeIf “gitdir:<path_to_organisation1_directory_with_repos>“]
    path = <path_to_organisation1_special_git_config_file>

[includeIf “gitdir:<path_to_organisation2_directory_with_repos>“]
    path = <path_to_organisation2_special_git_config_file>
Daria
  • 61
  • 1
  • 2
3

After a lot of searching I found what I was looking for (but maybe not exactly what I initially asked). The solution I went with was to use ssh. Github has some good help docs to follow here (under authenticating) but basically you have to 1. Generate a ssh key for each account and add it to the ssh agent* 2. Add the ssh key to your github accounts 3. On a mac Update the config file in ~/.ssh/config (scroll down to "choosing between multiple accounts on GitHub or Heroku")

  • Note: The addition of ssh keys to the ssh agent is transient. They last only so long as the agent is running. If you kill it or restart your computer they're lost until you re-add them again.

To fix this, I created a bash function, added it to the bash rc files for all shells so I can just call it as a terminal command. The code is here:

#!/bin/bash

function addSshKeysToAgent() {
  echo "starting ssh agent:"
  echo ""

  eval 'ssh-agent -s'
  echo ""

  echo "adding identities: "
  ssh-add -K ~/.ssh/personal_rsa
  ssh-add -K ~/.ssh/work_rsa
  echo ""

  echo "Private keys loaded into SSH: "
  ssh-add -l -E md5
  echo ""

  echo "ssh keys added to ssh agent."
}

function clone() {
  addSshKeysToAgent
  git clone gh-work:Company/$1
}

Replace personal_rsa and work_rsa with the name of your ssh key file and Company with your organization name (if that's what you want to do), which comes from the config file you updated. Here is how my config file looks:

Host *
 Port 22
 ServerAliveInterval 60
 ForwardAgent yes
 UseKeychain yes
 AddKeysToAgent yes
 IdentityFile ~/.ssh/personal_rsa
 IdentityFile ~/.ssh/work_rsa

Host gh-personal
  Hostname github.com
  User git
  IdentityFile ~/.ssh/personal_rsa
  AddKeysToAgent yes
  UseKeychain yes

Host gh-work
  Hostname github.com
  User git
  IdentityFile ~/.ssh/work_rsa
  AddKeysToAgent yes    
  UseKeychain yes

with all this, you should be able to clone a repo from the terminal with the following command:

clone reponame.git

I sincerely hope this helps someone out. It took me almost a full day of searching and hacking things together to get this working and as I was writing this response I came across a really great gist to do exactly what I wanted here

cbutler
  • 1,111
  • 5
  • 16
  • 32
  • No need to have separate ssh keys for each account. You should have separate ssh keys for each device, and register the public key from each device to each account that needs it. However, to get the email and user name correct for commits one of the other answers here will work (Daria's is great to apply the same settings to all repos in a given folder). – LightCC Sep 14 '22 at 18:27
-2

This guide is probably the one you're looking for.

Take a look at the git config --local part specifically which is the one that's going to apply changes in the repo you're executing the command.

Mattia Righetti
  • 1,265
  • 1
  • 18
  • 31
  • Git credentials are not stored in the local repository but usually handled by the git credential helper. For instance, on Windows, this is stored in the Credential Manager, part of Windows. At most, when dealing with ssh, the username is part of the remote url configuration. – Lasse V. Karlsen May 31 '18 at 09:58