如何在 git [GitExtension] 中保存用户名和密码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35942754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save username and password in git [GitExtension]?
提问by Edson Cezar
I want to use a push and pull automatically in GitExtension, without entering my user and password in a prompt, every time.
我想在GitExtension 中自动使用推和拉,而不是每次都在提示中输入我的用户名和密码。
So how can I save my credentials in git?
那么如何在 git 中保存我的凭据呢?
回答by Neetika
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然后把违规的user+password从~/.git-credentials
文件中去掉,所以现在重新运行
git pull
to provide a new password so it works as earlier.
提供一个新密码,使其像以前一样工作。
回答by Farhad Faghihi
You can use the git config
to enable credentials storage in git.
您可以使用git config
来启用 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.
存储格式为.git-credentials
文件,以明文形式存储。
Also, you can use other helpers for the git config credential.helper
, namely memory cache:
此外,您可以使用其他帮助程序git config credential.helper
,即内存缓存:
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).
这需要一个可选的timeout parameter
,确定凭据将在内存中保留多长时间。使用帮助器,凭据将永远不会触及磁盘,并会在指定的超时后被擦除。该default
值是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
警告:如果您使用此方法,您的 git 帐户密码将以plaintext
格式保存global .gitconfig file
,例如在 linux 中/home/[username]/.gitconfig
If this is undesirable to you, use an ssh key
for your accounts instead.
如果您不希望这样做,请ssh key
为您的帐户使用。
回答by Andreas Bigger
Recommended and Secure Method: SSH
推荐和安全的方法: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.
创建一个 ssh Github 密钥。转到github.com-> 设置 -> SSH 和 GPG 密钥 -> 新的 SSH 密钥。现在将您的私钥保存到您的计算机。
Then, if the private key is saved as id_rsain the ~/.ssh/directory, we add it for authentication as such:
然后,如果私钥在~/.ssh/目录中保存为id_rsa,我们将其添加为身份验证:
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-credential-store 来缓存我们的用户名和密码一段时间。只需在您的 CLI(终端或命令提示符)中输入以下内容:
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-credential-store,但将密码保存在磁盘上的纯文本文件中,如下所示:
git config credential.helper store
Outdated Answer - Quick and Insecure
过时的答案 - 快速且不安全
This is an insecuremethod 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"
回答by simhumileco
Turn on the credential helper so that Git will save your password in memory for some time:
打开凭证助手,以便 Git 将您的密码保存在内存中一段时间:
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.
默认情况下,Git 会将您的密码缓存 15 分钟。
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
回答by T04435
You can edit the ~/.gitconfig
file to storeyour credentials
您可以编辑该~/.gitconfig
文件以存储您的凭据
sudo nano ~/.gitconfig
Which should already have
哪个应该已经有
[user]
email = [email protected]
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
然后当你拉| 克隆| 输入你的git密码,一般情况下,密码会以~/.git-credentials
格式保存
https://GITUSER:[email protected]
WHERE DOMAIN.XXX COULD BE GITHUB.COM | BITBUCKET.ORG | OTHER
DOMAIN.XXX 可能是 GITHUB.COM | BITBUCKET.ORG | 其他
See Docs
查看文档
Restart your terminal.
重启你的终端。
回答by Nadu
Just put your credentials in the Url like this:
只需将您的凭据放在 Url 中,如下所示:
https://Username
:Password
@github.com/myRepoDir/myRepo.git
https://Username
:Password
@github.com/myRepoDir/myRepo.git
You may storeit like this:
你可以这样存储:
git remote add myrepo https://Userna...
git remote add myrepo https://Userna...
...example to useit:
...使用它的示例:
git push myrepo master
git push myrepo master
Now that is to Listthe url aliases:
现在是列出url 别名:
git remote -v
git remote -v
...and that the command to delete one of them:
...以及删除其中之一的命令:
git remote rm myrepo
git remote rm myrepo
回答by Tuananhcwrs
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.
这样,您机器上的任何本地 git 存储库都将使用该信息。
You can individually config for each repo by doing:
您可以通过执行以下操作为每个 repo 单独配置:
- open terminal at the repo folder.
run the following:
git config user.name "your username" git config user.password "your password"
- 在 repo 文件夹中打开终端。
运行以下命令:
git config user.name "your username" git config user.password "your password"
It affects only that folder (because your configuration is local).
它仅影响该文件夹(因为您的配置是本地的)。
回答by David Navarro Astudillo
You can use git-credential-store to store your passwords unencrypted on the disk, protected only by the permissions of the file system.
您可以使用 git-credential-store 将未加密的密码存储在磁盘上,仅受文件系统权限的保护。
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
您可以检查存储在文件中的凭据 ~/.git-credentials
For more info visit git-credential-store - Helper to store credentials on disk
有关更多信息,请访问 git-credential-store - 在磁盘上存储凭据的助手
回答by Birol Efe
You will be more secure if you use SSH authentication than username/password authentication.
如果使用 SSH 身份验证比用户名/密码身份验证更安全。
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:
如果您使用的是 Mac,SSH 客户端身份验证已集成到 MacOS 钥匙串中。创建 SSH 密钥后,在终端中输入:
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.
这会将 SSH 私钥添加到 MacOS 钥匙串。git 客户端在连接到远程服务器时将使用 ssh。只要您在服务器上注册了 ssh 公钥,就可以了。
回答by CoolAJ86
After going over dozensof SO posts, blogs, etc, I tried out everymethod, and this is what I came up with. It covers EVERYTHING.
在浏览了数十篇SO 帖子、博客等之后,我尝试了每种方法,这就是我想出的。它涵盖了一切。
The Vanilla DevOps Git Credentials & Private Packages Cheatsheet
Vanilla DevOps Git 凭证和私有包速查表
These are all the ways and tools by which you can securely authenticate git to clone a repository without an interactive password prompt.
这些都是您可以安全地对 git 进行身份验证以在没有交互式密码提示的情况下克隆存储库的方法和工具。
- 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
- SSH 公钥
- SSH_ASKPASS
- API 访问令牌
- GIT_ASKPASS
- .gitconfig 代替
- .gitconfig [凭证]
- .git-凭证
- .netrc
- 私人包(免费)
- 节点/ npm package.json
- python/pip/eggs requirements.txt
- 红宝石 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):
获取您的访问令牌(如果您需要 Github 或 Gitea 说明,请参阅备忘单中的部分)并将其设置在环境变量中(用于本地开发和部署):
MY_GIT_TOKEN=xxxxxxxxxxxxxxxx
For Github, copy and run these lines verbatim:
对于 Github,复制并逐字运行这些行:
git config --global url."https://api:[email protected]/".insteadOf "https://github.com/"
git config --global url."https://ssh:[email protected]/".insteadOf "ssh://[email protected]/"
git config --global url."https://git:[email protected]/".insteadOf "[email protected]:"
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.
恭喜,现在任何克隆 git 存储库的自动化工具都不会受到密码提示的阻碍,无论是使用 https 还是 ssh url 样式。
Not using Github?
不使用 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).
对于其他平台(Gitea、Github、Bitbucket),只需更改 URL。不要更改用户名(尽管是任意的,但不同的配置条目需要它们)。
Compatibility
兼容性
This works locally in MacOS, Linux, Windows (in Bash), Docker, CircleCI, Heroku, Akkeris, etc.
这在 MacOS、Linux、Windows(在 Bash 中)、Docker、CircleCI、Heroku、Akkeris 等本地工作。
More Info
更多信息
See the ".gitconfig insteadOf" section of the cheatsheet.
请参阅备忘单的“.gitconfig insteadOf”部分。
Security
安全
See the "Security" section of the cheatsheet.
请参阅备忘单的“安全”部分。