git - 远程添加origin vs remote set-url origin

本文讨论了在Git中使用`git remote add origin`和`git remote set-url origin`的区别。`git remote add origin`用于添加新的远程仓库,而`git remote set-url origin`则是用来更改已存在的远程仓库URL。若在`git init`后立即使用`remote set-url origin`,则会更新已存在的origin。在需要添加或更改远程仓库时,这两个命令提供了不同的功能。
摘要由CSDN通过智能技术生成

本文翻译自:git - remote add origin vs remote set-url origin

I create a new repository: 我创建了一个新的存储库:

git init
echo "# MESSAGE" >> README.md
git add README.md
git commit -m "first commit"

Then I want to push my commit to the empty remote repository created on github so I have to set remote. 然后我想将我的提交推送到在github上创建的空远程存储库,所以我必须设置远程。

What is difference between using following commands ? 使用以下命令有什么区别? :

git remote add origin git@github.com:User/UserRepo.git
git remote set-url origin git@github.com:User/UserRepo.git

At the end I perform push: 最后我执行推送:

git push -u origin master

Edit1: EDIT1:

What happens when I call remote set-url origin just after git init ? 当我在git init之后调用remote set-url origin时会发生什么? Does remote set-url origin create origin ? 远程set-url origin是否创建了origin? If origin already exists after git init there is no difference between using those commands in my scenario, right ? 如果在git init之后已经存在origin,那么在我的场景中使用这些命令没有区别,对吧?


#1楼

参考:https://stackoom.com/question/2tiBR/git-远程添加origin-vs-remote-set-url-origin


#2楼

To add a new remote, use the git remote add command on the terminal, in the directory your repository is stored at. 要添加新远程数据库,请在存储库所在目录中的终端上使用git remote add命令。

The git remote set-url command changes an existing remote repository URL. git remote set-url命令更改现有的远程存储库URL。

So basicly, remote add is to add a new one, remote set-url is to update an existing one 所以基本上, remote add是添加新的, remote set-url是更新现有的


#3楼

below is used to a add a new remote: 下面用于添加新的遥控器:

git remote add origin git@github.com:User/UserRepo.git

below is used to change the url of an existing remote repository: 下面用于更改现有远程存储库的URL:

git remote set-url origin git@github.com:User/UserRepo.git

below will push your code to the master branch of the remote repository defined with origin and -u let you point your current local branch to the remote master branch: 下面将您的代码推送到使用origin定义的远程存储库的主分支, -u允许您将当前本地分支指向远程主分支:

git push -u origin master

Documentation 文档


#4楼

  • When you run git remote add origin git@github.com:User/UserRepo.git , then a new remote created named origin . 当你运行git remote add origin git@github.com:User/UserRepo.git ,然后创建一个名为origin的新远程。
  • When you run git remote set-url origin git@github.com:User/UserRepo.git ,git searches for existing remote having name origin and change it's remote repository url. 当你运行git remote set-url origin git@github.com:User/UserRepo.git ,git会搜索具有name origin现有远程并更改它的远程存储库url。 If git unable to find any remote having name origin , It raise an error fatal: No such remote 'origin' . 如果git无法找到任何具有名称origin远程,它会引发一个fatal: No such remote 'origin'的错误fatal: No such remote 'origin'

If you are going to create a new repository then use git remote add origin git@github.com:User/UserRepo.git to add remote. 如果要创建新的存储库,请使用git remote add origin git@github.com:User/UserRepo.git添加远程。


#5楼

Below will reinitialize your local repo; 下面将重新初始化您的本地回购; also clearing remote repos (ie origin): 还清除远程回购(即原产地):

git init

Then below, will create 'origin' if it doesn't exist: 然后在下面,如果它不存在,将创建'origin':

git remote add origin [repo-url]

Else, you can use the set-url subcommand to edit an existing remote: 否则,您可以使用set-url子命令编辑现有远程:

git remote set-url origin [repo-url]

Also, you can check existing remotes with 此外,您可以检查现有的遥控器

git remote -v

Hope this helps! 希望这可以帮助!


#6楼

git remote add => ADDS a new remote. git remote add => ADDS一个新的遥控器。

git remote set-url => UPDATES existing remote. git remote set-url => 更新现有的远程。


  1. The remote name that comes after add is a new remote name that did not exist prior to that command. add后出现的远程名称是在该命令之前不存在的新远程名称。
  2. The remote name that comes after set-url should already exist as a remote name to your repository. set-url之后的远程名称应该已作为存储库的远程名称存在。

git remote add myupstream someurl => myupstream remote name did not exist now creating it with this command. git remote add myupstream someurl => myupstream远程名称现在不存在,现在使用此命令创建它。

git remote set-url upstream someurl => upstream remote name already exist i'm just changing it's url. git remote set-url upstream someurl =>上游远程名称已经存在我只是更改它的url。


git remote add myupstream https://github.com/nodejs/node => **ADD** If you don't already have upstream
git remote set-url upstream https://github.com/nodejs/node # => **UPDATE** url for upstream
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值