1. 程式人生 > >【轉】GIT REMOTE -v

【轉】GIT REMOTE -v

你的程式碼庫(repository)可以存放在你的電腦裡,同時你也可以把程式碼庫託管到Github的伺服器上。

在預設情況下,origin指向的就是你本地的程式碼庫託管在Github上的版本。

我們假設你首先在github上建立了一個Repository,叫做repository,假設你的Github ID是user1,這個時候指向你的程式碼庫的連結是
https://github.com/user1/repository
如果你在terminal裡輸入
git clone https://github.com/user1/repository
那麼git就會在本地拷貝一份託管在github上的程式碼庫
這個時候你cd到repository
然後輸入
git remote -v
你會看到控制檯輸出
origin https://github.com/user1/repository.git (fetch)
origin https://github.com/user1/repository.git (push)
也就是說git為你預設建立了一個指向遠端程式碼庫的origin(因為你是從這個地址clone下來的)

再假設現在有一個使用者user2 fork了你個repository,那麼他的程式碼庫連結就是這個樣子
https://github.com/user2/repository
如果他也照著這個clone一把,然後在他的控制檯裡輸入
git remote -v
他會看的的就是
origin https://github.com/user2/repository.git (fetch)
origin https://github.com/user2/repository.git (push)
可以看的origin指向的位置是user2的的遠端程式碼庫

這個時候,如果user2想加一個遠端指向你的程式碼庫,他可以在控制檯輸入
git remote add upstream https://github.com/user1/repository.git
然後再輸入一遍 git remote -v

輸出結果就會變為
origin https://github.com/user2/repository.git (fetch)
origin https://github.com/user2/repository.git (push)
upstream https://github.com/user1/repository.git (push)
upstream https://github.com/user1/repository.git (push)
增加了指向user1程式碼庫的upstream,也就是之前對指向位置的命名。

總結來講,顧名思義,origin就是一個名字,它是在你clone一個託管在Github上程式碼庫時,git為你預設建立的指向這個遠端程式碼庫的標籤,
@陳肖恩
的答案並不準確,origin指向的是repository,master只是這個repository中預設建立的第一個branch。當你git push的時候因為origin和master都是預設建立的,所以可以這樣省略,但是這個是bad practice,因為當你換一個branch再git push的時候,有時候就糾結了