Ally's Blog

邁向前端領域的學習筆記

0%

【Git】關於Git指令 & 創建Github Page網頁

(一) command line 指令

  • 命令提示字元,簡稱cmd,Terminal,有別於使用GUI(圖形使用者介面)。
    使用文字一行一行對電腦下指令,使電腦去執行動作。
    有新增cmder工具輔助,cmder工具可以仿Linux環境下使用cmd。

    • ls : list 顯示 (ls -al 顯示全部)
    • cd : change directory 切換 (cd..回上一層)
    • mkdir : make directory 新增資料夾
    • dir : 顯示檔案清單以列出目錄中的檔案及子目錄
    • move : move *.md folder url

(二) 你知道 Git 在做什麼,以及為何我們需要 Git?

  • 因為手上可能會新增rs1, rs2, rs3…rs100的更新檔案,這100個檔案你會忘記之間的差異在哪,不同版本不同在哪裡。
    這裡就需要版本控制工具,讓你知道差異在哪裡,或是在別人合作專案,更新,衝突,新增,合併在哪。
    Git屬於分散式版本控管系統,有遠端server和本機操作,可以將github做為遠端server。

(三) Git 指令

  • add : 新增修改檔案,進入index索引狀態
    • git add .新增全部檔案
    • git rm –cached filename : 後悔加入
  • status : 確認git狀態
  • commit -m : 記得提交到本機資料庫 & 一併註解
    • git commit –amend : 可修改 commit message
  • push : 推送commit到遠端資料庫
  • pull : 從遠端拉回新增的資料
  • log : 檢視提交的歷史記錄
  • init : 初始化git,創建local git儲存庫。
  • .gitignore
    • 可將不想放入版本控制的檔案加入,例如帳號密碼或系統設定檔
    • touch .gitignore
    • 將檔案名稱寫入 .gitignore
  • git diff
    • 可以看到在上次commit和這次commit之間修改過什麼檔案
  • git reset
    • commit 可是後悔了,可以使用 git reset 回到還沒 commit 之前
    • 指令:git reset HEAD^
    • 有三種模式可選:soft、mixed、hard,預設值是 mixed,下指令時可不必附註模式。看更多了解三種模式差異

(四) 你知道怎麼使用 branch 並送出 Pull Request?

  • git branch -d 先切換到master,刪除分支

出現error: The branch ‘cat’ is not fully merged.
If you are sure you want to delete it, run ‘git branch -D cat’.
改用 -D 參數就可以強制刪除

(五) 你熟悉 Git Workflow?

  • (a.) 本地上傳遠端
  1. mkdir 創新資料夾
  2. Git init 初始化這個目錄,讓 Git 對這個目錄開始進行版控
    工作目錄 Working Directory
  3. Git status 確認git狀態
  4. Git add file / git add .新增全部檔案到暫存區
  5. Git commit –m “xxxx” 提交 & commit note
  6. Git remote add origin (http)(SSH key)
  7. Git push –u origin master

-u 表示推送 & 一起設定upstream,預設去追蹤遠端
之後可以打git push

  • (b.) 遠端連結到本機
  1. Github new repository
  2. 本機cmd git clone
  • (c.) git clone 後,更新遠端資料庫內容後,才在local mkdir當工作目錄
  1. 本機沒有與遠端server同步更新,local cmd出現git push error

    1
    2
    3
    4
    $ git push
    To https://github.com/eddiekao/dummy-git.git
    ! [rejected] master -> master (fetch first)
    error: failed to push some refs to 'https://github.com/eddiekao/dummy-git.git'

    ☛ git pull –rebase origin master,先將remote拉回local
    ☛ git push -u origin master

  2. git clone 如何更新到本機的工作目錄 ?
    follow git workflow

如何建立 Github Pages

  • 靜態網站
    • 主分支 (每個人帳號只限制一個)
      • { 帳號 }.github.io/
    • 次分支 (沒有限制~主要在 gh-pages)
      • { 帳號 }.github.io/(自己設定的專案名稱)
  • 建站步驟
    (一) 上傳路徑 master
  1. Create a new repository

  2. 用cmd git init (把儲存庫建立起來,git 開始初始化本地儲存庫)

    • 注意的點在windows下系統預設會隱藏資料夾檔案,可點擊後找到隱藏項目進行勾選,就可以看到隱藏的git資料夾。
  3. …or push an existing repository from the command line

    • git remote add origin git@github.com:sammy/my-new-project.git

    • 出現 Git error - Fatal: remote origin already exist

    • Change remote URL to new GitHub repo:

  1. 按指令推上去,GitHub Pages 設置網頁

(二)上傳路徑 gh-pages

  1. git branch gh-pages 建立分支 gh-pages
  2. git checkout gh-pages 切換進入到分支 gh-pages
  3. git push origin gh-pages 把儲存庫推送到 gh-pages 分支上
  4. 網頁頁面渲染會等比較久
  5. git push origin gh-pages 推送更新檔案

狀況題

(一) git add .後,希望某個檔案不要加入追蹤

  • git reset HEAD 檔案名稱
    將這個檔案從暫存索引中移除,待會不會被Commit出去。

  • git reset HEAD
    將所有檔案從暫存索引中移除,取消進入追蹤。