53

When I do

git push

I get the command prompt like

Username for 'https://github.com':

then I enter my username manually like

Username for 'https://github.com': myusername

and then I hit Enter and I get prompt for my password

Password for 'https://[email protected]':

I want the username to be written automatically instead of manually having to type it all the time.

I tried doing it with xdotool but it didn't work out.

I have already done

git config --global user.name myusername
git config --global user.email [email protected]

but still it always asks for me to type manually

Braiam
  • 35,380
  • 25
  • 108
  • 167
GypsyCosmonaut
  • 3,988
  • 7
  • 38
  • 62
  • you know you can store credentials with git, right? – Diego Roccia Jul 18 '17 at 13:41
  • 1
    You should set up an SSH key on GitHub and use that instead. – Stephen Kitt Jul 18 '17 at 13:42
  • @DiegoRoccia yes, mentioned that as well in the question, but it doesn't help. – GypsyCosmonaut Jul 18 '17 at 13:45
  • 2
    You can use as `git config credential.helper store` described here: https://stackoverflow.com/questions/11403407/git-asks-for-username-every-time-i-push In this case you do not store the password in clear text in the origin URL, but in a file in you profile. (Also not encrypted) – Oleg Rudenko Mar 15 '19 at 17:43

5 Answers5

59

In Terminal, enter the following to enable credential memory:

$ git config --global credential.helper cache

You may update the default password cache timeout (in seconds):

# This cache timeout is in seconds
$ git config --global credential.helper 'cache --timeout=3600' 

You may also use (but please use the single quotes, else double quotes may break for some characters):

$ git config --global user.name 'your user name'
$ git config --global user.password 'your password'
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
prosti
  • 998
  • 1
  • 7
  • 18
  • 3
    how to cache forever? – R. Gurung Jul 05 '20 at 17:16
  • 3
    @R.Gurung Use `git config credential.helper 'store` in that case, but _be aware_ that this stores your `git` credentials on disk in plain-text, without any encryption whatsoever. (`~/.git-credentials`) – Per Lundberg Dec 08 '20 at 11:29
  • 3
    Note, the above snippet should be --global flagged as well and close its opening `'` such that it reads: `git config --global credential.helper 'store'` – Albert Renshaw Mar 09 '21 at 01:29
  • If you want the config to be specific of a website (e.g. only for github.com, but not gitlab), you have to use `git config credential.https//github.com ...` instead of `git config credential.helper ...` – PlasmaBinturong Jan 19 '22 at 18:17
21

Actually what you did there is setting up the author information, just for the commits. You didn't store the credentials. credentials can be stored in 2 ways:

  1. using the git credential functions: https://git-scm.com/docs/git-credential-store
  2. change the origin url to "https://username:[email protected]".
  3. a third alternative is to use an ssh key (as @StephenKitt said). For github configuration, you can find all needed information in GitHub help page
Braiam
  • 35,380
  • 25
  • 108
  • 167
Diego Roccia
  • 823
  • 5
  • 9
  • 1
    adding username and password to origin url is not good becouse of security reasons but if you feel yourself in secure then this is best path. – kodmanyagha Mar 18 '20 at 16:38
11

Copied this from git scm

$ git config credential.helper store
$ git push http://example.com/repo.git
Username: <type your username>
Password: <type your password>
[several days later]
$ git push http://example.com/repo.git

[your credentials are used automatically]

2

In linux (Ubuntu 18.04) the username / password can be saved in the file ~/.git-credentials, just edit the file to use your new username / password.

The file format is quiet easy to understand and manipulate, each line contains credentials for one user / domain, in the following format:

https://<username>:<password>@github.com
https://<username2>:<password2>@bitbucket.com
...
Ohad Cohen
  • 148
  • 4
0

Try git-credential-oauth, included in many Linux distributions.

No more passwords! No more personal access tokens! No more SSH keys!

git-credential-oauth is a Git credential helper that securely authenticates to GitHub, GitLab, BitBucket and Gerrit using OAuth.

The first time you push, the helper will open a browser window to authenticate. Subsequent pushes within storage lifetime require no interaction.

Colonel Panic
  • 453
  • 2
  • 8
  • 19