how to set up username and passwords for different git repos?

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

  User skitt

and then I can clone using

git clone ssh://

(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.

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.