Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store credentials locally per repo in Git Credential Manager for Windows?

Tags:

git

windows

I have 2 github accounts (work & personal) for which I want to store credentials in my Windows 10 (in secure way).

git config --global credential.helper manager command sets only single username & password which conflicts between personal repo and work repo in my machine. Both repo are cloned using HTTPS.

I want to store and access different credentials probably based on repo username. Is it possible?

I know SSH is an option but I would like to know the way for HTTPS.

like image 607
GorvGoyl Avatar asked Sep 07 '18 11:09

GorvGoyl


People also ask

How do I store multiple Git-credentials in Windows?

Just prefix the origin path with your username and @, like so: [email protected]/repo/reporepo. git This allows the Windows credential manager to store multiple logins.

How do I store credentials in Git credential Manager?

You can use git-credential-store to store your passwords unencrypted on the disk, protected only by the permissions of the file system. You can check the credentials stored in the file ~/.git-credentials . For more information, visit git-credential-store - Helper to store credentials on disk. Save this answer.

Where does Git store credentials locally?

The default path for the git credential store is $HOME/. git-credentials (or $XDG_CONFIG_HOME/git/credentials, if the previous location doesn't exist).

Where does Git store credentials on Windows?

It is located at %UserProfile%\. git-credentials , which corresponds to C:\Users\<username>\.


2 Answers

Username in URL

On Bitbucket, you can add a username to the HTTPS URL of your remote:

  • use: https://[email protected]/path/repo.git
  • instead of: https://bitbucket.org/path/repo.git

Since the URL is technically different, you can add both your work and personal forks as remotes for the same repository if you want to. Issue #749 for the legacy Git Credential Manager suggests that the username-in-URL technique works on GitHub as well, but other repository hosts may not support it.

Set useHttpPath

More generally, you can use credential.useHttpPath to split credential management for multiple repositories run by the same host. The quote below from the legacy Git Credential Manager documentation includes command-line suggestions for setting it on bitbucket.com, but you can modify for your own purposes or take a look at the longer example text in the newer GCM Core docs.

useHttpPath

Instructs Git to supply the path portion of the remote URL to credential helpers. When path is supplied, the GCM will use the host-name + path as the key when reading and/or writing credentials.

Note: This option changes the behavior of Git.

Supports true or false. Defaults to false.

git config --global credential.bitbucket.com.useHttpPath true

As rjmunro notes in the comments, you can drop --global to use the path for only the current repo. If so, you may as well drop the hostname, too:

git config credential.useHttpPath true

This second method does not help if you want to log into the exact same repo with different credentials on a whim. (See legacy GCM #363.)

like image 150
Michael Avatar answered Oct 04 '22 21:10

Michael


You could do it with a different helper e.g. git-credential-store which takes an optional parameter for a credential file path. You could set this in local config in each repo, with a different credentials file for each repository.

Alternatively use the suggestion in the link in @phd's comment which should work for Git Credential Manager For Windows

like image 29
rbennett485 Avatar answered Oct 04 '22 21:10

rbennett485