当前位置: 首页 > 工具软件 > git-point > 使用案例 >

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

鲍理
2023-12-01

本文翻译自: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
 类似资料: