Git中https和ssh相互切换方法

本文详细介绍了如何使用Git的remote set-url命令来更改已存在的远程仓库URL,包括从SSH切换到HTTPS,以及从HTTPS切换到SSH的过程。通过具体的命令行操作步骤,帮助读者理解和掌握远程仓库URL的修改方法。

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

搬运自https://www.cnblogs.com/h07061108/p/url_ssh_https.html

Changing a remote’s URL

The git remote set-url command changes an existing remote repository URL.

Tip: For information on the difference between HTTPS and SSH URLs, see “Which remote URL should I use?”

The git remote set-url command takes two arguments:

An existing remote name, for example, origin
A new URL for the remote, for example:

  1. https://github.com/USERNAME/REPOSITORY_2.git if you’re updating to use HTTPS
  2. git@github.com:USER/REPOSITORY_2.git if you’re updating to use SSH

Switching remote URLs from SSH to HTTPS

  • Open Terminal (for Mac and Linux users) or the command line (for Windows users).

  • Change the current working directory to your local project.

  • List your existing remotes in order to get the name of the remote you want to change.
    $ git remote -v
    #origin git@github.com:USERNAME/REPOSITORY.git (fetch)
    #origin git@github.com:USERNAME/REPOSITORY.git (push)

  • Change your remote’s URL from SSH to HTTPS with the remote set-url command.
    $ git remote set-url origin https://github.com/USERNAME/REPOSITORY_2.git

  • Verify that the remote URL has changed.
    $ git remote -v
    #Verify new remote URL
    #origin https://github.com/USERNAME/REPOSITORY2.git (fetch)
    #origin https://github.com/USERNAME/REPOSITORY2.git (push)

    The next time you git fetch, git pull, or git push to the remote repository, you’ll be asked for your GitHub username and password.

  • If you have two-factor authentication enabled, you must create a personal access token to use instead of your GitHub password.

  • You can use a credential helper so Git will remember your GitHub username and password every time it talks to GitHub.

Switching remote URLs from HTTPS to SSH

  1. Open Terminal (for Mac and Linux users) or the command line (for Windows users).
  2. Change the current working directory to your local project.
  3. List your existing remotes in order to get the name of the remote you want to change.
    $ git remote -v
    #origin https://github.com/USERNAME/REPOSITORY.git (fetch)
    #origin https://github.com/USERNAME/REPOSITORY.git (push)
  4. Change your remote’s URL from HTTPS to SSH with the remote set-url command.
    $ git remote set-url origin git@github.com:USERNAME/REPOSITORY2.git
  5. Verify that the remote URL has changed.
    $ git remote -v
    #Verify new remote URL
    #origin git@github.com:USERNAME/REPOSITORY2.git (fetch)
    #origin git@github.com:USERNAME/REPOSITORY2.git (push)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值