iT邦幫忙

0

Git-上傳檔案至遠端儲存庫

git
WM 2019-05-14 23:59:487196 瀏覽

延續Git- 一次刪除全部已合併的分支

建立遠端儲存庫

登入GitHub,在右上角建立新的儲存庫。
https://ithelp.ithome.com.tw/upload/images/20190514/201125730NBtt6wxlh.png

輸入名稱,綠色勾勾表示沒有重複,可以使用。
再按下綠色按鈕建立。
https://ithelp.ithome.com.tw/upload/images/20190514/20112573C4SMsQn8vm.png

上傳檔案

建立完成,會進入此畫面。
https://ithelp.ithome.com.tw/upload/images/20190514/20112573HrUj0ZX25g.png
這時,GitHub會給提示:
…or create a new repository on the command line
建立一個新的儲存庫,再上傳。

…or push an existing repository from the command line
直接上傳現有的儲存庫。

我們選擇要上傳現有的儲存庫。
指令「git remote add origin https: //github.com/s164975/ngapp.git
https://ithelp.ithome.com.tw/upload/images/20190514/20112573vohZUZ5g19.png
這段指令的意思是,將遠端儲存庫的網址「https: //github.com/s164975/ngapp.git」, 加到origin
所以,origin代表遠端儲存庫的網址。
origin只是預設的名稱,可以改成其他名稱,但建議維持預設。

因為我之前已經做過了,所以git會顯示警告訊息「fatal: remote origin already exists.」,表示路徑已存在。

使用git remote -v查看origin所指向的網址。
https://ithelp.ithome.com.tw/upload/images/20190514/20112573Bgwpbi5dEc.png
網址有兩種:
fetch:從遠端儲存庫下載有哪些更新。
push:將本地端的新版本上傳至遠端。

將檔案上傳至遠端儲存庫。
指令「git push -u origin master」。
https://ithelp.ithome.com.tw/upload/images/20190514/20112573NMQ72l1w1f.png
-u 是--set-upstream的簡寫。
在第一次上傳時,要設定上游的遠端分支,不然本地位置會無法與遠端位置建立連結。

這段指令的意思是,將本地的master分支,上傳至origin所指向的網址。

上傳成功後,有幾則比較重要的訊息:

[new branch] master -> master
在遠端資料庫也建立了一條名為master的分支。

Branch 'master' set up to track remote branch 'master' from 'origin'.
我們的master分支設置為追蹤origin/master的分支。

origin/master代表的是,遠端儲存庫的master分支。

上傳成功後,進入GitHub儲存庫查看。
commit紀錄成功上傳。
https://ithelp.ithome.com.tw/upload/images/20190514/201125734RknMfQHAW.png

但只有master分支。
https://ithelp.ithome.com.tw/upload/images/20190514/20112573nlaZmJCdMh.png
所以剛剛的push,只上傳master分支而已。

目前本地端有3條分支。
在分支後面加上-r,可以列出遠端追蹤分支。
https://ithelp.ithome.com.tw/upload/images/20190514/20112573iiqV8S75wf.png

透過我們剛剛第一次push時,設定的-u參數,讓遠端的origin/master分支,跟本地端的master分支有所連結。

連結在一起,有什麼用呢?

接著,我們在master分支commit一個新增檔案的版本。
https://ithelp.ithome.com.tw/upload/images/20190514/20112573CmX7ArjOs3.png
目前,master分支是最新版。
在push之前,遠端的origin/master分支肯定是不會有這個版本。

之後每次push,我們只要打「git push」。
因為已經設定-u的關係,git會自動將master分支上傳至遠端的origin/master。

指令「git branch -a」,可以查看所有本地與遠端的分支。
https://ithelp.ithome.com.tw/upload/images/20190514/20112573OrMcteYX8R.png

或許你會有疑問,為何不一次將所有的分支上傳?

因為某些分支只是小測試,或是不穩定的版本,並不希望上傳至共用的儲存庫。

上傳其他分支

假設我們要上傳feature分支。
一樣執行指令「git push -u origin feature」。
https://ithelp.ithome.com.tw/upload/images/20190514/20112573Owd5CXJfhu.png
訊息顯示,遠端新建了一個feature分支,並且跟本地端的feature分支有所連結。

執行「git branch -a」。
https://ithelp.ithome.com.tw/upload/images/20190514/201125730OJJ7lzwDd.png
多了一條遠端的feature分支。

線圖。
https://ithelp.ithome.com.tw/upload/images/20190514/20112573YmQmLHpJtq.png

遠端儲存庫。
https://ithelp.ithome.com.tw/upload/images/20190514/20112573ML1k9AMLsl.png


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言