Git 基礎介紹、常用指令介紹及Github實作練習
Git 是什麼?
一種分散式版本控制系統Distributed Revision Control System
,可用來管理專案Repository
,也非常適合用來多人協作一個專案。
以下這個圖大致描述了分散式版本控制系統的模式,所有參與專案的人都會得到一份完整鏡像的專案內容,再透過分支推送等等操作來多人完成一個專案。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
引用自https://git-scm.com/
安裝方式
請至官方網站 https://git-scm.com/進行下載
Git 常用指令
-
git init
初始化資料夾(建立本地專案)
這個指令會讓我們本機端的專案資料夾成功加入Git系統內,示意圖如下↓
我們可以看到加入成功後會出現第一個分支master
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
git status
確認當前 git 狀態
這個指令會在Command Line介面顯示出你目前的 git 狀態,是否有異動或需要提交等等
以下的示意圖分成兩種,一個是沒有異動的狀態,而另一個是做了異動(新增檔案)↓
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
可以看到若有異動會以紅色文字提示↓
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
git add
將檔案加入 git 版本控制
這個指令可以讓我們把檔案加入 git 版本控制系統內
可以看到新增後使用git status
確認就可以看到檔案已成功加入,以下是示意圖↓
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
git commit
提交異動
這個指令可以讓我們把異動的部分提交給 git 版本庫,類似存檔的概念,
在目前動作設置一個存檔點。
這邊使用的指令是git commit
的小變化,示意圖如下↓
git commit -m "註解"
提交異動並新增註解
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
git branch
查看分支及分支操作
這個指令可以用來查看目前這個專案有幾個分支,目前只有一個master
分支,示意圖如下↓
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
git branch
這個指令可以加上一些變化用來操作分支的狀態
-
git branch "brachname"
加上了名稱可以新增分支
新增了一個demo分支↓
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
git branch -d "branchname"
加上-d
可以刪除分支
刪除了剛剛建立的demo分支
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
git checkout "branchname"
切換分支
這個指令可以切換目前所在分支↓
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
-
git push
發布至遠端專案
這個指令可以將我們本地端的專案推送到遠端:github等網站。
-
git clone
下載遠端專案
這個指令則是可以讓我們從遠端下載專案到本機端進行編輯。
Github又是什麼?
GitHub 是一個以 Git 為基礎的服務平台,
為開發者提供雲端版本控制服務Repository Hosting
。
在Github上我們可以發布自己的專案或者參與別人的專案,同時也可能會有人來參與你的專案。
接下來我們要稍微說明一下建立專案Repository
的流程↓
首先你要有一個Gihub的帳號,請到Github申請。
你有了帳號後可以在這裡新增專案,請點選右上角的New↓
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
接著填入你想要的專案名稱並填入一些設定↓
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
接著我們就可以看到專案成功建立↓
你可以選擇在本地端利用Command Line建立新專案的資料夾
請跟著create a new repository on the command line
進行操作。
或是你在本地已經有專案,請跟著push an existing repository from the command line
操作
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Github Page 上傳靜態網站實作
以下是使用Command Line來上傳本機端的網頁到github上的流程介紹。
首先我們使用mv index.html firstPage
指令將我們的網頁放進專案的資料夾中,並移動到firstPage
資料夾內。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
接著使用git status
指令確認資料夾內的狀態,可以看到畫面上出現了紅色的index.html
,這代表目前我們新增的index.html
檔案尚未加入git內。
所以我們將這個動作使用git add index.html
指令加入git中,再次用git status
指令確認資料夾內的狀態,這次可以看到新檔案已經變成綠色,代表已經成功加入git。
接下來的指令git commit -m "add index.html"
是將目前的改動提交並新增註解"add index.html"
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
再來會一步一步使用git指令進行新增分支branch
,推送到github的步驟↓
首先我們使用git branch gh-page
來新增一個名稱叫做gh-page
的branch
↓
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
之後利用git branch
指令來查看是否新增成功↓
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
這個畫面上可以看到我們目前有main
跟gh-pages
兩個分支↓
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
我們繼續利用git checkout gh-pages
來將當前分支從main
切換到gh-pages
↓
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
切換完成的畫面如下↓
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
現在要開始將本地的檔案推送push
到github上,使用指令git push
,但因為目前github上並沒有這個名稱的分支branch
,所以畫面上提示我們需要輸入以下指令來完成推送push
↓
git push –set-upstream origin gh-pages
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
輸入之後會開始跑一下,最終成功顯示的結果是↓
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
而我們在github頁面上我們可以看到分支成功建立↓
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
而我們剛剛新增的index.html
網頁檔案也成功地上傳到分支內了↓
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
上傳成功後我們就可以透過github的網址在瀏覽器上顯示我們的靜態網站了。
以我自己舉例來說網址會是:https://jessie2534.github.io/firstPage