git pull 时每次都要输入账号和密码的解决办法

当使用HTTPS方式克隆Git仓库时,每次push或pull都需要输入账号密码,这很不便。解决方法包括切换为SSH协议或使用git config设置credential.helper为store,将账号密码缓存。切换SSH协议需添加机器的SSH公钥到远程仓库,而使用credential.helper store则会在本地存储凭证,避免重复输入密码。

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

如果我们 git clone 下载代码的时候是连接的 https:// (就是 git remote -v 显示的那个 url)

root@master ~/xxx# git remote -v
origin	https://gitee.com/account/repo.git (fetch)
origin	https://gitee.com/account/repo.git (push)

而不是 git@git (ssh) 的形式,当我们操作 git pull/push 到远程的时候,总是提示我们输入账号和密码才能操作成功,频繁的输入账号和密码会很麻烦。

解决办法1:

把原创仓库地址写成用 git@git 的形式(记得添加机器的 ssh 公钥到远端仓库哟!!!)

root@master ~/xxx# git remote rm origin
root@master ~/xxx# git remote add origin git@gitee.com:account/repo.git
root@master ~/xxx# git remote -v
origin	git@gitee.com:account/repo.git (fetch)
origin	git@gitee.com:account/repo.git (push)

解决办法2(常用):

cd xxx 进入你的项目目录,输入:

git config --global credential.helper store

输入以上指令后,下面文件的最后会多出以下内容:

root@master ~/xxx# cat ~/.gitconfig 
[user]
	name = looking
	email = looking@qq.com
[credential]
	helper = store

然后你使用上述的命令配置好之后,再操作一次 git pull,然后它会提示你输入账号密码,这一次之后就不需要再次输入密码了。

root@master ~/xxx# git pull
Username for 'https://gitee.com': 
Password for 'https://account@gitee.com': 
From https://gitee.com/account/repo
 * [new branch]      database   -> origin/database
 * [new branch]      master     -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to rebase against.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> master

关联分支:

root@master ~/xxx# git branch --set-upstream-to=origin/master
Branch 'master' set up to track remote branch 'master' from 'origin'.

 同时在与 .gitconfig  相同目录下会存在一个 .git-credentials 文件,上边就已经记录了你刚才输入的账号和密码,之后操作就不用再输入密码了。

root@master ~/insight-tool# cat ~/.git-credentials 
https://account:passwd@gitee.com

 


 

idea git提交每次都要登录的问题可以通过以下方法解决: 首先,我们需要在项目中使用git pull命令拉取最新的代码,确保代码是最新的。 然后,我们可以通过运行命令git config --global credential.helper store来保存我们的用户名密码。这样,当我们进行git push操作,系统就会自动使用我们之前保存的用户名密码,而不需要每次手动输入。 这样,每次进行git提交就不再需要登录了,而是直接使用之前保存的用户名密码进行提交。 另外,如果你在使用idea进行git操作,可以使用idea的相关功能来解决这个问题。例如,可以通过配置idea的Git设置来保存用户名密码,或者配置SSH密钥来实现自动登录。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [IDEA中git每次push都要输入用户名密码](https://blog.csdn.net/qq_37530429/article/details/123575214)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [详解idea切换git账号的两个方法](https://download.csdn.net/download/weixin_38624332/12924313)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值