问题
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?
回答1:
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.
回答2:
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.
回答3:
You can set your username and password like this:
git config --global user.name "your username"
git config --global user.password "your password"
回答4:
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
回答5:
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.
回答6:
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
回答7:
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).
回答8:
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
回答9:
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.
回答10:
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.
回答11:
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.
回答12:
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:
- Open config by running:
vi ~/.ssh/config
- Added the following:
UseKeychain yes
- 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
回答13:
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.
回答14:
From the comment by rifrol, on Linux Ubuntu, from this answer:
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.
回答15:
Store username and password in .git-credentials
.git-credentials
is where your username and password 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:
https://${username}:${password_or_access_token}@github.com
So, in order to save the username and password:
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.
回答16:
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
来源:https://stackoverflow.com/questions/35942754/how-to-save-username-and-password-in-git