👋 We've unified all of GitHub's product documentation in one place! Check out the content for REST API, GraphQL API, and Developers. Learn more on the GitHub blog.

文章版本: Enterprise Server 2.20

Caching your GitHub credentials in Git

If you're cloning GitHub Enterprise repositories using HTTPS, you can use a credential helper to tell Git to remember your credentials.

If you clone GitHub Enterprise repositories using SSH, then you authenticate using an SSH key instead of using other credentials. For information about setting up an SSH connection, see "Generating an SSH Key."


  • 您需要 Git 1.7.10 或更高版本才能使用 osxkeychain 凭据小助手。
  • 如果您使用 Homebrew 安装了 Git,则已经安装了 osxkeychain 助手
  • 如果您运行 Mac OS X 10.7 及更高版本,并且通过 Apple 的 Xcode 命令行工具安装了 Git,则 osxkeychain 助手自动包含在您的 Git 安装中。

安装 Git 和 osxkeychain 助手并告诉 Git 使用它。

  1. 核实是否已安装 Git 和 osxkeychain 助手

    $ git credential-osxkeychain
    # Test for the cred helper
    > Usage: git credential-osxkeychain <get|store|erase>
  2. 如果 osxkeychain helpper 尚未安装,而您使用的是 OS X 10.9 或更高版本,您的计算机会提示您将其下载为 Xcode Command Line 工具的一部分:

    $ git credential-osxkeychain
     > xcode-select: note: no developer tools were found at '/Applications/Xcode.app',
     > requesting install. Choose an option in the dialog to download the command line developer tools.

    或者,您也可以使用 Homebrew 安装 Git 和 osxkeychain helper

    $ brew install git
  3. 使用 global credential.helper config 指示 Git 使用 osxkeychain helper

    $ git config --global credential.helper osxkeychain
    # Set git to use the osxkeychain credential helper

The next time you clone an HTTPS URL that requires authentication, Git will prompt you for your username and password. For enhanced security, we recommend using a personal access token (PAT) instead of a password. Treat your PAT just like a password and enter your PAT when prompted for a password. For more information, see "Creating a personal access token."

Once you've authenticated successfully, your credentials are stored in the macOS keychain and will be used every time you clone an HTTPS URL. You won't be required to type your credentials in to Git again unless you change your credentials.

提示:您需要 Git 1.7.10 或更高版本才能使用凭据小助手。

您还可以安装本机 Git shell,例如 Windows 版 Git。 使用 Windows 版 Git 时,在命令行中运行以下内容将存储凭据:

$ git config --global credential.helper wincred

提示:您需要 Git 1.7.10 或更高版本才能使用凭据小助手。

开启凭据小助手使 Git 将您的密码在内存中保存一段时间。 默认情况下,Git 会缓存密码 15 分钟。

  1. 在终端,输入以下命令:
    $ git config --global credential.helper cache
    # Set git to use the credential memory cache
  2. 要更改默认的密码缓存时限,请输入以下命令:
    $ git config --global credential.helper 'cache --timeout=3600'
    # Set the cache to timeout after 1 hour (setting is in seconds)