1732

I want to use a push and pull automatically in GitExtension, without entering my user and password in a prompt, every time.

So how can I save my credentials in git?

Boris
  • 7,044
  • 6
  • 62
  • 63
Edson Cezar
  • 17,674
  • 4
  • 14
  • 25
  • 8
    You can also authenticate via SSH: https://stackoverflow.com/questions/6565357/git-push-requires-username-and-password – Anton Tarasenko Nov 12 '19 at 00:12
  • 1
    Also see [Is there a way to cache GitHub credentials for pushing commits?](https://stackoverflow.com/q/5343068/608639) and [Configuring user and password with Git Bash](https://stackoverflow.com/q/8840551/608639). – jww Nov 12 '19 at 20:49
  • 1
    @AntonTarasenko my gitconfig says nothing about Proxy, only [user] email = email@dummy.com name = uname [credential] helper = store – guroosh Feb 04 '20 at 15:43

21 Answers21

3111

Attention: This method saves the credentials in plaintext on your PC's disk. Everyone on your computer can access it, e.g. malicious NPM modules.

Run

git config --global credential.helper store

then

git pull

provide a username and password and those details will then be remembered later. The credentials are stored in a file on the disk, with the disk permissions of "just user readable/writable" but still in plaintext.

If you want to change the password later

git pull

Will fail, because the password is incorrect, git then removes the offending user+password from the ~/.git-credentials file, so now re-run

git pull

to provide a new password so it works as earlier.

Dominik
  • 1,428
  • 1
  • 15
  • 31
Neetika
  • 31,372
  • 1
  • 8
  • 7
  • 6
    how is the -u flag important to change password later? – lucidbrot Sep 25 '17 at 16:18
  • 13
    for Debian/Ubuntu use libsecret https://stackoverflow.com/questions/36585496/error-when-using-git-credential-helper-with-gnome-keyring-as-sudo/40312117#40312117 – rofrol Oct 02 '17 at 14:29
  • 3
    @lucidbrot sorry for replying late. git pull -u not working with latest version. I have updated the answer. Hope it will answer your question. – Neetika Nov 13 '17 at 11:21
  • 117
    Note that this will store your username and password in a plain text file at ~/.git-credentials. Anyone can open it and read it. – RoboAlex Sep 24 '18 at 04:13
  • the edit `edited Oct 22 at 23:45` does not make sense. @rogerdpack, you'll run `git pull` to change password? – Reigel Nov 30 '18 at 06:05
  • well, fetch works as well, anything that "re-requires" the password seems to do it :) – rogerdpack Nov 30 '18 at 14:47
  • 1
    It only works for one repository, the repo you are currently in. If I want to work with another repo, I need to do that again – Shamsul Arefin Sajib Jan 20 '19 at 11:42
  • 1
    @ShamsulArefinSajib Use `git config --global ...` – Sean Allred Jan 22 '19 at 00:52
  • 1
    `store` is unsafe, and if my machine gets stolen? I would be doomed..., should be `cache` – Aquarius Power Jan 23 '19 at 04:21
  • 2
    and use this if you want to forget : git config --global credential.helper forget – jacktrade Oct 23 '19 at 10:22
  • 14
    I think it's worth pointing out that one may want to skip the `--global` to only store the password for one repository (when pulling and pushing) but not for any other repository (which might be on a different hoster, with different credentials that one might not want to store for whatever reasons) – pseyfert Nov 26 '19 at 10:04
  • help full this command, for permanently save username and password on git. – jatin chavda Dec 21 '19 at 10:34
  • 1
    @RoboAlex If you are on Linux, the file is protected and can only be read by you. So yes, there is a file, but no, noone except for you can read it. (I don't know about Windows, though.) – vauhochzett Jan 07 '20 at 20:37
  • thanks! this helped to reset my password, that I changed from the GitHub UI previously. – realnikunj Feb 20 '20 at 04:44
  • how can I use this for gitlab @Neetika – anil Feb 28 '20 at 07:23
  • I work with 2 different repositories(office + personal) with different usernames/passwords. Can I save 2 usernames/passwords with different repositories? If yes then how? – Pradeep Singh Mar 08 '20 at 18:21
  • Note that you can create an oauth-token and use that instead of your GitHub password. This slightly improves security, since anyone with the token can still access your repos, but cannot access your GitHub account (through a browser). – AstroFloyd May 20 '20 at 12:50
  • 1
    If using Gitlab or Github, you can create an access token in your account settings, and provide this token instead of your password. The token will be saved in the ~/.git-credentials file instead of your password and can be easily revoked from your account settings. – Mathieu Rollet Feb 17 '21 at 09:22
  • 1
    @RoboAlex's comment is popular but the protections on `~/.git-credentials` are no different than a private key like `~/.ssh/id_rsa`. So if you don't have a password on your private key then `~/git-credentials` is no worse than ssh keys – Jason S Mar 19 '21 at 13:05
  • 1
    it is really great. – Wang Jijun Mar 19 '21 at 17:07
  • [stack answer](https://askubuntu.com/a/959662/443958) shows a stellar way to easily encrypt the token/password in 3 simple steps. – agent18 May 02 '21 at 20:05
457

You can use the git config to enable credentials storage in git.

git config --global credential.helper store

When running this command, the first time you pull or push from the remote repository, you'll get asked about the username and password.

Afterwards, for consequent communications with the remote repository you don't have to provide the username and password.

The storage format is a .git-credentials file, stored in plaintext.

Also, you can use other helpers for the git config credential.helper, namely memory cache:

git config credential.helper cache <timeout>

which takes an optional timeout parameter, determining for how long the credentials will be kept in memory. Using the helper, the credentials will never touch the disk and will be erased after the specified timeout. The default value is 900 seconds (15 minutes).


WARNING : If you use this method, your git account passwords will be saved in plaintext format, in the global .gitconfig file, e.g in linux it will be /home/[username]/.gitconfig

If this is undesirable to you, use an ssh key for your accounts instead.

Farhad Faghihi
  • 9,874
  • 5
  • 27
  • 55
  • 3
    Wish you showed the .gitconfig file - the first command has been overwritten by the second :( – Adam Mar 08 '17 at 13:24
  • 6
    For `git config credential.helper cache` the passwords will not be saved to a file, only stored in memory. See: https://git-scm.com/docs/git-credential-cache – S.A. Jul 20 '19 at 06:11
  • 2
    Does not work. Gives fatal: Authentication failed. Doesnt even ask for password. – Philip Rego Oct 01 '19 at 18:41
  • 6
    Just as an addendum - your private ssh-key will also be stored in plaintext in a user-accessible location, so in essence the same attack surface in both cases. – Falco Jan 07 '20 at 11:48
  • how can I use this for gitlab – anil Feb 28 '20 at 07:22
  • there's no difference for gitlab configs. you can use these commands for github, gitlab, bitbucket,... – Farhad Faghihi Feb 28 '20 at 16:52
  • but setting up ssh is difficult for a first timer like me – Harsha Apr 02 '20 at 19:40
  • That doesnt add the time out, see @Andreas Bigger comment below: git config --global credential.helper 'cache --timeout=3600' – run_the_race Apr 30 '20 at 13:17
  • Does not work for me.. Now I get the error: git: 'credential-cache' is not a git command. See 'git --help'. fatal: Authentication failed for [url] – letie May 01 '20 at 14:14
  • I have to store username and passwords twice, in the above named .gitconfig and in git-credentials, so someone nasty can be sure she/he HAS the right github key;) when checking both. – Timo Nov 13 '20 at 11:41
  • Unless you put a password on your private ssh key, it is also stored in plain text, no more securely than `~/.git-credentials`, by default in `~/.ssh/id_rsa`. So the advice to use an ssh key as more secure, would need to specify a password-protected private key. In which case you'd have to type that password each time, defeating the original purpose, unless you cache it with ssh-agent – Jason S Mar 19 '21 at 13:12
301

Recommended and Secure Method: SSH

Create an ssh Github key. Go to github.com -> Settings -> SSH and GPG keys -> New SSH Key. Now save your private key to your computer.

Then, if the private key is saved as id_rsa in the ~/.ssh/ directory, we add it for authentication as such:

ssh-add -K ~/.ssh/id_rsa


A More Secure Method: Caching

We can use git-credential-store to cache our username and password for a time period. Simply enter the following in your CLI (terminal or command prompt):

git config --global credential.helper cache

You can also set the timeout period (in seconds) as such:

git config --global credential.helper 'cache --timeout=3600'


An Even Less Secure Method

Git-credential-store may also be used, but saves passwords in plain text file on your disk as such:

git config credential.helper store


Outdated Answer - Quick and Insecure

This is an insecure method of storing your password in plain text. If someone gains control of your computer, your password will be exposed!

You can set your username and password like this:

git config --global user.name "your username"

git config --global user.password "your password"
Josh Correia
  • 2,133
  • 1
  • 17
  • 29
Andreas Bigger
  • 3,471
  • 1
  • 9
  • 16
  • 24
    This did not work for me, git clone still asks for the username and password – Oliver Dec 20 '18 at 05:09
  • 13
    i do not recommend storing your password like this because "git config --global -l" would reveal your password on the console – CCC Dec 26 '18 at 08:02
  • 2
    This is not working for me. It is strange that it should work for anyone, since in gitconfig's specification there is no room for "password" – Hamish Todd Nov 28 '19 at 18:12
  • .gitconfig is the different file: .git-credentials located in your home folder contains URL to repo with your username and password in URL, just like git://username:password@server/git/repo Be careful, IT IS SAVED PLAIN TEXT PASSWORD. Use "git config --global credential.helper cache" if you cannot use ssh keys. – skabbit Apr 13 '20 at 07:11
  • When I tried to add user and password mannually the config file broke and I had to clone the repo again. Do not try this.. – letie May 01 '20 at 14:11
  • @LTV, this is a fairly well documented process. Day in and out, people update git username manually (not password please); the issue may well be on your end. – Richard McFriend Oluwamuyiwa Jul 29 '20 at 17:56
  • 1
    The ssh method is preferred, particularly if you create a pass-phrase. I find Git's instructions helpful: https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh – kingaj Aug 19 '20 at 14:39
  • This answer needs a lot of elaboration on the first part "Recommended and Secure Method: SSH". A random user online needs to know the commands to execute to create an SSH key. – Shailen Oct 06 '20 at 18:13
  • Quick and insecure works, you can also change the gitconfig manually. – Timo Nov 16 '20 at 11:00
  • 1
    If you use the SSH method, you'll need to start using SSH URLs instead of git URLs - https://stackoverflow.com/questions/14762034/push-to-github-without-a-password-using-ssh-key – yndolok Feb 24 '21 at 03:32
  • "An even less secure method". Have to disagree. Does anyone here realise your passwordless private ssh key (the whole point of the question is to avoid typing your password) is stored in plain text (by default in `~/.ssh/id_rsa`, no more securely than `~/.git-credentials` ? Also the credential.helper store method does not put the password / token in .gitconfig Also due to quirks of sites like GitHub you get more fine-grained control over permissions on a Personal Access Token than an SSH public key. So for that specific case I'd say PAT with credential helper is "more secure" – Jason S Mar 19 '21 at 13:21
  • If you don't have a PIN for authenticator use `ssh-add ~/.ssh/id_rsa` instead. (Omit -K) – Halfacht May 20 '21 at 10:36
122

Turn on the credential helper so that Git will save your password in memory for some time:

In Terminal, enter the following:

# Set git to use the credential memory cache
git config --global credential.helper cache

By default, Git will cache your password for 15 minutes.

To change the default password cache timeout, enter the following:

# Set the cache to timeout after 1 hour (setting is in seconds)
git config --global credential.helper 'cache --timeout=3600'

From GitHub Help

Koraktor
  • 35,732
  • 8
  • 65
  • 94
simhumileco
  • 21,911
  • 14
  • 106
  • 90
  • 7
    you're the only one who suggested the global version which is IMPORTANT, cause it got ruined for me every time I re-cloned the repo – Xerus Jan 24 '18 at 21:56
  • 5
    How to set the timeout to infinity? I never want to enter my password again. – Avamander Apr 04 '18 at 19:33
  • 8
    @Avamander just replace the `cache` part with `store`. So, the full command will be: `git config --global credential.helper store`. Note that this will store Your password in a open-text file (without any encryption, so to say). – Aleksandar Apr 20 '18 at 06:40
  • 3
    @Casper That doesn't work with more than one account, the password isn't fetched from the store based on the e-mail like it should, instead the first one in the list is taken. – Avamander Apr 21 '18 at 09:53
  • 2
    @Avamander hm.. is that supposed to be like that or it might be a bug? What is the maximum value for the `--timeout` parameter? – Aleksandar Apr 21 '18 at 12:50
  • 1
    If you do this approach on Windows you will need to install the latest windows credential manager https://github.com/Microsoft/Git-Credential-Manager-for-Windows/releases/latest or else you will get fatal HttpRequestExceptions – David May 30 '19 at 15:31
  • After doing that, when I use git, I get the message "git: 'credential-cache' is not a git command. See 'git --help'." – John Deighan Sep 17 '19 at 02:51
  • @user1738579 Do you use GIT on Windows? Maybe this will help you: https://stackoverflow.com/questions/11693074/git-credential-cache-is-not-a-git-command – simhumileco Sep 17 '19 at 11:12
67

You can edit the ~/.gitconfig file to store your credentials

sudo nano ~/.gitconfig

Which should already have

[user]
        email = your@email.com
        user = gitUSER

You should add at the bottom of this file.

[credential]
        helper = store

The reason I recommend this option is cause it is global and if at any point you need to remove the option you know where to go and change it.

ONLY USE THIS OPTION IN YOU PERSONAL COMPUTER.

Then when you pull | clone| enter you git password, in general, the password will be saved in ~/.git-credentials in the format

https://GITUSER:GITPASSWORD@DOMAIN.XXX

WHERE DOMAIN.XXX COULD BE GITHUB.COM | BITBUCKET.ORG | OTHER

See Docs

Restart your terminal.

T04435
  • 7,493
  • 1
  • 41
  • 47
  • 2
    Don't forget to restart git bash window. Only when I did that, it worked for me. – sofs1 Jan 15 '19 at 22:33
  • The other alternative not recommended would be to leave the [credential] and the git-credentials and store the password direct in the .gitconfig. Either manually or with git config .. – Timo Nov 16 '20 at 10:59
  • Restarting means `exec bash` – Timo Nov 16 '20 at 12:04
47

Just put your credentials in the Url like this:

https://Username:Password@github.com/myRepoDir/myRepo.git

You may store it like this:

git remote add myrepo https://Userna...

...example to use it:

git push myrepo master


Now that is to List the url aliases:

git remote -v

...and that the command to delete one of them:

git remote rm myrepo

Topera
  • 11,264
  • 14
  • 59
  • 100
Nadu
  • 2,058
  • 1
  • 20
  • 27
  • 9
    You can also leave your password out of the URL so Git will ask for your password, but not your username. – kangaroo Jul 19 '18 at 00:02
  • 1
    It's much easy and secure to use SSH instead of HTTP. So, you could keep your Git password secret and only use your SSH-Key. Information about SSH on Git: https://help.github.com/en/enterprise/2.16/user/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent `git@github.com:Username/myRepo.git` – Loïch Jan 06 '20 at 14:32
  • 2
    Having your password in the url will save it to your terminal's history so this way isn't very secure. – Josh Correia Feb 19 '20 at 22:03
  • 1
    Note that a Wiki can currently only be cloned through `https`, not `ssh`, so a solution like this can be useful there. Note also that you can use an oauth-token, which is marginally more secure that your GitHub password. – AstroFloyd May 20 '20 at 15:22
41

For global setting, open the terminal (from any where) run the following:

  git config --global user.name "your username"
  git config --global user.password "your password"

By that, any local git repo that you have on your machine will use that information.

You can individually config for each repo by doing:

  • open terminal at the repo folder.
  • run the following:

    git config user.name "your username"
    git config user.password "your password"
    

It affects only that folder (because your configuration is local).

Máté
  • 2,195
  • 3
  • 16
  • 25
Tuananhcwrs
  • 543
  • 4
  • 4
  • 11
    Isn't it dangerous having such credentials in a simple config file viewable by anyone? – bool3max May 05 '19 at 15:28
  • 1
    your question should be for another topic. Here we discus about how to config name and password for git global and locally. – Tuananhcwrs May 06 '19 at 07:30
  • 16
    I'd just like to add that asking about possible security issues of an answer has a place here as it directly influences its quality and potentially thousands of users implementing it. – Michael Kargl May 11 '19 at 16:51
  • 2
    doesn't work for me , added ```--local``` tag but again nothing happens – PayamB. Dec 04 '19 at 11:58
  • I think @bool3max question is a fair one. No-one really answered though. `git config --global` is still only accessible to the user, at `~/.gitconfig`, so in that sense it is probably no worse than a private ssh key (with no password protection) in your home directory, or things like `~/.aws/config` for aws cli. If you used `--system` for git config it would be available to all users on your computer, so better not do that, and no need to, although it would still be limited to the other users on your computer. – Jason S Mar 18 '21 at 00:21
33

After going over dozens of SO posts, blogs, etc, I tried out every method, and this is what I came up with. It covers EVERYTHING.

The Vanilla DevOps Git Credentials & Private Packages Cheatsheet

These are all the ways and tools by which you can securely authenticate git to clone a repository without an interactive password prompt.

  • SSH Public Keys
    • SSH_ASKPASS
  • API Access Tokens
    • GIT_ASKPASS
    • .gitconfig insteadOf
    • .gitconfig [credential]
    • .git-credentials
    • .netrc
  • Private Packages (for Free)
    • node / npm package.json
    • python / pip / eggs requirements.txt
    • ruby gems Gemfile
    • golang go.mod

The Silver Bullet

Want Just Works™? This is the magic silver bullet.

Get your Access Token (see the section in the cheatsheet if you need the Github or Gitea instructions for that) and set it in an environment variable (both for local dev and deployment):

MY_GIT_TOKEN=xxxxxxxxxxxxxxxx

For Github, copy and run these lines verbatim:

git config --global url."https://api:$MY_GIT_TOKEN@github.com/".insteadOf "https://github.com/"
git config --global url."https://ssh:$MY_GIT_TOKEN@github.com/".insteadOf "ssh://git@github.com/"
git config --global url."https://git:$MY_GIT_TOKEN@github.com/".insteadOf "git@github.com:"

Congrats, now any automated tool cloning git repositories won't be obstructed by a password prompt, whether using https or either style of ssh url.

Not using Github?

For other platforms (Gitea, Github, Bitbucket), just change the URL. Don't change the usernames (although arbitrary, they're needed for distinct config entries).

Compatibility

This works locally in MacOS, Linux, Windows (in Bash), Docker, CircleCI, Heroku, Akkeris, etc.

More Info

See the ".gitconfig insteadOf" section of the cheatsheet.

Security

See the "Security" section of the cheatsheet.

coolaj86
  • 64,368
  • 14
  • 90
  • 108
  • 3
    What I was after was `git config --global credential."https://somegithost.com".username MyUserName`, which is _in_ your cheatsheet, but not anywhere else in this answer thread. That particular solution doesn't answer the OP's question, but it answered mine, so thanks! – TheDudeAbides Feb 14 '20 at 04:14
  • 1
    Finally: documentation on non-interactive global usage of an API access token for pipelines without Jenkins credentials helpers or Vault. – Matt Schuchard Dec 10 '20 at 16:12
30

You can use git-credential-store to store your passwords unencrypted on the disk, protected only by the permissions of the file system.

Example

$ 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]

You can check the credentials stored in the file ~/.git-credentials

For more info visit git-credential-store - Helper to store credentials on disk

30

I think it's safer to cache credentials, instead of store forever:

git config --global credential.helper 'cache --timeout=10800'

now you can enter your username and password(git pull or ...), and keep using git for next 3 hours.

nice and safe.

Timeout is in seconds(3 hours in the example).

Amin Shojaei
  • 1,943
  • 1
  • 14
  • 26
  • 3
    syntax is invalid, quotes must be around 'cache --timeout=' – fmigneault Sep 29 '20 at 21:10
  • @fmigneault It's the exact copy of that command which worked for me on Ubuntu terminal and last version of Git. What software are you using? – Amin Shojaei Dec 11 '20 at 12:33
  • 1
    I'm using Ubuntu 20.04 with zsh. When I ran the command as in the original post, only `'cache'` part seemed to be interpreted as the value of `credentials.helper` to set, and `--timeout` was interpreted as another option of `git config`, which made syntax error. This makes sense to be honest considering how command options are usually parsed. Using `'cache --timeout='` in quotes makes it explicit that this whole thing must be the set value under `credential.helper` and his just more portable. No space for interpretation how to read `--timemout` part. – fmigneault Dec 14 '20 at 06:12
  • 2
    @fmigneault I double checked with `git config --global -e` and as the Walter white said, "You are God damn right!" :). thanks, I'll edit my answer – Amin Shojaei Dec 15 '20 at 13:04
20

You will be more secure if you use SSH authentication than username/password authentication.

If you are using a Mac, SSH client authentication is integrated into the MacOS keychain. Once you have created an SSH key, type into your terminal:

ssh-add -K ~/.ssh/id_rsa

This will add the SSH private key to the MacOS keychain. The git client will use ssh when it connects to the remote server. As long as you have registered your ssh public key with the server, you will be fine.

vy32
  • 24,271
  • 30
  • 100
  • 197
Birol Efe
  • 1,325
  • 12
  • 12
  • 1
    should be k not K? – dez93_2000 Apr 28 '18 at 15:23
  • 1
    FYI: I am working on a Mac. Having said that, from the "man" info: "-k" When loading keys into or deleting keys from the agent, process plain private keys only and skip certificates. "-K" When adding identities, each passphrase will also be stored in the user's keychain. When removing identities with -d, each passphrase will be removed from it. – Birol Efe Apr 30 '18 at 11:35
  • 3
    I don't think this work for https-based repositories. – zakmck May 19 '18 at 13:49
  • 6
    This answer seems to confuse passwords (HTTPS repos) with SSH private keys. – Steve Bennett May 23 '18 at 04:51
  • 1
    Yes, my recommended solution is for SSH, not HTTPS. Hence, the command "ssh-add". For HTTPS it would be the solution mentioned above "git credential-osxkeychain" (on Mac). – Birol Efe Jul 20 '18 at 10:25
  • 2
    I disagree. You can't be more secure by giving access to the host an `SSH` access. Using HTTP authentication, someone who steal the credentials would only have access to `GitHub/GitLab`. Also token are designed to have a limited life. – Dimitri Kopriwa Oct 01 '18 at 20:50
13

In that case you need git credential helper to tell git to remember your GitHub password and username by using following command line :

git config --global credential.helper wincred 

and if you are using repo using SSH key then you need SSH key to authenticate.

Karol Dowbecki
  • 38,744
  • 9
  • 58
  • 89
shaw
  • 131
  • 1
  • 2
  • 1
    `wincred` is obsoleted, see https://stackoverflow.com/a/53184233/5281824 – weaming Jul 15 '20 at 07:46
  • To add to what @weaming said, on Windows, manager-core is now bundled with the Git installer for Windows https://github.com/microsoft/Git-Credential-Manager-Core – Jason S Mar 19 '21 at 13:35
13

None of the answers above worked for me. I kept getting the following every time I wanted to fetch or pull:

Enter passphrase for key '/Users/myusername/.ssh/id_rsa':


For Macs

I was able to stop it from asking my passphrase by:

  1. Open config by running: vi ~/.ssh/config
  2. Added the following: UseKeychain yes
  3. Saved and quit: Press Esc, then enter :wq!

For Windows

I was able to get it to work using the info in this stackexchange: https://unix.stackexchange.com/a/12201/348665

Gene Parcellano
  • 4,883
  • 3
  • 30
  • 38
9

Apart from editing the ~/.gitconfig file, that you can do if you ask:

git config --local --edit

or

git config --global --edit

Note to always use single quotes:

git config --local user.name 'your username'
git config --local user.password 'your password'

or

git config --global user.name 'your username'
git config --global user.password 'your password'

Your username and password may use some characters that would break your password if you use double quotes.

--local or --global means configuration params are saved for the project or for the os user.

prosti
  • 27,149
  • 7
  • 127
  • 118
9

Store username and password in .git-credentials

.git-credentials is where your username and password(access token) is stored when you run git config --global credential.helper store, which is what other answers suggest, and then type in your username and password or access token:

https://${username_or_access_token}:${password_or_access_token}@github.com

So, in order to save the username and password(access token):

git config —-global credential.helper store
echo “https://${username}:${password_or_access_token}@github.com“ > ~/.git-credentials

This is very useful for github robot, e.g. to solve Chain automated builds in the same docker repository by having rules for different branch and then trigger it by pushing to it in post_push hooker in docker hub.

An example of this can be seen here in stackoverflow.

JiaHao Xu
  • 1,556
  • 8
  • 21
9

just use

git config --global credential.helper store

and do the git pull, it will ask for username and password, from now on it will not provide any prompt for username and password it will store the details

Srikanth Josyula
  • 446
  • 4
  • 12
  • 8
    You've just basically repeated the [accepted answer](https://stackoverflow.com/a/35942890/11342519). – jsamol Sep 07 '19 at 12:43
7

If you are using the Git Credential Manager on Windows...

git config -l should show:

credential.helper=manager

However, if you are not getting prompted for a credential then follow these steps:

  1. Open Control Panel from the Start menu
  2. Select User Accounts
  3. Select Manage your credentials in the left hand menu
  4. Delete any credentials related to Git or GitHub

Also ensure you have not set HTTP_PROXY, HTTPS_PROXY, NO_PROXY environmental variables if you have proxy and your Git server is on the internal network.

You can also test Git fetch/push/pull using git-gui which links to credential manager binaries in C:\Users\<username>\AppData\Local\Programs\Git\mingw64\libexec\git-core

Armfoot
  • 4,137
  • 2
  • 39
  • 58
6

Check official git documentation:

If you use the SSH transport for connecting to remotes, it’s possible for you to have a key without a passphrase, which allows you to securely transfer data without typing in your username and password. However, this isn’t possible with the HTTP protocols – every connection needs a username and password. This gets even harder for systems with two-factor authentication, where the token you use for a password is randomly generated and unpronounceable.

Fortunately, Git has a credentials system that can help with this. Git has a few options provided in the box:

  • The default is not to cache at all. Every connection will prompt you for your username and password.

  • The “cache” mode keeps credentials in memory for a certain period of time. None of the passwords are ever stored on disk, and they are purged from the cache after 15 minutes.

  • The “store” mode saves the credentials to a plain-text file on disk, and they never expire. This means that until you change your password for the Git host, you won’t ever have to type in your credentials again. The downside of this approach is that your passwords are stored in cleartext in a plain file in your home directory.

  • If you’re using a Mac, Git comes with an “osxkeychain” mode, which caches credentials in the secure keychain that’s attached to your system account. This method stores the credentials on disk, and they never expire, but they’re encrypted with the same system that stores HTTPS certificates and Safari auto-fills.

  • If you’re using Windows, you can install a helper called “Git Credential Manager for Windows.” This is similar to the “osxkeychain” helper described above, but uses the Windows Credential Store to control sensitive information. It can be found at https://github.com/Microsoft/Git-Credential-Manager-for-Windows.

You can choose one of these methods by setting a Git configuration value:

$ git config --global credential.helper cache

$ git config --global credential.helper store

https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage

Sma Ma
  • 1,729
  • 14
  • 29
4

From the comment by rifrol, on Linux Ubuntu, from this answer, here's how in Ubuntu:

sudo apt-get install libsecret-1-0 libsecret-1-dev
cd /usr/share/doc/git/contrib/credential/libsecret
sudo make
git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret

Some other distro's provide the binary so you don't have to build it.

In OS X it typically comes "built" with a default module of "osxkeychain" so you get it for free.

rogerdpack
  • 50,731
  • 31
  • 212
  • 332
4

For windows users look at the .gitconfig file and check what has been configured for the credential helper if you have the following...

[credential "helperselector"] selected = wincred

you'll find the credentials in the Windows Credential Manager.

enter image description here

There you can edit the credential.

EDIT: Wincred has been deprecated, see...

https://github.com/git-for-windows/git-sdk-64/tree/main/mingw64/doc/git-credential-manager

So alternatively you may want to reconfigure git to use the built-in GIT credential manager...

git config --global credential.helper manager
Mick
  • 5,754
  • 4
  • 39
  • 60
  • I am having an issue with the manager-core helper, where my credentials disappear every day - Any clue on how to solve this? – Jonathan Dec 22 '20 at 17:26
  • The edit was the answer I was looking for, THANKS! – generic-user Jan 19 '21 at 12:48
  • @Jonathan The Credential manager bundled with Git for Windows changed a few times recently so the easiest option is to just download the newest Git for Windows and install over the top and select the credential manager in the installer. The credential manager is a separate utility, which you can see it on GitHub at https://github.com/microsoft/Git-Credential-Manager-Core, but better to just let the Git for Windows installer, install it, so you know you have version compatibility – Jason S Mar 19 '21 at 13:46
3

After reading the thread in full and experimenting with most of the answers to this question, I eventually found the procedure that works for me. I want to share it in case someone has to deal with a complex use case but still do not want to go through the full thread and the gitcredentials, gitcredentials-store etc. man pages, as I did.

Find below the procedure I suggest IF you (like me) have to deal with several repositories from several providers (GitLab, GitHub, Bitbucket, etc.) using several different username / password combinations. If you instead have only a single account to work with, then you might be better off employing the git config --global credential.helper store or git config --global user.name "your username" etc. solutions that have been very well explained in previous answers.

My solution:

  1. unset global credentials helper, in case some former experimentation gets in the way :)

> git config --global --unset credentials.helper

  1. move to the root directory of your repo and disable the local credential helper (if needed)

> cd /path/to/my/repo

> git config --unset credential.helper

  1. create a file to store your repo's credentials into

> git config credential.helper 'store --file ~/.git_repo_credentials'

Note: this command creates a new file named ".git_repo_credentials" into your home directory, to which Git stores your credentials. If you do not specify a file name, Git uses the default ".git_credentials". In this case simply issuing the following command will do:

> git config credential.helper store

  1. set your username

git config credential.*.username my_user_name

Note: using "*" is usually ok if your repositories are from the same provider (e.g. GitLab). If instead your repositories are hosted by different providers then I suggest to explicitly set the link to the provider for every repository, like in the following example (for GitLab):

git config credential.https://gitlab.com.username my_user_name

At this point if you issue a command requiring your credentials (e.g. git pull) you will be asked for the password corresponding to "my_user_name". This is only required once because git stores the credentials to ".git_repo_credentials" and automatically uses the same data at subsequent accesses.

Sal Borrelli
  • 1,274
  • 11
  • 13