如何避免在每次 git push 时指定用户名和密码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8588768/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:34:26  来源:igfitidea点击:

How do I avoid the specification of the username and password at every git push?

gitgithubgit-push

提问by Leem.fin

I git pushmy work to a remote Git repository.

我将git push我的工作转移到远程 Git 存储库。

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

每个push都会提示我输入usernamepassword。我想每次推送都避免它,但是如何配置以避免它?

回答by First Zero

1. Generate an SSH key

1.生成SSH密钥

Linux/Mac

Linux/Mac

Open terminal to create ssh keys:

打开终端以创建 ssh 密钥:

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

For Windows

对于 Windows

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

(仅当提交程序能够使用证书/私有和公共 ssh 密钥时才有效)

  1. Use Putty Gen to generate a key
  2. Export the key as an open SSH key
  1. 使用 Putty Gen 生成密钥
  2. 将密钥导出为开放的 SSH 密钥

Here is a walkthroughon putty gen for the above steps

以下是上述步骤的腻子生成演练

2. Associate the SSH key with the remote repository

2. 将 SSH 密钥与远程仓库关联

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

此步骤会有所不同,具体取决于您的遥控器的设置方式。

  • If it is a GitHub repository and you have administrative privileges, go to settingsand click 'add SSH key'. Copy the contents of your ~/.ssh/id_rsa.pubinto 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

  • 如果它是 GitHub 存储库并且您具有管理权限,请转到设置并单击“添加 SSH 密钥”。将您的内容复制~/.ssh/id_rsa.pub到标有“密钥”的字段中。

  • 如果您的存储库由其他人管理,请将您的id_rsa.pub.

  • 如果您的远程存储库由您管理,则可以使用此命令,例如:

    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

3. 将您的远程 URL 设置为支持 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

如果您已完成上述步骤但仍收到密码提示,请确保您的 repo URL 为以下形式

git+ssh://[email protected]/username/reponame.git

as opposed to

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

To see your repo URL, run:

要查看您的存储库 URL,请运行:

git remote show origin

You can change the URL with:

您可以通过以下方式更改 URL:

git remote set-url origin git+ssh://[email protected]/username/reponame.git

[1] This section incorporates the answer from Eric P

[1] 本节包含Eric P的回答

回答by Jay Patel

Permanently authenticating with Git repositories,

使用 Git 存储库进行永久身份验证,

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,

使用还应指定缓存 expire

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

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

启用凭据缓存后,将缓存7200 秒(2 小时)

Note:Credential helper storing unencrypted password on local disk.

注意:凭证助手在本地磁盘上存储未加密的密码。

回答by Eric P

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

如果您已经设置了 SSH 密钥并且仍然收到密码提示,请确保您的 repo URL 为以下格式

git+ssh://[email protected]/username/reponame.git

as opposed to

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

To see your repo URL, run:

要查看您的存储库 URL,请运行:

git remote show origin

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

您可以git remote set-url像这样更改 URL :

git remote set-url origin git+ssh://[email protected]/username/reponame.git

回答by Pavel

Just use --repooption for git push command. Like this:

只需使用--repogit push 命令的选项。像这样:

$ git push --repo https://name:[email protected]/name/repo.git

回答by serv-inc

Saving Indefinitely

无限期保存

You can use the git-credential-storevia

您可以使用混帐凭据店通过

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.

使用此助手会将您的密码未加密地存储在磁盘上,仅受文件系统权限的保护。如果这不是一个可接受的安全权衡,请尝试 git-credential-cache,或找到与您的操作系统提供的安全存储集成的帮助程序。

With a Timeout

有超时

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

使用git-credential-cache默认情况下将密码存储 15 分钟。

git config credential.helper cache

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

要设置不同的超时,请使用--timeout(此处为 5 分钟)

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

Secure Saving Indefinitely(OS X and Windows)

无限期安全保存(OS X 和 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]
  • 如果您使用的是 Mac,Git 带有“osxkeychain”模式,它将凭据缓存在附加到您的系统帐户的安全钥匙串中。此方法将凭据存储在磁盘上,并且它们永不过期,但它们使用存储 HTTPS 证书和 Safari 自动填充的相同系统进行加密。在命令行上运行以下命令将启用此功能:git config --global credential.helper osxkeychain. 您还需要使用 Keychain 应用程序将凭据存储在 Keychain 中。
  • 如果您使用的是 Windows,则可以安装一个名为“Git Credential Manager for Windows”的帮助程序。这类似于上面描述的“osxkeychain”帮助程序,但使用 Windows 凭据存储来控制敏感信息。它可以在https://github.com/Microsoft/Git-Credential-Manager-for-Windows上找到。[强调我的]

回答by ishan

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

我使用的是 https 链接 ( https://github.com/org/repo.git) 而不是 ssh 链接;

[email protected]:org/repo.git  

Switching solved the problem for me!

切换为我解决了问题!

回答by Anil Agrawal

Step 1 -

第1步 -

Create SSH keys on your linux system using below command

使用以下命令在您的 linux 系统上创建 SSH 密钥

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)

它将询问密码和文件名(默认为 ~/.ssh/id_rsa, ~/.ssh/id_rsa.pub)

Step 2 -

第2步 -

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

创建文件后,将公钥 id_rsa.pub 添加到 github 帐户 ssh 部分。

Step 3 -

第 3 步 -

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

在您的机器上,使用以下命令将私钥 id_rsa 添加到 ssh-agent

ssh-add ~/.ssh/id_rsa 

Step 4 -

第四步 -

Now add remote url [email protected]:user_name/repo_name.git to your local git repo using below command.

现在使用以下命令将远程 url [email protected]:user_name/repo_name.git 添加到您的本地 git repo。

git remote remove origin
git remote add origin [email protected]:user_name/repo_name.git

Thats it.

就是这样。

回答by Ayejuni Ilemobayo Kings

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

在 Windows 操作系统上改用它,这对我有用:

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

回答by TrollAxeThrower

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.

在此之后,您可以使用任何需要密码的命令。你不必推动。(例如,您也可以拉取)之后,您将无需再次输入您的用户名/密码。

回答by Daniel

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

将您的 git 客户端连接到您的操作系统凭证存储。例如,在 Windows 中,您将凭证助手绑定到 wincred:

git config --global credential.helper wincred

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

在 GitHub 上使用 https:// 时,有没有办法跳过密码输入?