运维的同事切换服务器, ssh 端口给关了, 临时只能用https 与gitlab交互。
1 查看当前的远程地址
$ git remote -v
origin git@git.xxxx.net:APS-AI/aps-domestic.git (fetch)
origin git@git.xxxx.net:APS-AI/aps-domestic.git (push)
2 更新新的URL 路径
2.1 查看新的路径
从 gitlab 项目的下查看 https 地址
2.2 更改
~@~ MINGW64 /d/worker_project/aps-domestic (grb_test)
$ git remote set-url origin https://git.xxxx.net/APS-AI/aps-domestic.git
~@~ MINGW64 /d/worker_project/aps-domestic (grb_test)
$ git remote -v
origin https://git.xxxx.net/APS-AI/aps-domestic.git (fetch)
origin https://git.xxxx.net/APS-AI/aps-domestic.git (push)
可以看到已经切换完成了。如果用git pull 测试, 可能会遇到 Authentication failed , 可以执行消除以前 SSH 登录方式的影响
git config --system --unset credential.helper
3 保存账号密码
改完以后,每次更新都要提示密码, 可以执行下面的这条:
git config --global credential.helper store
输入一次用户名, 密码后, 就会记住了, 不用每次重复输入。 也可以参考网上的例子, 设置密码过期时间(没有尝试, 我的是本地PC, 就需要永久记住):
git config credential.helper 'cache --timeout=3600' # 1小时后过期
注意: 如果是公共机器,要将上面的 --global 去掉,只需要设置你自己的repo的账号密码。
4 修改密码
本地存储了旧的密码, 这个时候如果修改了账号的密码, 本地存储的密码不会自动更新,执行 git 操作时会遇到下面的错误
remote: HTTP Basic: Access denied. The provided password or token is incorrect or your account has 2FA enabled and you must use a personal access token instead of a password. See https://git.xxxx.net/help/topics/git/troubleshooting_git#error-on-git-fetch-http-basic-access-denied
可以执行下面的命令后, 再次执行 git 操作, 会提示输入密码。
git config --system --unset credential.helper