本文翻譯自: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 [email protected]:User/UserRepo.git
git remote set-url origin [email protected]: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 [email protected]:User/UserRepo.git
below is used to change the url of an existing remote repository: 下面用於更改現有遠程存儲庫的URL:
git remote set-url origin [email protected]: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
#4樓
- When you run
git remote add origin [email protected]:User/UserRepo.git
, then a new remote created namedorigin
. 當你運行git remote add origin [email protected]:User/UserRepo.git
,然後創建一個名爲origin
的新遠程。 - When you run
git remote set-url origin [email protected]:User/UserRepo.git
,git searches for existing remote having nameorigin
and change it's remote repository url. 當你運行git remote set-url origin [email protected]:User/UserRepo.git
,git會搜索具有nameorigin
現有遠程並更改它的遠程存儲庫url。 If git unable to find any remote having nameorigin
, It raise an errorfatal: 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 [email protected]:User/UserRepo.git
to add remote. 如果要創建新的存儲庫,請使用git remote add origin [email protected]: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
=> 更新現有的遠程。
- The remote name that comes after
add
is a new remote name that did not exist prior to that command.add
後出現的遠程名稱是在該命令之前不存在的新遠程名稱。 - 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