我把我的工作推到一个远程git存储库。
每次推送都会提示我输入用户名和密码。我想避免它的每一个推送,但如何配置以避免它?
我把我的工作推到一个远程git存储库。
每次推送都会提示我输入用户名和密码。我想避免它的每一个推送,但如何配置以避免它?
当前回答
看起来,至少在Windows上使用TortoiseGIT时,可以创建SSH密钥并将其传输到GIT服务器,只需使用:
> ssh-keygen.exe
> ssh-copy-id [username]@[GIT_server]
其他回答
如果你的电脑是安全的,或者你不关心密码安全,这可以很简单地实现。假设远程存储库位于GitHub上,origin是远程存储库的本地名称,请使用此命令
git remote set-url --push origin https://<username>:<password>@github.com/<repo>
——push标志确保只更改git push命令的存储库URL。(在最初的帖子中问的问题只是关于git push命令。仅推送操作需要用户名+密码是GitHub上公共存储库的正常设置。注意,GitHub上的私有存储库也需要用户名+密码来进行拉取操作,所以对于私有存储库,你不会想要使用——push标志…)
警告:这本质上是不安全的,因为:
你的ISP,或者任何记录你的网络访问的人,可以很容易地 在URL中以明文形式查看密码; 任何访问你电脑的人都可以使用git remote show origin查看你的密码。
这就是为什么使用SSH密钥是可以接受的答案。
即使是SSH密钥也不是完全安全的。例如,任何获得PC访问权限的人仍然可以进行推送操作,破坏你的存储库,或者更糟糕的是,对你的代码进行细微更改的推送提交。(所有推送提交在GitHub上明显可见。但是如果有人想偷偷地改变你的代码,他们可以在不改变提交消息的情况下修改之前的提交,然后强制推送。这将是隐秘的,在实践中很难被发现。)
但泄露密码更糟糕。如果攻击者获得了您的用户名+密码,他们可以做的事情,如锁定您自己的帐户,删除您的帐户,永久删除存储库等。
或者,为了简单和安全,你可以在URL中只提供你的用户名,这样你每次推送git时都需要输入你的密码,但你不必每次都提供你的用户名。(我很喜欢这种方法,每次git push时都需要输入密码,这样我就不会不小心git push了。)
git remote set-url --push origin https://<username>@github.com/<repo>
只是想指出上面说过几次的解决方案:
git config credential.helper store
在此之后,您可以使用任何需要密码的命令。你不需要用力。之后,你就不需要再输入你的用户名/密码了。
第一步-
使用以下命令在linux系统上创建SSH密钥
ssh-keygen -t rsa -b 4096 -C "your_email"
它将询问密码短语和文件名(默认为~/)。ssh / id_rsa ~ / . ssh / id_rsa . pub)
步骤2 -
一旦文件创建,添加公钥id_rsa。Pub到github帐户SSH部分。
第三步-
在您的机器上,使用以下命令将私钥id_rsa添加到ssh-agent
ssh-add ~/.ssh/id_rsa
步骤4 -
现在使用以下命令将远程url git@github.com:user_name/repo_name.git添加到本地git repo中。
git remote remove origin
git remote add origin git@github.com:user_name/repo_name.git
这是它。
无限期保存
您可以使用git-credential-store
git config credential.helper store
在文件系统中保存未加密的密码:
使用此助手将在磁盘上存储未加密的密码,仅受文件系统权限的保护。如果这不是一种可以接受的安全折衷,请尝试git-凭据-缓存,或寻找与操作系统提供的安全存储集成的帮助器。
使用暂停
使用git-credential-cache,默认保存密码15分钟。
git config credential.helper cache
要设置不同的超时,使用——timeout(这里是5分钟)
git config credential.helper 'cache --timeout=300'
安全无限保存(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]
我在Windows上的解决方案:
右键单击目录,并选择“Git Bash Here”。 ssh-keygen -t rsa(所有值都按enter键) 您的公钥已保存在/c/Users/<your_user_name_here>/.ssh/id_rsa.pub 从指定文件复制公钥。 去你的GitHub配置文件=>设置=> SSH和GPG密钥=>新的SSH密钥=>粘贴您的SSH密钥=>保存