7

I have two different github accounts and two repositories. When cloning the repositories I have used the correct username.

But When I want to commit I am not able to choose between the users. It picks up the global git user by default.

Is there a option to choose which credential to use at the point of the commit in SourceTree.

Liviu
  • 1,859
  • 2
  • 22
  • 48
karuthan
  • 241
  • 3
  • 6

4 Answers4

6

This worked for me on macOS (... only)

Go to Repository > Repository Settings > Remotes and edit the repository path by adding the user name (like https://<user name>@github.com, by exemple https://trentreznor@github.com/landron/Problems.gitinstead of just https://github.com/landron/Problems.git). Then, at the first push (tentative), SourceTree will ask for password (and store it to Keychain if you're on macOS). I found the idea on the Atlassian support site.

Windows update I did not succeed in doing the same because I did not convince SourceTree to make/use two accounts in Credentials Manager: git:https://<user1>@github.com and git:https://<user2>@github.com (they can be created manually by "Add a generic credential" in Control Panel\User Accounts\Credential Manager).

Identification update @Simeon Visser's answer matters (at least) for github statistics (contributions) because github identifies the user by email address (see "The email address used for the commits is associated with your GitHub account." here).

Liviu
  • 1,859
  • 2
  • 22
  • 48
  • Please tell me why do you downvote ? What can I improve ? Is the other answer better - it did not work for me ... I agree this is not a good answer, at least not for Windows, but it might still be helpful with all the tried I've made. – Liviu Dec 01 '19 at 20:19
  • For Windows, found it uses default github.com accounts to access any of the github repos. For a quite manual workaround, if u want to access repo A, u can switch default account to account for repo A. Then, repo B, switch your default account to account for repo B. U can set default account for github.com on Tools > Options > Authentication > Accounts > Expand the account u'll able to see Set as default. Not the best solution, but at least don't need to re-login every times switch repo on different github account. – sanme98 Sep 21 '22 at 07:38
1

A simple workaround if you have multiple github accounts configured in Sourcetree is:

  • Go to Tools -> Options -> Authentication
  • Click Set As Default on the account you want to use
  • Click Yes on the popup and now you can push your code
  • Repeat the above to go back to your previous default account

Sourcetree Multiple Github Accounts

GeorgeP
  • 784
  • 10
  • 26
0

Go to Repository > Repository Settings > Advanced and untick the box "Use global user settings".

You can then configure a different username and email address there. This changes the details used for the commit. You still need to make sure that git has the ability to push to the repository (you can edit the configuration file of that repository to do that).

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
0

https://fofxacademy.com/how-to-setup-git-on-your-pc-for-multiple-github-accounts/

Two Simple Steps to Configure Git for Multiple GitHub Accounts on Windows

  1. Enable Windows Credential Manager

    git config --list

    git config --global credential.helper wincred

  2. Enable WCM for Multiple Credentials

    git config --global credential.useHttpPath true

Anh Duy
  • 1,145
  • 15
  • 25