我想在 Git Extensions、Sourcetree 或其他 Git GUI 中自动使用 push 和 pull,无需每次手动输入用户名和密码。
那么我该如何保存我的 Git 凭据呢?
我想在 Git Extensions、Sourcetree 或其他 Git GUI 中自动使用 push 和 pull,无需每次手动输入用户名和密码。
那么我该如何保存我的 Git 凭据呢?
如果Git客户端不需要考虑安全问题,可以按照以下方式编辑URL:
git remote set-url origin https://${access_token}@github.com/${someone}/${somerepo}.git
在git clone
的情况下同样如此:
git clone https://${access_token}@github.com/${someone}/${somerepo}.git
个人不喜欢使用global
域的git config
,因为在多账户情况下会很混乱。
access_token
是你可以在设置/开发者设置/个人访问令牌
中生成的。记得将其授权为repo
范围。
[credential "helperselector"] selected = wincred
您将在Windows 凭据管理器中找到凭据。
在那里您可以编辑凭据。
注意:Wincred已被弃用,请参见...
所以,作为替代方案,您可能希望重新配置Git使用内置的Git凭据管理器...git config --global credential.helper manager
根据rofrol的评论,在Linux Ubuntu上,可以按照此答案的步骤进行操作:
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
一些其他的发行版提供二进制文件,因此您不必自己构建它。
在OS X中,通常会默认添加一个名为"osxkeychain"的模块,因此您可以免费获取。 OS X内置的和homebrew版本的都默认有此功能。
git config --global credential.helper store
或git config --global user.name "your username"
等解决方案。Unset global credentials helper, in case some former experimentation gets in the way :)
git config --global --unset credentials.helper
Move to the root directory of your repo and disable the local credential helper (if needed)
cd /path/to/my/repo
git config --unset credential.helper`
Create a file to store your repo's credentials into
git config credential.helper 'store --file ~/.git_repo_credentials'
Note: this command creates a new file named ".git_repo_credentials" into your home directory, to which Git stores your credentials. If you do not specify a file name, Git uses the default ".git_credentials". In this case simply issuing the following command will do:
git config credential.helper store
set your username
git config credential.*.username my_user_name
Note: using "*" is usually ok if your repositories are from the same provider (e.g. GitLab). If instead your repositories are hosted by different providers then I suggest to explicitly set the link to the provider for every repository, like in the following example (for GitLab):
git config credential.https://gitlab.com.username my_user_name
git pull
),系统会要求您输入与“my_user_name”相对应的密码。这只需要一次,因为git将凭据存储到“.git_repo_credentials”中,并在后续访问中自动使用相同的数据。https://github...
替换为github...
。 - Tonsiccredential.helper=manager
如果您没有收到凭据提示,请按照以下步骤操作:
还要确保您未设置HTTP_PROXY
、HTTPS_PROXY
、NO_PROXY
环境变量,如果您使用代理并且您的 Git 服务器位于内部网络。
您还可以使用链接到凭证管理器二进制文件的C:\Users\\AppData\Local\Programs\Git\mingw64\libexec\git-core
中的git-gui
测试 Git 的获取/推送/拉取功能。
GitHub的推荐方法已经改变,而且最好的方法也是最简单的。 详情请见此处
brew install gh
。gh auth login
,然后按照提示操作。不再需要密码!不再需要个人访问令牌!不再需要 SSH 密钥!
第一次推送时,该助手将打开浏览器窗口以进行身份验证。在缓存超时内的后续推送不需要任何交互。
从 https://github.com/hickford/git-credential-oauth/releases/ 安装
使用以下配置:
git config --global --unset-all credential.helper
git config --global --add credential.helper "cache --timeout 7200" # two hours
git config --global --add credential.helper oauth
git: 'credential-oauth' is not a git command. See 'git --help'.
- DollarAkshaygit-credential-oauth
二进制文件在PATH中,并且可执行。 - Colonel Panicgit config --global user.name "your username"
git config --global user.password "your password"
git config --list
~/.git-credentials
,而应将它们视为加密的秘密。https://your_user_name:your_token@github.com/your_user_name/your_repo_name.git
然后您可以使用它来克隆公共或私有存储库以及其子模块,并自动执行推送和拉取操作。
# put the credentials in a variable
export REPOSITORY=${{ secrets.REPOSITORY }}
# git clone public or private repos
git clone --recurse-submodules -j8 ${REPOSITORY}
# git pull will do automatic
cd /path/to/the/repo
git pull
# git push to a branch in the repo
git add . && \
git commit -m "Action from ${GITHUB_SHA}" && \
git push --force $REPOSITORY master:$BRANCH