Git asks for username every time I push

LinuxGitGithub

Linux Problem Overview


Whenever I try to push into my repo git asks for both username & password.

I have no problem in re-entering my password each time but the problem is in entering username. I use https to clone my repository.

So, how can I configure git so that it doesn't asks for username on each git push.

I am new to linux but IIRC in windows git push only asks for password.

Linux Solutions


Solution 1 - Linux

Edit (by @dk14 as suggested by moderators and comments)

WARNING: If you use credential.helper store from the answer, your password is going to be stored completely unencrypted ("as is") at ~/.git-credentials. Please consult the comments section below or the answers from the "Linked" section, especially if your employer has zero tolerance for security issues.

Even though accepted, it doesn't answer the actual OP's question about omitting a username only (not password). For the readers with that exact problem @grawity's answer might come in handy.


Original answer (by @Alexander Zhu):

You can store your credentials using the following command

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

Also I suggest you to read
$ git help credentials

Solution 2 - Linux

You can accomplish this in the .git/config file of your local repository. This file contains a section called 'remote' with an entry called 'url'. The 'url' entry should contains the https link of repository you're talking about.

When you prefix the host 'url' with your username, git shouldn't be asking for your username anymore. Here's an example:

url = https://[email protected]

Solution 3 - Linux

Permanently authenticating with Git repositories

Run 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://[email protected]': <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 hour).


Read credentials Docs

$ git help credentials

Solution 4 - Linux

Add new SSH keys as described in this article on GitHub.

If Git still asks you for username & password, try changing https://github.com/ to [email protected]: in remote URL:

$ git config remote.origin.url 
https://github.com/dir/repo.git

$ git config remote.origin.url "[email protected]:dir/repo.git"

Solution 5 - Linux

The easiest way I found was with this command:

git config --global credential.https://github.com.username <your_username>

This works on a site by site basis and modifies your global git config.

To see the changes, use:

git config --global --edit

Solution 6 - Linux

If you are using https instead of ssh, you can edit "url" in .git/config as user701648 says, but if you want you can add also the password:

url = https://username:[email protected]

Solution 7 - Linux

git config --global credential.helper cache

Solution 8 - Linux

You can set your username for all repositories at a given site by putting something like the following in your Git configuration file. You'll want to change "https://example.com" and "me" to the actual URL and your actual username.

[credential "https://example.com"]
    username = me

(This is directly from "git help credentials")

Solution 9 - Linux

Changing the cloned repo works:

git remote set-url origin git@github.com:gitusername/projectname.git

Using cached credentials works, and setting the timeout period makes things less annoying. If you use the Windows credential helper, that should be the easiest method (especially if SSH is blocked).

Solution 10 - Linux

Make sure that you are using SSH instead of http. Check this SO answer.

Solution 11 - Linux

In addition to user701648's answer, you can store your credentials in your home folder (global for all projects), instead of project folder using the following command

$ git config --global credential.helper store
$ git push http://example.com/repo.git
Username: <type your username>
Password: <type your password>

Solution 12 - Linux

The easiest way is to create a ~/.netrc file with the following contents:

machine github.com
login YOUR_GITHUB_USERNAME
password YOUR_GITHUB_PASSWORD

(as shown here: https://gist.github.com/ahoward/2885020)

You can even close up the permissions on this file so that no one can read your password by typing:

chmod 600 ~/.netrc

Solution 13 - Linux

When I only git pull, git pull origin xxx, git asks for both username & password.

git config credential.helper store

it works.

Solution 14 - Linux

If you use SSH version, you will not have any problems with passwords.Only one time you generate SSH key, to tell git, that this pc will work with this github account and never ask me again about any access (for this pc).

How To Generate SSH for Github

Solution 15 - Linux

To avoid entering username, but still be prompted to enter a password, then you can simply clone your repository including the username:

git clone [email protected]/my_repo.git

Solution 16 - Linux

Quick method

get url of your git when inside working dir :

git config remote.origin.url

then :

git config credential.helper store

git push "url of your git"

will ask username and password last one time

done.

Solution 17 - Linux

ssh + key authentication is more reliable way than https + credential.helper

You can configure to use SSH instead of HTTPS for all the repositories as follows:

git config --global url.ssh://[email protected]/.insteadOf https://github.com/

url.<base>.insteadOf is documented here.

Solution 18 - Linux

Please note that, Beginning August 13, 2021, github will no longer accept account passwords when authenticating Git operations and will require the use of token-based authentication for all authenticated Git operations on GitHub.com. You may also continue using SSH keys where you prefer.

So for, static configuration of usernames for a given authentication context you have to use (when using github):

https://username:<personal-access-tokens>@repository-url.com

Please note that, we are using <personal-access-tokens> and not <password>

For further details on how to create a personal access token, please check the documentation.

github documentation says:

> GitHub over HTTPS... Every time you use Git to authenticate with > GitHub, you'll be prompted to enter your credentials to authenticate > with GitHub, unless you cache them with a credential helper. >
>... > > Using SSH ... Every time you use Git to authenticate with GitHub, > you'll be prompted to enter your SSH key passphrase, unless you've > stored the key.

github recommends Connecting over HTTPS. Probably because they want you to use Git Credential Manager Core. But Git Credential Manager Core runs on Windows and macOS. Linux support is (up to the point in time of posting) in an early preview.

So, I will only give Git and SSH solutions.

Git's documentation discuss how to avoid inputting the same credentials over and over using gitcredentials.

Solution 1

Use credential helpers to cache password (in memory for a short period of time).

git config --global credential.helper cache

Solution 2

Use credential helpers to store password (indefinitely on disk).

git config --global credential.helper 'store --file ~/.my-credentials'

You can find where the credential will be saved (If not set explicitly with --file) in the documentation.

NOTE: To address the concern that gitcredentials store the credentials completely unencrypted ("as is"), You can always encrypt the file and decrypt it before using.

Solution 3

This is almost a direct copy paste from Generating a new SSH key and adding it to the ssh-agent. I think following the link will be better than reading from here. Anyways, I will copy-paste the commands here.

  1. Create the ssh key using ssh-keygen -t ed25519 -C "[email protected]". You can also use RSA instead of Ed25519 using ssh-keygen -t rsa -b 4096 -C "[email protected]"

  2. Add the SSH key to your account on GitHub.

  3. If not started, start the ssh-agent in the background using $ eval "$(ssh-agent -s)"

  4. Add your SSH private key to the ssh-agent using $ ssh-add ~/.ssh/id_ed25519. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519 in the command with the name of your private key file.

Solution 19 - Linux

  1. $ git config credential.helper store

  2. $ git push/push https://github.com/xxx.git

  3. Then enter your user name and password.

  4. Done

Solution 20 - Linux

You can just run

git config --global credential.helper wincred

after installing and logging into GIT for windows in your system.

Solution 21 - Linux

The proper way to solve it is to change the http to ssh.

You can use this git remote rm origin to remove your remote repo.

And then you can add it again with the ssh sintax (which you can copy from github): git remote add origin [email protected]:username/reponame.git .

Check this article. https://www.freecodecamp.org/news/how-to-fix-git-always-asking-for-user-credentials/

Solution 22 - Linux

To set the credentials for the entire day that is 24 hours.

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

Else for 1 hour replace the 86400 secs to 3600.

OR

all configuration options for the 'cache' authentication helper:

git help credential-cache

for more information: https://docs.github.com/en/get-started/getting-started-with-git/caching-your-github-credentials-in-git

Solution 23 - Linux

I use to store my Git Repository Username and Password on Disk. So git doesn't ask for username and password every time while I push my code

follow these commands to save credentials in the local storage

$ git config credential.helper store
or
$ git config --global credential.helper store

From now on, Git will write credentials to the ~/.git-credentials file for each URL context, when accessed for the first time. To view the content of this file, you can use the cat command as shown.

$ cat  ~/.git-credentials

Solution 24 - Linux

As of August 13, 2021 password-based logins are no longer accepted. You are now required to use token/SSH-based authentication.

If you don't want to be asked every time you push, install GitHub CLI, and then to cache your credentials:

git config --global credential.helper store
gh auth login   

And follow the prompts. But make sure you check Authenticate Git with your GitHub credentials? with Y.

Solution 25 - Linux

###(Only For Mac OS)###

for Windows and Linux please refer to Github's documentation link mentioned below.

From Github Documentation: Documentation Link

Caching your GitHub password in Git

  • You need Git 1.7.10 or newer to use the osxkeychain credential helper.

>to check your Git version: git --version

  • to find out osxkeychain helper is already installed:

>git credential-osxkeychain

if you get following response then you have it already installed and ready to use on your mac: > > Usage: git credential-osxkeychain <get|store|erase>

if you don't get a response as shown above then you can install it running following command: >git credential-osxkeychain

  • to tell git to globally use osxkeychain for git credentials:

>git config --global credential.helper osxkeychain

It will prompt once before it saves credentials to the keychain.

Solution 26 - Linux

Use the following command can give you a 15 minutes to timeout.

$ git config --global credential.helper cache

You can also modify the time by the command below. This is an example for 1 hours.

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

If you would like to cache locally, use the command for an hour to timeout.

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

Solution 27 - Linux

For me this issue appeared when I enabled 2-Factor Authentication (2FA) for my account. I would enter correct credentials and authentication would still fail because of course the terminal authentication does not ask for authentication code.

I simply had to disable 2FA for my account. Upon doing that I had to enter my credentials just one time during git push. They weren't required from then on.

Solution 28 - Linux

This occurs when one downloads using HTTPS rather than the SSH,easiet way which I implemented was I pushed everything as I made a few changes once wherein it asked for the username and password, then I removed the directory from my machine and git clone SSH address. It was solved.

Just clone it using SSH rather than HTTP and it won't ask for username or password.

Also having two-factor authentication creates the problem even when you download using SSH so disabling it solves the issue.

Solution 29 - Linux

  1. Open the Windows credential manager and remove the git:https://dev.azure.com/orgname from generic credential. For steps look into https://stackoverflow.com/questions/37736232/why-tfs-with-git-is-not-working-from-command-line/53590162#53590162
  2. Delete the .git-credentials files from the folder C:\Users\{username}.
  3. Make sure you update the git latest version from https://git-scm.com/download/win

Solution 30 - Linux

I faced this issue today. If you are facing this issue in November 2020 then you need to update your git version. I received an email telling me about this. Here is what exactly was the email by github team.

> We have detected that you recently attempted to authenticate to GitHub > using an older version of Git for Windows. GitHub has changed how > users authenticate when using Git for Windows, and now requires the > use of a web browser to authenticate to GitHub. To be able to login > via web browser, users need to update to the latest version of Git for > Windows. You can download the latest version at: > > * https://gitforwindows.org/ > > If you cannot update Git for Windows to the latest version please see > the following link for more information and suggested workarounds: > > * https://aka.ms/gcmcore-githubauthchanges > > If you have any questions about these changes or require any > assistance, please reach out to GitHub Support and we’ll be happy to > assist further. > > * https://support.github.com/contact > > Thanks, The GitHub Team

Solution 31 - Linux

I tried these steps and it worked :

  1. Create a personalized access token : https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token

  2. Add the token to Android studio (File->Settings->Version Control->Github->Add - Login with token)

  3. Goto File->Settings->Version Control->Git and uncheck 'Use Credential Helper'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRanRagView Question on Stackoverflow
Solution 1 - LinuxAlexander ZhugastrovView Answer on Stackoverflow
Solution 2 - Linuxuser701648View Answer on Stackoverflow
Solution 3 - LinuxJaykumar PatelView Answer on Stackoverflow
Solution 4 - LinuxLukasz CzerwinskiView Answer on Stackoverflow
Solution 5 - LinuxBenView Answer on Stackoverflow
Solution 6 - LinuxDaniel LerchView Answer on Stackoverflow
Solution 7 - LinuxKai WangView Answer on Stackoverflow
Solution 8 - LinuxAlan De SmetView Answer on Stackoverflow
Solution 9 - LinuxLayer8ErrView Answer on Stackoverflow
Solution 10 - LinuxgkrisView Answer on Stackoverflow
Solution 11 - LinuxYuviView Answer on Stackoverflow
Solution 12 - LinuxSimplerView Answer on Stackoverflow
Solution 13 - LinuxLanguoguangView Answer on Stackoverflow
Solution 14 - LinuxHazarapet TunanyanView Answer on Stackoverflow
Solution 15 - LinuxManoDestraView Answer on Stackoverflow
Solution 16 - LinuxAominéView Answer on Stackoverflow
Solution 17 - LinuxAkifView Answer on Stackoverflow
Solution 18 - LinuxAhmad IsmailView Answer on Stackoverflow
Solution 19 - LinuxStephen Tun AungView Answer on Stackoverflow
Solution 20 - LinuxcyperpunkView Answer on Stackoverflow
Solution 21 - LinuxMatheus SilvaView Answer on Stackoverflow
Solution 22 - Linuxchampion-runnerView Answer on Stackoverflow
Solution 23 - LinuxGunjan PaulView Answer on Stackoverflow
Solution 24 - LinuxahmelqView Answer on Stackoverflow
Solution 25 - LinuxAkash GandhiView Answer on Stackoverflow
Solution 26 - LinuxchenghuayangView Answer on Stackoverflow
Solution 27 - LinuxBikramjeet SinghView Answer on Stackoverflow
Solution 28 - LinuxRiya_DharView Answer on Stackoverflow
Solution 29 - LinuxKarunakaranView Answer on Stackoverflow
Solution 30 - LinuxSanan AliView Answer on Stackoverflow
Solution 31 - LinuxReejeshView Answer on Stackoverflow