408

I git push my work to a remote Git repository.

Every push will prompt me to input username and password. I would like to avoid it for every push, but how to configure to avoid it?

nbro
  • 15,395
  • 32
  • 113
  • 196
Leem.fin
  • 40,781
  • 83
  • 202
  • 354
  • 6
    An answer is available here: http://stackoverflow.com/questions/6565357/git-push-requires-username-and-password – kbdjockey Dec 21 '11 at 11:02
  • 3
    Long story short: For github, Just follow these two links https://help.github.com/articles/generating-ssh-keys https://help.github.com/articles/changing-a-remote-s-url – JRun Aug 12 '14 at 11:45
  • for bitbucket please follow [this link](https://confluence.atlassian.com/bitbucket/set-up-ssh-for-git-728138079.html) – JDP Oct 13 '16 at 04:11
  • Does this answer your question? [Git push requires username and password](https://stackoverflow.com/questions/6565357/git-push-requires-username-and-password) – T.Todua Dec 27 '21 at 10:12

20 Answers20

447

1. Generate an SSH key

Linux/Mac

Open terminal to create ssh keys:

cd ~                 #Your home directory
ssh-keygen -t rsa    #Press enter for all values

For Windows

(Only works if the commit program is capable of using certificates/private & public ssh keys)

  1. Use Putty Gen to generate a key
  2. Export the key as an open SSH key

Here is a walkthrough on putty gen for the above steps

2. Associate the SSH key with the remote repository

This step varies, depending on how your remote is set up.

  • If it is a GitHub repository and you have administrative privileges, go to settings and click 'add SSH key'. Copy the contents of your ~/.ssh/id_rsa.pub into the field labeled 'Key'.

  • If your repository is administered by somebody else, give the administrator your id_rsa.pub.

  • If your remote repository is administered by your, you can use this command for example:

    scp ~/.ssh/id_rsa.pub YOUR_USER@YOUR_IP:~/.ssh/authorized_keys/id_rsa.pub

3. Set your remote URL to a form that supports SSH 1

If you have done the steps above and are still getting the password prompt, make sure your repo URL is in the form

git+ssh://git@github.com/username/reponame.git

as opposed to

https://github.com/username/reponame.git

To see your repo URL, run:

git remote show origin

You can change the URL with:

git remote set-url origin git+ssh://git@github.com/username/reponame.git

[1] This section incorporates the answer from Eric P

alditis
  • 4,633
  • 3
  • 49
  • 76
First Zero
  • 21,586
  • 6
  • 46
  • 45
  • On Windows: If you are using the git bash window the **terminal** commands also work. The only thing is you must be certain you want to overwrite the default id_rsa file, and you must type `y` when prompted if you want to overwrite the current id_rsa key. – Agustín Amenabar May 06 '13 at 19:49
  • 3
    I ran step 1 and step 2 on ubuntu but it still asks me for u_id passwd. I also ran: ssh -T git@github.com. Still the same. What to do? – Radhika Jan 07 '15 at 07:24
  • 4
    @Rads Have you tried the other answer, by **Eric P**? – yo' Mar 02 '15 at 01:48
  • 2
    @dialex He meant a person that administrates the remote git repository. – selalerer Dec 22 '15 at 11:40
  • Well that person is me, on GitHub. It's my account. Can anyone refer me a link explaining how to perform that last step? – dialex Dec 22 '15 at 12:14
  • 7
    git remote set-url origin git+ssh://git@github.com/username/reponame.git this command is helpful to me. – ArtificiallyIntelligence Jun 18 '16 at 00:09
  • I followed these steps exactly, but am now getting "Permission denied (publickey). fatal: Could not read from remote repository." What's worse is I can't undo the changes, making me unable to use the git repo at all. I've removed the public key from github and removed github from known hosts, and deleted the keys generated, but still can't get back to the password prompt. – bjarkemoensted Feb 09 '17 at 13:18
  • Step 2 needs more info for people not pushing to GitHub and who are administering their own repo remotely. How do you associate the SSH key with the repo?? – brentonstrine Jun 27 '17 at 22:09
  • The `git+ssh` option does not seem to work if my local username is different from my github username. – dinosaur Dec 13 '17 at 00:13
  • A command that is useful to copy the contents of the public key and give yourself access to a github repo from a new machine/newly minted rsa key is to use: `clip < ~/.ssh/id_rsa.pub` and paste that content as a new SSH key in the "SSH and GPG Keys" section of your GitHub Settings. – MattMakes Feb 18 '18 at 15:30
  • step 2 of this answer is not clear for me, I don't have the `~/.ssh` folder in my remote repo. – JustWe Oct 24 '18 at 08:18
  • "If your remote repository is administered by your [sic], you can use this command for example". Your what? – James Ray Feb 01 '19 at 04:01
  • 2
    This worked for me `git remote set-url origin git@github.com:/username/projectname.git` – Kurt Apr 11 '19 at 14:00
  • I met some problem on: How to avoid input username and passwd everytime when make git push? 1) my system is Ubuntu 18.04 2) I created id_rsa.pub and copy it to github website setting page. 3) However, I am still require to input username and passwd when I make `git push` operation. 4) I also tried --globe user.name and user.pasword, however, i was still require passwd when I make `git push` operation. 5) No respose when I use `ssh -T git@github.com` to test SSH connection. Is there any suggestion? – Shicheng Guo Jul 11 '19 at 20:05
  • I'm going to add that git will only take your public key if you name it exactly "id_rsa". If your public/private keys are named in any other way, you will get the following error - Permission denied (publickey). fatal: Could not read from remote repository. – ragzputin Aug 15 '19 at 02:09
  • Note that RSA, especially with its default 1024-bit key is no longer considered secure. Either using 4096+ bits or switching to ed25519 appears to be the current recommendation. See: https://security.stackexchange.com/q/143442/111605 – Mr Fooz Dec 10 '20 at 17:27
  • Note that there are handy scripts to automate step 3, like [this one](https://gist.github.com/m14t/3056747) – Nicolai Weitkemper Aug 23 '21 at 20:47
220

Permanently authenticating with Git repositories,

Run the following command to enable credential caching.

$ git config credential.helper store
$ git push https://github.com/repo.git

Username for 'https://github.com': <USERNAME>
Password for 'https://USERNAME@github.com': <PASSWORD>

Use should also specify caching expire,

git config --global credential.helper 'cache --timeout 7200'

After enabling credential caching, it will be cached for 7200 seconds (2 hours).

Note: Credential helper stores an unencrypted password on a local disk.

Milan
  • 1,743
  • 2
  • 13
  • 36
Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76
  • 4
    Thanks. Can you please explain what the config command does ? I wonder why it does not even ask me for a password the first time I push to a repo. – HelloWorldNoMore May 10 '16 at 21:06
  • 2
    @vivoconunxino Probably because it's inherently unsafe. From the `git` manpage: "Using this helper will store your passwords unencrypted on disk, protected only by filesystem permissions." – cmaster - reinstate monica Dec 15 '17 at 15:33
  • 32
    **This answer is missing a warning about the `store` credential helper storing the password on disk unencrypted.** – cmaster - reinstate monica Dec 15 '17 at 15:34
  • I really like this solution because for some circumstances, a lot of developers can't authenticate with ssh keys to push to their repos. This is a viable workaround if you have to necessarily do https. – Alfredo Gallegos Jan 17 '18 at 23:14
  • @JayPatel - How do I disable `git config credential.helper store` if I do not want it any more? – David R Tribble Jul 25 '18 at 15:51
  • 1
    @DavidRTribble: set it to cache? Or remove the relevant file, see https://git-scm.com/docs/git-credential-store – serv-inc Mar 29 '19 at 14:44
  • 3
    @DavidRTribble: better: `git config --unset credential.helper` – serv-inc Apr 03 '19 at 08:11
  • 1
    Also note that, push isn't necessary, any command which asks for credentials like git fetch will also work. Command git push might give a wrong impression that any config is being pushed to the remote. – 0xc0de May 24 '19 at 11:14
  • @cmaster-reinstatemonica, just to confirm, where exactly `git` stores those credentials? I mean in which file at which location? – Milan Oct 05 '20 at 23:14
  • 1
    @Milan `man git-credential-store` lists the files `~/.git-credentials` and `XDG_CONFIG_HOME/git/credentials`, in that order. Along with the note: "Using this helper will store your passwords unencrypted on disk, protected only by filesystem permissions." – cmaster - reinstate monica Oct 06 '20 at 07:07
128

If you already have your SSH keys set up and are still getting the password prompt, make sure your repo URL is in the form

git+ssh://git@github.com/username/reponame.git

as opposed to

https://github.com/username/reponame.git

To see your repo URL, run:

git remote show origin

You can change the URL with git remote set-url like so:

git remote set-url origin git+ssh://git@github.com/username/reponame.git
c0d3rman
  • 665
  • 6
  • 15
Eric P
  • 4,587
  • 6
  • 24
  • 19
  • This is what fixed my problem after I had used `git init` locally, then tried to push after adding the remote from a new repo on the GitHub website. – sheldonkreger Dec 27 '14 at 01:26
  • 1
    Not sure why this is not part of the accepted answer. Thanks! – jwalk Jan 15 '16 at 07:24
  • 1
    Accepted answer should be composed of actual accepted answer PLUS this one. – Puce Jan 16 '16 at 16:57
  • 3
    You can omit the "git+ssh://" and just use `git@github.com/username/reponame.git` – Ivan Shulev Jan 26 '16 at 15:11
  • 1
    @IvanShulev I did try initially without git+ssh and didn't worked – Antonello Dec 08 '16 at 12:59
  • In case it's a GitHub repo you can automate the change from https to git+ssh by using ``git remote set-url origin `git remote get-url origin | awk -F/ '{ print "git+ssh://git@github.com/" $(NF-1) "/" $(NF) }'`` – Felipe Cortez Mar 09 '19 at 20:05
42

Just use --repo option for git push command. Like this:

$ git push --repo https://name:password@bitbucket.org/name/repo.git
Community
  • 1
  • 1
Pavel
  • 629
  • 6
  • 5
  • 41
    Or you can set this by default with `git remote set-url origin https://name:password@github.com/repo.git` – ACyclic Sep 14 '12 at 10:08
  • I note setting the URL to just a `name@github.com/....git` works too if you don't mind being nagged for the password each time. – timday Apr 01 '20 at 13:44
  • For github users, `git push --repo https://github.com/Username/repo ` It does work without the `.git ` extension – Pe Dro May 13 '20 at 07:45
34

Saving Indefinitely

You can use the git-credential-store via

git config credential.helper store

which stores your password unencrypted in the file system:

Using this helper will store your passwords unencrypted on disk, protected only by filesystem permissions. If this is not an acceptable security tradeoff, try git-credential-cache, or find a helper that integrates with secure storage provided by your operating system.

With a Timeout

Use the git-credential-cache which by default stores the password for 15 minutes.

git config credential.helper cache

to set a different timeout, use --timeout (here 5 minutes)

git config credential.helper 'cache --timeout=300'

Secure Saving Indefinitely (OS X and Windows)

  • 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. Running the following on the command line will enable this feature: git config --global credential.helper osxkeychain. You'll need to store the credentials in the Keychain using the Keychain app as well.
  • 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. [emphases mine]
rholmes
  • 4,064
  • 3
  • 25
  • 34
serv-inc
  • 35,772
  • 9
  • 166
  • 188
  • 1
    For Windows `credential.helper cache` does NOT work. It should be `git config --global credential.helper wincred`. – Paulo Merson Jul 17 '19 at 16:38
  • 2
    This is helpful when you are forced to use https. My git on the server hangs if I use ssh, so I don't have the option of SSH keys. – Sridhar Sarnobat Oct 21 '19 at 18:24
8

I was using the https link (https://github.com/org/repo.git) instead of the ssh link;

git@github.com:org/repo.git  

Switching solved the problem for me!

Matt
  • 74,352
  • 26
  • 153
  • 180
ishan
  • 1,029
  • 1
  • 12
  • 19
7

Step 1 -

Create SSH keys on your linux system using below command

ssh-keygen -t rsa -b 4096 -C "your_email"

It will ask for passphrase and file name (default will be ~/.ssh/id_rsa, ~/.ssh/id_rsa.pub)

Step 2 -

Once files created add public key id_rsa.pub to github account ssh section.

Step 3 -

On your machine add private key id_rsa to ssh-agent using below command

ssh-add ~/.ssh/id_rsa 

Step 4 -

Now add remote url git@github.com:user_name/repo_name.git to your local git repo using below command.

git remote remove origin
git remote add origin git@github.com:user_name/repo_name.git

Thats it.

Anil Agrawal
  • 2,748
  • 1
  • 24
  • 31
7

On Windows operating system use this instead, this works for me:

https://{Username}:{Password}@github.com/{Username}/{repo}.git

e.g.

git clone https://{Username}:{Password}@github.com/{Username}/{repo}.git

git pull https://{Username}:{Password}@github.com/{Username}/{repo}.git

git remote add origin https://{Username}:{Password}@github.com/{Username}/{repo}.git

git push origin master
5

Wire your git client to your OS credential store. For example in Windows you bind the credential helper to wincred:

git config --global credential.helper wincred

Is there a way to skip password typing when using https:// on GitHub?

Community
  • 1
  • 1
Daniel B
  • 4,145
  • 1
  • 21
  • 21
5

Just wanted to point out something about the solution said above several times:

git config credential.helper store

You can use any command that requires a password after this. You don't have to push. (you can also pull for instance) After that, you won't need to type in your username / password again.

4

If your PC is secure or you don't care about password security, this can be achieved very simply. Assuming that the remote repository is on GitHub and origin is your local name for the remote repository, use this command

git remote set-url --push origin https://<username>:<password>@github.com/<repo>

The --push flag ensures this changes the URL of the repository for the git push command only. (The question asked in the original post is about git push command only. Requiring a username+password only for push operations is the normal setup for public repositories on GitHub . Note that private repositories on GitHub would also require a username+password for pull and fetch operations, so for a private repository you would not want to use the --push flag ...)

WARNING: This is inherently unsecure because:

  1. your ISP, or anyone logging your network accesses, can easily see the password in plain text in the URL;

  2. anyone who gains access to your PC can view your password using git remote show origin.

That's why using an SSH key is the accepted answer.

Even an SSH key is not totally secure. Anyone who gains access to your PC can still, for example, make pushes which wreck your repository or - worse - push commits making subtle changes to your code. (All pushed commits are obviously highly visible on GitHub. But if someone wanted to change your code surreptitiously, they could --amend a previous commit without changing the commit message, and then force push it. That would be stealthy and quite hard to notice in practice.)

But revealing your password is worse. If an attacker gains knowledge of your username+password, they can do things like lock you out of your own account, delete your account, permanently delete the repository, etc.


Alternatively - for simplicity and security - you can supply only your username in the URL, so that you will have to type your password every time you git push but you will not have to give your username each time. (I quite like this approach, having to type the password gives me a pause to think each time I git push, so I cannot git push by accident.)

git remote set-url --push origin https://<username>@github.com/<repo>
radfast
  • 538
  • 1
  • 7
  • 14
  • what if password has `@`, it is breaking command thinking password after @ as part of git server? – Pranav Singh Apr 20 '18 at 07:52
  • If your password contains '@' you're on your own here, sorry. You can change your password? – radfast Apr 22 '18 at 11:53
  • Need to raise request to change password as I was doing automation for Continuous Integration having service account(not personal). Right now used SSH Keys for secure login – Pranav Singh Apr 23 '18 at 04:39
  • `git remote set-url --push origin https://@github.com/` needs to be `git remote set-url --push origin https://@github.com//` – TheEagle Aug 01 '21 at 18:30
4

Running the below command solved the problem for me.

git config --global credential.helper wincred

Please refer the below github documentation:

https://help.github.com/articles/caching-your-github-password-in-git/
Krishna
  • 7,290
  • 3
  • 25
  • 25
3

I used the answer that Pavel suggested and it worked for me. My difference was to do it while I was adding the remote like so: git remote add (alias) https://(name:password)@github.com/(the remote address).git

dsmryder
  • 31
  • 3
3

My Solution on Windows:

  1. Right click at directory to push and select "Git Bash Here".
  2. ssh-keygen -t rsa (Press enter for all values)
  3. Check terminal for: Your public key has been saved in /c/Users/<your_user_name_here>/.ssh/id_rsa.pub
  4. Copy public key from specified file.
  5. Go to your GitHub profile => Settings => SSH and GPG keys => New SSH key => Paste your SSH key there => Save
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
3

As far as I know, there are simply two safe ways: ssh or passwd encrypted using a keystore.

SSH

  1. Log in your github;
  2. Visit: https://github.com/settings/keys;
  3. New SSH key;
  4. cat ~/.ssh/id_rsa.pub, paste it there, name and save it (if you have no such file, generate one for yourself by ssh-keygen -t rsa - just Enter for all prompts);
  5. Go to your local repository and update your remote by git remote set-url origin git+ssh://git@github.com/username/reponame.git - you can check it first by git remote -v);
  6. Here you go, just touch t; git add t; git commit -m "test"; git push and confirm yes to enjoy the password-free world.

passwd ENCRYPTED using a keystore

If you just use git config --global credential.helper store as others mentioned, your unencrypted passwords will be just stored in a plain text under ~/.git-credentials which is not safe as it sounds.

Try to encrypt it as

sudo apt-get install libgnome-keyring-dev
sudo make --directory=/usr/share/doc/git/contrib/credential/gnome-keyring
git config --global credential.helper /usr/share/doc/git/contrib/credential/gnome-keyring/git-credential-gnome-keyring

git config --global credential.helper store

In this case, you are using https://git@github.com/username/reponame.git.

Hearen
  • 7,420
  • 4
  • 53
  • 63
2

I recommend using a credential manager to store the GitHub credentials. Using git config --global credential.helper store is not safe as it stores the GitHub password in clear text. For Linux, libsecret is an excellent alternative. For Ubuntu and some other linux distributions you can do the following:

Install:

sudo apt-get update
sudo apt install libsecret-1-0 libsecret-1-dev
sudo apt install gnome-keyring

Create

cd /usr/share/doc/git/contrib/credential/libsecret/
Sudo make 

Configure git to store passwords using libsecret

git config --global credentail.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret

Your git credentials will be stored by libsecret after you enter your password once after this setup.

Paolo
  • 20,112
  • 21
  • 72
  • 113
jakobinn
  • 1,832
  • 1
  • 20
  • 20
1

All this arises because git does not provide an option in clone/pull/push/fetch commands to send the credentials through a pipe. Though it gives credential.helper, it stores on the file system or creates a daemon etc. Often, the credentials of GIT are system level ones and onus to keep them safe is on the application invoking the git commands. Very unsafe indeed.

Here is what I had to work around. 1. Git version (git --version) should be greater than or equal to 1.8.3.

GIT CLONE

For cloning, use "git clone URL" after changing the URL from the format, http://{myuser}@{my_repo_ip_address}/{myrepo_name.git} to http://{myuser}:{mypwd}@{my_repo_ip_address}/{myrepo_name.git}

Then purge the repository of the password as in the next section.

PURGING

Now, this would have gone and

  1. written the password in git remote origin. Type "git remote -v" to see the damage. Correct this by setting the remote origin URL without password. "git remote set_url origin http://{myuser}@{my_repo_ip_address}/{myrepo_name.git}"
  2. written the password in .git/logs in the repository. Replace all instances of pwd using a unix command like find .git/logs -exec sed -i 's/{my_url_with_pwd}//g' {} \; Here, {my_url_with_pwd} is the URL with password. Since the URL has forward slashes, it needs to be escaped by two backward slashes. For ex, for the URL http://kris:passs123@192.168.0.1/proj.git -> http:\\/\\/kris:passs123@192.168.0.1\\/proj.git

If your application is using Java to issue these commands, use ProcessBuilder instead of Runtime. If you must use Runtime, use getRunTime().exec which takes String array as arguments with /bin/bash and -c as arguments rather then the one which takes a single String as argument.

GIT FETCH/PULL/PUSH

  1. set password in the git remote url as : "git remote set_url origin http://{myuser}:{mypwd}@{my_repo_ip_address}/{myrepo_name.git}"
  2. Issue the command git fetch/push/pull. You will not then be prompted for the password.
  3. Do the purging as in the previous section. Do not miss it.
Krishna
  • 11
  • 2
0

Appears that, at least when using TortoiseGIT on Windows, it is possible to create the SSH keys and transfer these to the GIT server using simply:

> ssh-keygen.exe
> ssh-copy-id [username]@[GIT_server]
EquipDev
  • 5,573
  • 10
  • 37
  • 63
0

In my case, using SSH, after adding the public key to GitHub, then setting the remote to something like git@github.com/username/reponame.git, as well as permanently adding the private key locally worked. The local commands where:

ssh-add

ssh-add -K

I guess the latter step might be missing for some people.

maitridan
  • 29
  • 4
0

You have to setup a SSH private key, you can review this page, how to do the setup on Mac, if you are on linux the guide should be almost the same, on Windows you would need tool like MSYS.

RageZ
  • 26,800
  • 12
  • 67
  • 76