git 有没有办法缓存 GitHub 凭据以推送提交?

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

Is there a way to cache GitHub credentials for pushing commits?

gitauthenticationgithubgit-pushgit-config

提问by Zepplock

I recently switched to synchronizing my repositories to https:// on GitHub (due to firewall issues), and it asks for a password every time.

我最近切换到将我的存储库同步到 GitHub 上的 https://(由于防火墙问题),并且每次都要求输入密码。

Is there a way to cache the credentials, instead of authenticating every time that git push?

有没有办法缓存凭据,而不是每次都进行身份验证git push

回答by Mark Longair

With Git version 1.7.9 and later

使用 Git 版本 1.7.9 及更高版本

Since Git 1.7.9 (released in late January 2012), there is a neat mechanism in Git to avoid having to type your password all the time for HTTP / HTTPS, called credential helpers. (Thanks to dazonicfor pointing out this new feature in the comments below.)

自 Git 1.7.9(于 2012 年 1 月下旬发布)以来,Git 中有一个巧妙的机制可以避免在 HTTP / HTTPS 中一直输入密码,称为凭据助手。(感谢dazonic在下面的评论中指出了这个新功能。)

With Git 1.7.9 or later, you can just use one of the following credential helpers:

对于 Git 1.7.9 或更高版本,您可以只使用以下凭证助手之一:

git config --global credential.helper cache

... which tells Git to keep your password cached in memory for (by default) 15 minutes. You can set a longer timeout with:

...它告诉 Git 将您的密码缓存在内存中(默认情况下)15 分钟。您可以设置更长的超时时间:

git config --global credential.helper "cache --timeout=3600"

(That example was suggested in the GitHub help page for Linux.) You can also store your credentials permanently if so desired, see the other answers below.

(该示例在LinuxGitHub 帮助页面中建议。)如果需要,您还可以永久存储您的凭据,请参阅下面的其他答案。

GitHub's help also suggeststhat if you're on Mac OS X and used Homebrewto install Git, you can use the native Mac OS X keystore with:

GitHub 的帮助还建议,如果您使用的是 Mac OS X 并使用Homebrew安装 Git,您可以使用原生 Mac OS X 密钥库:

git config --global credential.helper osxkeychain

For Windows, there is a helper called Git Credential Manager for Windowsor wincred in msysgit.

对于 Windows,在 msysgit 中有一个名为Git Credential Manager for Windowswincred 的帮助程序

git config --global credential.helper wincred # obsolete

With Git for Windows 2.7.3+(March 2016):

使用适用于 Windows 2.7.3+ 的 Git(2016 年 3 月):

git config --global credential.helper manager

For Linux, you can use gnome-keyring(or other keyring implementation such as KWallet).

对于 Linux,您可以使用gnome-keyring(或其他密钥环实现,例如 KWallet)。

With Git versions before 1.7.9

使用 1.7.9 之前的 Git 版本

With versions of Git before 1.7.9, this more secure option is not available, and you'll need to change the URL that your originremote uses to include the password in this fashion:

对于 1.7.9 之前的 Git 版本,此更安全的选项不可用,您需要更改origin远程使用的 URL,以这种方式包含密码:

https://you:[email protected]/you/example.git

... in other words with :passwordafter the username and before the @.

... 换句话说,:password在用户名之后和@.

You can set a new URL for your originremote with:

您可以使用以下命令为origin遥控器设置新 URL :

git config remote.origin.url https://you:[email protected]/you/example.git

Make sure that you use https, and you should be aware that if you do this, your GitHub password will be stored in plaintext in your .gitdirectory, which is obviously undesirable.

确保您使用https,并且您应该注意,如果您这样做,您的GitHub密码将以明文形式存储在您的.git目录中,这显然是不可取的。

With any Git version (well, since version 0.99)

使用任何 Git 版本(嗯,从 0.99 版开始)

An alternative approach is to put your username and password in your ~/.netrcfile, although, as with keeping the password in the remote URL, this means that your password will be stored on the disk in plain text and is thus less secure and not recommended. However, if you want to take this approach, add the following line to your ~/.netrc:

另一种方法是将您的用户名和密码放在您的~/.netrc文件中,尽管与将密码保存在远程 URL 中一样,这意味着您的密码将以纯文本形式存储在磁盘上,因此安全性较低,不推荐使用。但是,如果您想采用这种方法,请将以下行添加到您的~/.netrc

machine <hostname> login <username> password <password>

... replacing <hostname>with the server's hostname, and <username>and <password>with your username and password. Also remember to set restrictive file system permissions on that file:

...更换<hostname>与服务器的主机名,<username><password>与您的用户名和密码。还要记住在该文件上设置限制性文件系统权限:

chmod 600 ~/.netrc

Note that on Windows, this file should be called _netrc, and you may need to define the %HOME% environment variable - for more details see:

请注意,在 Windows 上,应调用此文件_netrc,您可能需要定义 %HOME% 环境变量 - 有关更多详细信息,请参阅:

回答by Giri Alwar

You can also have Git store your credentials permanently using the following:

您还可以使用以下命令让 Git 永久存储您的凭据:

git config credential.helper store

Note: While this is convenient, Git will store your credentials in clear text in a local file (.git-credentials) under your project directory (see below for the "home" directory). If you don't like this, delete this file and switch to using the cache option.

注意:虽然这很方便,但 Git 会将您的凭据以明文形式存储在项目目录下的本地文件 (.git-credentials) 中(“home”目录见下文)。如果您不喜欢这样,请删除此文件并切换到使用缓存选项。

If you want Git to resume to asking you for credentials every time it needs to connect to the remote repository, you can run this command:

如果您希望 Git 在每次需要连接到远程存储库时继续询问您的凭据,您可以运行以下命令:

git config --unset credential.helper

To store the passwords in .git-credentialsin your %HOME%directory as opposed to the project directory: use the --globalflag

要将密码存储.git-credentials在您的%HOME%目录而不是项目目录中:使用--global标志

git config --global credential.helper store

回答by VonC

TLDR; Use an encrypted netrc file with Git 1.8.3+.

TLDR;在 Git 1.8.3+ 中使用加密的 netrc 文件

Saving a password for a Git repository HTTPS URL is possible with a ~/.netrc(Unix) or %HOME%/_netrc(note the _) on Windows.

在 Windows 上可以使用~/.netrc(Unix) 或%HOME%/_netrc(注意_)为 Git 存储库 HTTPS URL 保存密码。

But: That file would store your password in plain text.

但是:该文件将以纯文本形式存储您的密码。

Solution: Encrypt that file with GPG (GNU Privacy Guard), and make Git decrypt it each time it needs a password (for push/pull/fetch/cloneoperation).

解决方案:加密与文件GPG(GNU隐私卫士),使Git的解密每次需要输入密码(时间push/ pull/ fetch/clone操作)。



Note: with Git 2.18 (Q2 2018), you now can customize the GPG used to decrypt the encrypted .netrcfile.

注意:使用 Git 2.18(2018 年第二季度),您现在可以自定义用于解密加密.netrc文件的 GPG 。

See commit 786ef50, commit f07eeed(12 May 2018) by Luis Marsano (``).
(Merged by Junio C Hamano -- gitster--in commit 017b7c5, 30 May 2018)

请参阅Luis Marsano (``) 的commit 786ef50commit f07eeed(2018 年 5 月 12 日
(由Junio C gitsterHamano合并-- --commit 017b7c5,2018 年 5 月 30 日)

git-credential-netrc: accept gpgoption

git-credential-netrcwas hardcoded to decrypt with 'gpg' regardless of the gpg.program option.
This is a problem on distributions like Debian that call modern GnuPG something else, like 'gpg2'

git-credential-netrc: 接受gpg选项

git-credential-netrcgpg无论 gpg.program 选项如何,都被硬编码为使用“ ”解密。
这是 Debian 等发行版的问题,这些发行版将现代 GnuPG 称为其他东西,例如 ' gpg2'



Step-by-Step instructions for Windows

适用于 Windows 的分步说明

With Windows:

使用 Windows:

(Git has a gpg.exein its distribution, but using a full GPG installation includes a gpg-agent.exe, which will memorize your passphrase associated to your GPG key.)

(Gitgpg.exe在其发行版中有 ,但使用完整的 GPG 安装包括gpg-agent.exe,它将记住与 GPG 密钥关联的密码。)

  • Install gpg4Win Lite, the minimum gnupg command-line interface (take the most recent gpg4win-vanilla-2.X.Y-betaZZ.exe), and complete your PATH with the GPG installation directory:

    set PATH=%PATH%:C:\path\to\gpg
    copy C:\path\to\gpg\gpg2.exe C:\path\to\gpg\gpg.exe
    
  • 安装gpg4Win Lite,最小的 gnupg 命令行界面(采用最新的gpg4win-vanilla-2.X.Y-betaZZ.exe),并使用 GPG 安装目录完成您的 PATH:

    set PATH=%PATH%:C:\path\to\gpg
    copy C:\path\to\gpg\gpg2.exe C:\path\to\gpg\gpg.exe
    

(Note the 'copy' command: Git will need a Bash script to execute the command 'gpg'. Since gpg4win-vanilla-2comes with gpg2.exe, you need to duplicate it.)

(注意“ copy”命令:Git 将需要一个 Bash 脚本来执行命令“ gpg”。由于gpg4win-vanilla-2带有gpg2.exe,你需要复制它。)

  • Create or import a GPG key, and trust it:

    gpgp --import aKey
    # or
    gpg --gen-key
    
  • 创建或导入 GPG 密钥,并信任它:

    gpgp --import aKey
    # or
    gpg --gen-key
    

(Make sure to put a passphrase to that key.)

(确保为该密钥添加密码。)

  • Trust that key

  • Install the credential helper script in a directory within your %PATH%:

    cd c:\a\fodler\in\your\path
    curl -o c:\prgs\bin\git-credential-netrc https://raw.githubusercontent.com/git/git/master/contrib/credential/netrc/git-credential-netrc.perl
    
  • 相信那把钥匙

  • 在您的目录中安装凭据帮助程序脚本%PATH%

    cd c:\a\fodler\in\your\path
    curl -o c:\prgs\bin\git-credential-netrc https://raw.githubusercontent.com/git/git/master/contrib/credential/netrc/git-credential-netrc.perl
    

(Beware: the script is renamed in Git 2.25.x/2.26, see below)

(注意:脚本在 Git 2.25.x/2.26 中重命名,见下文)

(Yes, this is a Bash script, but it will work on Windows since it will be called by Git.)

(是的,这是一个 Bash 脚本,但它可以在 Windows 上运行,因为它将被 Git 调用。)

  • Make a _netrc file in clear text

    machine a_server.corp.com
    login a_login
    password a_password
    protocol https
    
    machine a_server2.corp.com
    login a_login2
    password a_password2
    protocol https
    
  • 以明文形式制作 _netrc 文件

    machine a_server.corp.com
    login a_login
    password a_password
    protocol https
    
    machine a_server2.corp.com
    login a_login2
    password a_password2
    protocol https
    

(Don't forget the 'protocol' part: 'http' or 'https' depending on the URL you will use.)

(不要忘记 ' protocol' 部分:' http' 或 ' https',具体取决于您将使用的 URL。)

  • Encrypt that file:

    gpg -e -r a_recipient _netrc
    
  • 加密该文件:

    gpg -e -r a_recipient _netrc
    

(You now can deletethe _netrcfile, keeping only the _netrc.gpgencrypted one.)

(您现在可以删除_netrc文件,只保留_netrc.gpg加密的文件。)

  • Use that encrypted file:

    git config --local credential.helper "netrc -f C:/path/to/_netrc.gpg -v"
    
  • 使用该加密文件:

    git config --local credential.helper "netrc -f C:/path/to/_netrc.gpg -v"
    

(Note the '/': C:\path\to...wouldn't work at all.) (You can use at first -v -dto see what is going on.)

(请注意“ /”:C:\path\to...根本不起作用。)(您可以首先使用-v -d以查看发生了什么。)

From now on, any Git command using an HTTP(S) URL which requires authentication will decrypt that _netrc.gpgfile and use the login/password associated to the server you are contacting. The first time, GPG will ask you for the passphrase of your GPG key, to decrypt the file. The other times, the gpg-agent launched automaticallyby the first GPG call will provide that passphrase for you.

从现在开始,任何使用需要身份验证的 HTTP(S) URL 的 Git 命令都将解密该_netrc.gpg文件并使用与您正在联系的服务器关联的登录名/密码。第一次,GPG 会询问您 GPG 密钥的密码,以解密文件。其他时候,第一个 GPG 调用自动启动的 gpg-agent将为您提供该密码。

That way, you can memorize severalURLs/logins/passwords in one file, and have it stored on your disk encrypted.
I find it more convenient than a "cache" helper", where you need to remember and type (once per session) a different password for each of your remote services, for said password to be cached in memory.

这样,您可以在一个文件中记住多个URL/登录名/密码,并将其加密存储在您的磁盘上。
我发现它比“缓存”助手更方便,您需要记住并为每个远程服务输入(每个会话一次)不同的密码,以便将所述密码缓存在内存中。



With Git 2.26 (Q1 2020), the sample credential helper for using .netrchas been updated to work out of the box. See patch/discussion.

在 Git 2.26(2020 年第一季度)中,使用的示例凭证助手.netrc已更新为开箱即用。请参阅补丁/讨论

See commit 6579d93, commit 1c78c78(20 Dec 2019) by Denton Liu (Denton-L).
(Merged by Junio C Hamano -- gitster--in commit 1fd27f8, 25 Dec 2019)

请参阅提交 6579d93提交 1c78c78(2019 年 12 月 20 日)由Denton Liu ( Denton-L)
(由Junio C gitsterHamano合并-- --1fd27f8 提交中,2019 年 12 月 25 日)

contrib/credential/netrc: make PERL_PATHconfigurable

Signed-off-by: Denton Liu

The shebang path for the Perl interpreter in git-credential-netrcwas hardcoded.
However, some users may have it located at a different location and thus, would have had to manually edit the script.

Add a .perlprefix to the scriptto denote it as a template and ignore the generated version.
Augment the Makefileso that it generates git-credential-netrcfrom git-credential-netrc.perl, just like other Perl scripts.

The Makefile recipes were shamelessly stolen from contrib/mw-to-git/Makefile.

contrib/credential/netrc: 使可PERL_PATH配置

签字人:Denton Liu

Perl 解释器中的shebang 路径git-credential-netrc是硬编码的。
但是,某些用户可能会将其放置在不同的位置,因此必须手动编辑脚本。

.perl向脚本添加前缀以将其表示为模板并忽略生成的版本。
增强Makefile以便它git-credential-netrc从生成git-credential-netrc.perl,就像其他 Perl 脚本一样。

Makefile 食谱被无耻地从contrib/mw-to-git/Makefile.

And:

和:

With 2.26 (Q1 2020), Sample credential helper for using .netrc has been updated to work out of the box.

在 2.26(2020 年第一季度)中,使用 .netrc 的示例凭证助手已更新为开箱即用。

See commit 6579d93, commit 1c78c78(20 Dec 2019) by Denton Liu (Denton-L).
(Merged by Junio C Hamano -- gitster--in commit 1fd27f8, 25 Dec 2019)

请参阅提交 6579d93提交 1c78c78(2019 年 12 月 20 日)由Denton Liu ( Denton-L)
(由Junio C gitsterHamano合并-- --1fd27f8 提交中,2019 年 12 月 25 日)

contrib/credential/netrc: work outside a repo

Signed-off-by: Denton Liu

Currently, git-credential-netrcdoes not work outside of a git repository. It fails with the following error:

fatal: Not a git repository: . at /usr/share/perl5/Git.pm line 214.

There is no real reason why need to be within a repository, though. Credential helpers should be able to work just fine outside the repository as well.

Call the non-self version of config()so that git-credential-netrcno longer needs to be run within a repository.

contrib/credential/netrc: 在 repo 之外工作

签字人:Denton Liu

目前,git-credential-netrc在 git 存储库之外不起作用。它失败并出现以下错误:

fatal: Not a git repository: . at /usr/share/perl5/Git.pm line 214.

但是,没有真正的理由需要在存储库中。凭证助手也应该能够在存储库外正常工作。

调用非自身版本,config()以便git-credential-netrc不再需要在存储库中运行。

Jeff King (peff)adds:

杰夫·金 ( peff)补充说:

I assume you're using a gpg-encrypted netrc(if not, you should probably just use credential-store).
For "read-only" password access, I find the combination of passwith config like this is a bit nicer:

[credential "https://github.com"]
  username = peff
  helper = "!f() { test  = get && echo password=`pass github/oauth`; }; f"

我假设您使用的是 gpg 加密的netrc(如果没有,您可能应该只使用credential-store)。
对于“只读”密码访问,我发现pass与 config 这样的组合更好一些:

[credential "https://github.com"]
  username = peff
  helper = "!f() { test  = get && echo password=`pass github/oauth`; }; f"

回答by wortwart

There's an easy, old-fashioned way to store user credentials in an HTTPS URL:

有一种简单的老式方法可以将用户凭据存储在 HTTPS URL 中:

https://user:[email protected]/...

You can change the URL with git remote set-url <remote-repo> <URL>

您可以更改网址 git remote set-url <remote-repo> <URL>

The obvious downside to that approach is that you have to store the password in plain text. You can still just enter the user name (https://[email protected]/...) which will at least save you half the hassle.

这种方法的明显缺点是您必须以纯文本形式存储密码。您仍然可以只输入用户名 ( https://[email protected]/...),这至少可以为您省去一半的麻烦。

You might prefer to switch to SSH or to use the GitHub client software.

您可能更喜欢切换到 SSH 或使用 GitHub 客户端软件。

回答by roo2

Use a credential store.

使用凭证存储。

For Git 2.11+ on OS Xand Linux, use Git's built in credential store:

对于OS XLinux上的 Git 2.11+ ,使用 Git 的内置凭证存储

git config --global credential.helper libsecret

For msysgit 1.7.9+ on Windows:

对于Windows上的 msysgit 1.7.9+ :

git config --global credential.helper wincred


For Git 1.7.9+ on OS X use:

对于 OS X 上的 Git 1.7.9+ 使用:

git config --global credential.helper osxkeychain

回答by sgoran

You can just use

你可以使用

git config credential.helper store

When you enter password next time with pull or push, it will be stored in file .git-credentials as plain text (a bit unsecure, but just put it into a protected folder).

当您下次使用 pull 或 push 输入密码时,它将以纯文本形式存储在 .git-credentials 文件中(有点不安全,但只需将其放入受保护的文件夹即可)。

And that's it, as stated on this page:

就是这样,如本页所述:

git-credential-store

git-credential-store

回答by Tarun Gupta

Simply include the login credentials as part of the URL:

只需将登录凭据包含在 URL 中:

git remote rm origin
git remote add origin https://username:[email protected]/path/to/repo.git

Note: I do not recommend this method, but if you are in rush and nothing else works, you can use this method.

注意:我不推荐这种方法,但如果你很着急并且没有其他方法,你可以使用这种方法。

回答by Ben

It wasn't immediately obvious to me that I needed to download the helper first! I found the credential.helper download at Atlassian's Permanently authenticating with Git repositories.

对我来说,我需要先下载帮助程序并不是很明显!我在Atlassian's Permanently authentication with Git repositories找到了 credential.helper 下载。

Quote:

引用:

Follow these steps if you want to use Git with credential caching on OS X:

如果您想在 OS X 上使用带有凭证缓存的 Git,请按照以下步骤操作:

Download the binary git-credential-osxkeychain.

下载二进制文件 git-credential-osxkeychain。

Run the command below to ensure the binary is executable:

运行以下命令以确保二进制文件是可执行的:

chmod a+x git-credential-osxkeychain

Put it in the directory /usr/local/bin.

把它放在目录中/usr/local/bin

Run the command below:

运行以下命令:

git config --global credential.helper osxkeychain

回答by helloPiers

On a GNU/Linux setup, a ~/.netrc works quite well too:

在 GNU/Linux 设置中,~/.netrc 也能很好地工作:

$ cat ~/.netrc
machine github.com login lot105 password howsyafather

It might depend on which network libraries Git is using for HTTPStransport.

这可能取决于 Git 用于HTTPS传输的网络库。

回答by Nadeem Khedr

For Windows you can use the Git Credential Manager(GCM) plugin. It is currently maintained by Microsoft. The nice thing is that it saves the password in the Windows Credential Store, not as plain text.

对于 Windows,您可以使用Git Credential Manager(GCM) 插件。目前由微软维护。好消息是它将密码保存在 Windows Credential Store 中,而不是纯文本。

There is an installer on the releases pageof the project. This will also install the official version of Git for Windowswith the credential manager built-in. It allows two-factor authenticationfor GitHub (and other servers). And has a graphical interface for initially logging in.

项目的发布页面上有一个安装程序。这还将安装带有内置凭据管理器的WindowsGit正式版。它允许对 GitHub(和其他服务器)进行双重身份验证。并具有用于初始登录的图形界面。

For Cygwin users (or users already using the official Git for Windows), you might prefer the manual install. Download the zip package from the releases page. Extract the package, and then run the install.cmdfile. This will install to your ~/binfolder. (Be sure your ~/bindirectory is in your PATH.) You then configure it using this command:

对于 Cygwin 用户(或已经使用 Windows 官方 Git 的用户),您可能更喜欢手动安装。从发布页面下载 zip 包。解压缩包,然后运行该install.cmd文件。这将安装到您的~/bin文件夹。(确保您的~/bin目录在 PATH 中。)然后使用以下命令配置它:

git config --global credential.helper manager

Git will then run the git-credential-manager.exewhen authenticating to any server.

Git 将git-credential-manager.exe在对任何服务器进行身份验证时运行。