Try   HackMD

壹、目錄及代辦事項

Table of Contents

待學習項目

  • -
  • -
  • -
  • -

貳、環境安裝篇

一、Windows - 命令提示字元教學

1.命令提示字元:起手式

  • 移動路徑: cd + { 指定要到的路徑 }
  • 回上一層:cd ..
  • 展示這一層有什麼: ls
  • 快速懶人法: cd 資料夾拖曳進bash
  • 開新資料夾: mkdir + { 資料夾名稱 }
  • 開新檔案: touch + { 檔案名稱 }
    在 VScode 裡面用 ni + { 檔案名稱 }

2.Git環境設定

  • 登記使用者信箱:
    git config global user.email "csc981.04@gmail.com"
  • 登記使用者名字:
    git config global user.name "Pai"
  • 輸入確認是否有成功
    git config list
  • 跳出畫面 "Q" 或 ctrl+z

二、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 →

1.建立本地資料庫

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 →

  • 建立一個資料夾
  • cd 指定路徑到這個資料夾
  • 建立master: git init 建立 master
  • 到資料夾檢視是否有 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 →

2.加入索引 add -> 提交 commit

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 add 指定檔案名稱
  • 檢查狀態: git status
    • 這個狀態能檢查,檔案的增減或檔案有無修改
  • 提交更新: git commit -m '修改程式的註解'
  • 檢查紀錄: git log
    可以看到是誰什麼時間點更新了檔案

3. 使用 .gitignore 來忽略不必要放僅版控的檔案

  • mkdir .gitignore 創立一個 .gitignore 檔案

  • 將需要忽略的檔案放進 .gitignore 裡面

    • 忽略單一檔案: 直接把名稱放進來 | 1.js
    • 忽略全部同類型的檔案 : * + { .檔案類型 } | *.html
    • 忽略掉整個資料夾: /資料夾名稱 | /JS
      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 →
  • ignore資料庫

4. 程式還原術

a. 取消索引

  • 取消單一檔案索引: git reset head 檔案名稱
  • 取消所有的索引: git reset head

b. 刪除 commit

  • 刪除某次檔案的 commit: git checkout {ID} 檔案名稱
  • 刪除某次檔案的所有 commit: git reset hard {ID}
    *可以透過 git log 來查 ID

c. 刪除錯了 commit 怎麼辦 ?

  • 使用指令還原: git reset hard ORIG_HEAD

三、上傳到 Github(遠端數據庫)

1. clone repository 指令:

  • git clone {repository 的網址}

2. 上傳程式碼指令:

  • git push

3. 從 Github 找出實用插件流程

  • 在網路上看到可用的插件,可以到 Github 搜尋這個開發者是否有持續更新。
  • 檢視 issue 找出這個插件有沒有一些別人提出來的問題,例如: 瀏覽器兼容性。
  • issue 也可以記錄自己程式碼的修改紀錄,同時連結 commit 可以讓程式碼更清楚

四、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 →

1. HEAD - 瞭解目前所在位置

HEAD : 目前所在位置的指標

  • 瀏覽目前分支指令: **git branch **
  • 瀏覽某一版的程式碼: git checkout {要瀏覽的 commit ID 前四碼}
    與( git checkout {commit ID} 單一檔案名稱) 不同,這是將 commit 還原到當時那個 ID 的狀態,狀態會是在 master
  • 還原到最新的程式碼: git checkout master

2. git branch - 分支創立

  • 創立分支指令: git branch {分支名稱}
  • 查看現有分支: git branch
  • 切換分支指令: git checkout {分支名稱}

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 →

開一個新分支名稱為 feature1,將分支切換到 feature1,新增內容新增新的 commit,試著切換分支看與原本的 master 有什麼不同。
會發現原本的 master 沒變,而新增的 feature 有新增內容。

3. 合併分支

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 merge { branch 名稱 }
  • 切換到原本的 master 主支,使用合併分支指令 git merge,就可以把主支和分支合併在一起。

4. master 與 feature 同時被更新的合併(無衝突版本)

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 →

  • master 與 feature 同時被更新要如何合併
  • 一樣使用 git mergit 在最新的 master 上做合併。

5. master 與 feature 同時被更新的合併(衝突版本)

假如 master 與 feature 分支的某行程式碼試寫在同一行的,就會發生衝突。
這時一樣可以 git merge,不過會讓你選擇衝突的版本。
這時需要再執行 git add . 和 git commit -m 一次,因為發生衝突,git 不會自動幫你 commit

6. git tag - 標籤

用來標記程式的版本,可以透過 git checkout 來切換版本,同時可以寫上備註

  • 查詢標籤指令: git tag
  • 查詢詳細標籤指令(包含備註): git tag -n
  • 刪除標籤指令: git tag -d {標籤名稱}
  • 新增標籤: git tag {標籤名稱}
  • 新增包含備註的標籤: git tag -am {標籤備註} {標籤名稱}
  • 切換到標籤的 commit: git checkout {標籤名稱}

7. git stash 暫存資料

stash 像是本地端的 commit,和 commit 最大的差別是,push 的時候資料不會被上到遠端資料庫。

  • 暫存當前資料: git stash
  • 瀏覽暫存清單: git stash list
  • 還原暫存資料: git stash pop
  • 還原到指定版本的資料: git stash apply stash@{n}
  • 清除最新暫存資料: git stash drop
  • 清除所有暫存: git stash clear

五、Git、Github 團隊協作

1. git remote - 遠端數據庫

再產生 branch 後如果再使用 git push 是沒辦法把資料丟到遠端數據庫的。原因是 git 會不知道你要推送到哪個遠端數據庫

  • 查詢遠端數據庫: git remote
  • 更改遠端數據庫 (rep) 名稱: git remote rename {原名稱} {要更改的名稱}
  • 推送分支到遠端數據庫: git push origin(預設數據庫) branch(分支名稱),或者是 git push set-upsteam origin

2. git pull 拉下遠端數據庫的資料

當要複製遠端數據庫資料時,第一次用 git clone {url},第二次可以使用 git pull
git pull = git fetch + git merge
但有時候你不希望 pull 下來導致自己的數據庫太亂又擔心有衝突時,可以先使用下面這個指令。

git fetch origin(遠端數據庫) branch1(遠端分支)

此時你的分支會多一個 FETCH_HEAD 的分支,這個就是遠端數據庫的分支,可以等到你看過沒問題後,

再合併 FETCH_HEAD 也 ok。