我把我的工作推到一个远程git存储库。
每次推送都会提示我输入用户名和密码。我想避免它的每一个推送,但如何配置以避免它?
我把我的工作推到一个远程git存储库。
每次推送都会提示我输入用户名和密码。我想避免它的每一个推送,但如何配置以避免它?
当前回答
只是想指出上面说过几次的解决方案:
git config credential.helper store
在此之后,您可以使用任何需要密码的命令。你不需要用力。之后,你就不需要再输入你的用户名/密码了。
其他回答
我在Windows上的解决方案:
右键单击目录,并选择“Git Bash Here”。 ssh-keygen -t rsa(所有值都按enter键) 您的公钥已保存在/c/Users/<your_user_name_here>/.ssh/id_rsa.pub 从指定文件复制公钥。 去你的GitHub配置文件=>设置=> SSH和GPG密钥=>新的SSH密钥=>粘贴您的SSH密钥=>保存
如果你的电脑是安全的,或者你不关心密码安全,这可以很简单地实现。假设远程存储库位于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>
据我所知,只有两种安全的方法:使用密钥库加密的ssh或passwd。
SSH
Log in your github; Visit: https://github.com/settings/keys; New SSH key; cat ~/.ssh/id_rsa.pub, paste it there, name and save it (if you have no such file, generate one for yourself by ssh-keygen -t rsa - just Enter for all prompts); Go to your local repository and update your remote by git remote set-url origin git+ssh://git@github.com/username/reponame.git - you can check it first by git remote -v); Here you go, just touch t; git add t; git commit -m "test"; git push and confirm yes to enjoy the password-free world.
passwd使用密钥库加密
如果你只使用git config -global credential。如其他人所述,您未加密的密码将以纯文本形式存储在~/下。git证书听起来并不安全。
尝试将其加密为
sudo apt-get install libgnome-keyring-dev
sudo make --directory=/usr/share/doc/git/contrib/credential/gnome-keyring
git config --global credential.helper /usr/share/doc/git/contrib/credential/gnome-keyring/git-credential-gnome-keyring
git config --global credential.helper store
在本例中,您使用https://git@github.com/username/reponame.git。
在Windows操作系统上使用这个代替,这对我来说是有效的:
https://{Username}:{Password}@github.com/{Username}/{repo}.git
e.g.
git clone https://{Username}:{Password}@github.com/{Username}/{repo}.git
git pull https://{Username}:{Password}@github.com/{Username}/{repo}.git
git remote add origin https://{Username}:{Password}@github.com/{Username}/{repo}.git
git push origin master
这一切都是因为git在克隆/拉/推/取命令中没有提供通过管道发送凭证的选项。尽管它提供了证明。Helper,它存储在文件系统或创建一个守护进程等。通常,GIT的凭据是系统级的凭据,调用GIT命令的应用程序有责任保证它们的安全。非常不安全。
以下是我必须解决的问题。 1. Git版本(Git——version)应该大于或等于1.8.3。
吉特克隆
对于克隆,使用“git克隆URL”,将URL的格式从http://{myuser}@{my_repo_ip_address}/{myrepo_name.git}改为http://{myuser}:{mypwd}@{my_repo_ip_address}/{myrepo_name.git}
然后清除存储库中的密码,如下一节所示。
清除
现在,这个已经消失了
written the password in git remote origin. Type "git remote -v" to see the damage. Correct this by setting the remote origin URL without password. "git remote set_url origin http://{myuser}@{my_repo_ip_address}/{myrepo_name.git}" written the password in .git/logs in the repository. Replace all instances of pwd using a unix command like find .git/logs -exec sed -i 's/{my_url_with_pwd}//g' {} \; Here, {my_url_with_pwd} is the URL with password. Since the URL has forward slashes, it needs to be escaped by two backward slashes. For ex, for the URL http://kris:passs123@192.168.0.1/proj.git -> http:\\/\\/kris:passs123@192.168.0.1\\/proj.git
如果您的应用程序使用Java来发出这些命令,请使用ProcessBuilder而不是Runtime。如果必须使用Runtime,请使用getRunTime()。exec以字符串数组作为参数,以/bin/bash和-c作为参数,而不是以单个字符串作为参数。
GIT FETCH/PULL / PUSH
在git远程url中设置密码为:"git remote set_url origin http://{myuser}:{mypwd}@{my_repo_ip_address}/{myrepo_name.git}" 发出git fetch/push/pull命令。然后将不会提示您输入密码。 按照前一节的方法进行清理。不要错过。