[Git] git remote add origin的origin意思解析

當想要從在地端push資料上Git管理網時,會先在網頁上先建一個Repository,然後跟著網頁的指示做。

以下以個人的GitHub為例子,
  1. 創建一個Repository名稱為zcu111-EMIO,且「不」打勾"Initialize this repository with a README"
    …or push an existing repository from the command line
    
    git remote add origin https://github.com/weirenxue/zcu111-EMIO.git
    git push -u origin master
    
  2. 按照它的提示,就能把在地端的Git push上去。
  3. 這時再用git remote -v來看看remote資料
    user@pc:~$ git remote -v
    origin https://github.com/weirenxue/zcu111-EMIO.git (fetch)
    origin https://github.com/weirenxue/zcu111-EMIO.git (push)
    
  4. 很明顯的看得出來,origin就只是那remote git的名稱!為了讓使用者不必每次對remote操作都要打一堆,也就是說我們也可以把origin改為別的名稱
    user@pc:~$ git remote rename origin origin-github  #改名
    
    user@pc:~$ git remote -v
    origin-github https://github.com/weirenxue/zcu111-EMIO.git (fetch)
    origin-github https://github.com/weirenxue/zcu111-EMIO.git (push)
    
  5. 更甚者,如果有兩個Git管理網要push的話(例如:公司專用/個人習慣用),可以再加入一個remote!
    user@pc:~$ git remote add origin-company http://192.168.0.123/fake/zcu111-EMIO.git
    
    user@pc:~$ git remote -v
    origin-github https://github.com/weirenxue/zcu111-EMIO.git (fetch)
    origin-github https://github.com/weirenxue/zcu111-EMIO.git (push)
    origin-company http://192.168.0.123/fake/zcu111-EMIO.git (fetch)
    origin-company http://192.168.0.123/fake/zcu111-EMIO.git (push)
    
  6. push的方法都沒變
    user@pc:~$ git push origin-github master #到GitHub
    
    user@pc:~$ git push origin-company master#到公司
    
  7. 結語:Git真的是程式開發者的一個大福音,在知道Git之前常常在實驗室用桌電開發,回家前上傳雲端或隨身碟,然後回到宿再抓下來繼續開發,久了之後檔案多了,非常難管理,也很難找到之前到底修改了哪裡。有了Git之後,光是管理程式碼的部份就省很多時間!

留言

這個網誌中的熱門文章

[Hyper-V] 讓 Windows 可以吃到超過 16TB 的硬碟!