提交改动到 github 远程服务器,怎么跳过要求输入密码的步骤

本文介绍了如何在新机器上避免每次提交代码到GitHub时输入密码。通过修改工程根目录的.git/config文件,将HTTPS链接改为SSH链接,并生成SSH密钥对,将公钥添加到GitHub账户。当存在多个GitHub账号或遇到身份验证错误时,需注意密钥文件的影响。此外,还提供了在Mac OS X中将Git密码存储在钥匙串中的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新机器上将工程改动提交到 github 服务器时,发现每次都要输入密码,这个有点儿小烦人,怎么解决这个问题呢?

首先,切换到工程根目录的 .git 隐藏目录,用 TextEdit 打开 config 文件,改成如下的形式:

[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
[remote "origin"]
	url = git@github.com:user_name/project_name
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
一般情况下只需要改动 [remote "origin"].url 这条,

https://github.com/user_name/project_name.git 改成 git@github.com:user_name/project_name

然后,按照 Generating SSH Keys 所阐述的流程走一遍

(生成 ssh 公钥私钥文件,将公钥数据保存到自己的 github 主页设置中)

这里有一个问题要注意:

如果自己有多个 github 账号同时在使用,且 ~/.ssh 目录中包含了多个密钥文件,

已存在的密钥文件可能对新生成的密钥文件造成影响,直观表现就是身份验证出错:

你是以 A 的身份提交改动却被误判身份为 B,结果导致提交失败。

另外,如果按照上述的流程完整的走了一遍后还是提示身份验证出错,可以在重启机器后再做尝试。


参考资料:

How to save your git password in the Mac OSX keychain

原文内容:

Are you tired of constantly having to enter your Git password all the time? 

Here’s how to make life much easier for you by saving the git password in the Mac OSX keychain.

In order to not having enter your git based password every time you perform a git operation, 

here’s how you can store the git password in the Mac OSX keychain:

Check if you have the credential-osxkeychain helper already installed:

git credential-osxkeychain

If not, the download and install it:

curl http://github-media-downloads.s3.amazonaws.com/osx/git-credential-osxkeychain -o git-credential-osxkeychain

sudo mv git-credential-osxkeychain /usr/local/bin

sudo chmod u+x /usr/local/bin/git-credential-osxkeychain

And make git use the helper:

git config --global credential.helper osxkeychain

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值