30
─[$] cat ~/.gitconfig

[user]
    name = Shirish Agarwal
    email = xxxx@xxxx.xxx
[core]
    editor = leafpad
    excludesfiles = /home/shirish/.gitignore
    gitproxy = \"ssh\" for gitorious.org
[merge]
    tool = meld
[push]
    default = simple
[color]
    ui = true
    status = auto
    branch = auto

Now I want to put my git credentials for github, gitlab and gitorious so each time I do not have to lookup the credentials on the browser. How can this be done so it's automated ?

I am running zsh

1

3 Answers 3

28

Using SSH

The common approach for handling git authentication is to delegate it to SSH. Typically you set your SSH public key in the remote repository (e.g. on GitHub), and then you use that whenever you need to authenticate. You can use a key agent of course, either handled by your desktop environment or manually with ssh-agent and ssh-add.

To avoid having to specify the username, you can configure that in SSH too, in ~/.ssh/config; for example I have

Host git.opendaylight.org
  User skitt

and then I can clone using

git clone ssh://git.opendaylight.org:29418/aaa

(note the absence of a username there).

Using gitcredentials

If the SSH approach doesn't apply (e.g. you're using a repository accessed over HTTPS), git does have its own way of handling credentials, using gitcredentials (and typically git-credential-store). You specify your username using

git config credential.${remote}.username yourusername

and the credential helper using

git config credential.helper store

(specify --global if you want to use this setup everywhere).

Then the first time you access a repository, git will ask for your password, and it will be stored (by default in ~/.git-credentials). Subsequent accesses to the repository will use the stored password instead of asking you.

Warning: This does store your credentials plaintext in your home directory. So it is inadvisable unless you understand what this means and are happy with the risk.

4
  • 1
    can you elaborate a bit about credential.${remote} and credential.helper. Is there some documentation you could point out as this is the first time I am reading about it.
    – shirish
    Jan 8, 2017 at 19:14
  • I linked to the manpages in my answer, that's the documentation. ${remote} is the protocol and hostname part of the URI you want to clone, helper points to a helper program. Jan 8, 2017 at 19:22
  • 1
    There's also a chapter of Pro Git on the topic. Jan 8, 2017 at 19:24
  • Thanks @zahra_oveyedzade, it’s already included in the answer ;-). Feb 9, 2021 at 8:00
9

For those finding this later -- I had difficulties with this and finally made it work

https / credentials.helper / Ubuntu

  1. Unset Globally:
    git config --global --unset credentials.helper
  2. Unset locally: (In each repo) git config --unset credential.helper
  3. Create credential file for each repo: (inside each repo)

    git config credential.helper 'store --file ~/.git_reponame_credentials'
    

Not saying it is the best way or the only way - but it worked for me after several frustrating hours.

4
  • seems to be similar to what stephen kitt shared above.
    – shirish
    May 12, 2018 at 15:20
  • 3
    @shirish not really, the third point was the one I needed - option to specify where the files is located. Feb 6, 2019 at 21:38
  • @shirish; Also, Kitts instructions don't specify un setting the credentials, which was a crucial component for me since I had already set them globally.
    – SteveJ
    Sep 11, 2019 at 19:18
  • I also had difficulty with this as it isn't as if all the git repos. you will get would be from the same source/store. The second one is better but much more frustating to work in reality. Still thank you for sharing. @SteveJ
    – shirish
    Sep 12, 2019 at 12:47
0

Chances are that you have another account in github with those SSH keys. If that's the case:

  1. Logout from github
  2. Login with the another account
  3. Remove the SSH keys
  4. Logout from github
  5. Login with your wanted user
  6. Assign the SSH keys to the wanted user
  7. Try again the git command, such as "git push"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.