# Git筆記 ![](https://hackmd.io/_uploads/Byn6DTTU3.png) ![](https://hackmd.io/_uploads/BJ8xOTaIh.png) * Windows 環境:教室電腦 教學:[Git 安裝教學](https://git-scm.com/book/zh-tw/v2/%E9%96%8B%E5%A7%8B-Git-%E5%AE%89%E8%A3%9D%E6%95%99%E5%AD%B8) Linux 環境:VirtualBox - Ubuntu server 20.04 教學:[Git 安裝教學](https://git-scm.com/book/zh-tw/v2/%E9%96%8B%E5%A7%8B-Git-%E5%AE%89%E8%A3%9D%E6%95%99%E5%AD%B8) (Optional) 3rd party server Host:node1.puyuma.org username: ntc password: ntcuser2222 $ mkdir <nickname> $ cd <nickname> 初始化一個新的 Git 儲存庫 git init 將檔案加入到暫存區(Stage) git add <file> 請將 <file> 替換為要加入的檔案名稱,或使用 . 來加入所有檔案 提交檔案到儲存庫並添加提交訊息 git commit -m "commit message" 將 "commit message" 替換為你的提交訊息,描述這個提交的內容 檢查儲存庫的狀態 git status 查看提交記錄 git log ![](https://hackmd.io/_uploads/rJjzja6Ih.png) ![](https://hackmd.io/_uploads/SyzR96pL2.png) ## Git 時光機 ![](https://hackmd.io/_uploads/ryHKsaTI3.png) ## reflog ![](https://hackmd.io/_uploads/BytLn66L3.png) ## Remote Repository * 不同於本地的 Respoitory * 還有一個在遠端的 * 適合多人協作的 * 你可以在遠端開一個 Repository * 讓大家把程式碼抓下來一起合作 * 實驗場地 * https://github.com/ * [創建Github帳號及使用官方教學](https://docs.github.com/en/get-started/quickstart/create-a-repo) 克隆遠端儲存庫到本地 **git clone** <remote_repository_url> 請將 <remote_repository_url> 替換為遠端儲存庫的 URL 推送本地變更到遠端儲存庫 **git push** <remote_name> <branch_name> 請將 <remote_name> 替換為遠端儲存庫的名稱,通常是 origin 請將 <branch_name> 替換為要推送的分支名稱 從遠端儲存庫拉取最新的變更到本地 **git pull** <remote_name> <branch_name> 請將 <remote_name> 替換為遠端儲存庫的名稱,通常是 origin 請將 <branch_name> 替換為要拉取的分支名稱 ## 範例 create some new file git add file.xxx git commit -m “xxxx” git push https://ghp_XI0NIP6FjqI38PO77Oq89nxrs8JI0H1vUUiQ@github.com/你們的帳號名稱/xxx 註記:在 push 之前,你需要申請一組 key[教學](https://hackmd.io/@yillkid/ry_FvBmWY) ## [如何在 github 提交我的程式碼](https://hackmd.io/57zQytovQ4a7ivdQUv1_ZQ) git branch 是用來顯示本地存儲庫中的分支列表的命令。 git branch git branch beta 這個命令創建了一個名為 beta 的新分支。如果你再次執行 git branch,你將看到新增的 beta 分支。 git checkout beta 這個命令用於切換到指定的分支。 touch beta.txt git add beta.txt git commit -m "Add beta.txt" git push ## Branch範例 yillkid@Orz:/tmp$ git clone https://github.com/darren0607/001 正複製到 '001'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 接收物件中: 100% (3/3), 完成. yillkid@Orz:/tmp$ cd 001/ yillkid@Orz:/tmp/001$ git branch * master yillkid@Orz:/tmp/001$ git branch beta yillkid@Orz:/tmp/001$ git branch beta * master yillkid@Orz:/tmp/001$ git checkout beta 切換到分支 'beta' yillkid@Orz:/tmp/001$ git branch * beta master yillkid@Orz:/tmp/001$ touch beta.txt yillkid@Orz:/tmp/001$ git add beta.txt ; git commit -m "file: beta.txt" ; git push [beta 8389666] file: beta.txt 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 beta.txt fatal: 目前分支 beta 沒有對應的上游分支。 為推送目前分支並建立與遠端上游的追蹤,使用 git push --set-upstream origin beta yillkid@Orz:/tmp/001$ git push --set-upstream origin beta ## fetch範例 b: yillkid@Orz:/tmp/ntc-git$ git branch bbb d kkk * main yillkid@Orz:/tmp/ntc-git$ git branch B yillkid@Orz:/tmp/ntc-git$ git checkout B 切換到分支 'B' yillkid@Orz:/tmp/ntc-git$ touch B.txt ; git add B.txt ; git commit -m "B" ; git push [B d4359df] B 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 B.txt fatal: 目前分支 B 沒有對應的上游分支。 為推送目前分支並建立與遠端上游的追蹤,使用 git push --set-upstream origin B yillkid@Orz:/tmp/ntc-git$ git push --set-upstream origin B 枚舉物件: 3, 完成. 物件計數中: 100% (3/3), 完成. 使用 8 個執行緒進行壓縮 壓縮物件中: 100% (2/2), 完成. 寫入物件中: 100% (2/2), 225 位元組 | 225.00 KiB/s, 完成. 總共 2 (差異 1),復用 0 (差異 0),重用包 0 remote: Resolving deltas: 100% (1/1), completed with 1 local object. remote: remote: Create a pull request for 'B' on GitHub by visiting: remote: https://github.com/yillkid/ntc-git/pull/new/B remote: To https://github.com/yillkid/ntc-git * [new branch] B -> B 分支 'B' 設定為追蹤來自 'origin' 的遠端分支 'B'。 yillkid@Orz:/tmp/ntc-git$ git branch -a * B bbb d kkk main remotes/origin/B remotes/origin/HEAD -> origin/main remotes/origin/a remotes/origin/bbb remotes/origin/c remotes/origin/d remotes/origin/k remotes/origin/kkk remotes/origin/main remotes/origin/new remotes/origin/new2 remotes/origin/test yillkid@Orz:/tmp/ntc-git$ a: yillkid@Orz:~/workspace/ntc-git$ git branch a b c * main remotes/origin/bbb test yillkid@Orz:~/workspace/ntc-git$ git branch -a a b c * main remotes/origin/bbb test remotes/origin/a remotes/origin/bbb remotes/origin/c remotes/origin/d remotes/origin/k remotes/origin/kkk remotes/origin/main remotes/origin/new remotes/origin/new2 remotes/origin/test yillkid@Orz:~/workspace/ntc-git$ git fetch remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Compressing objects: 100% (1/1), done. remote: Total 2 (delta 1), reused 2 (delta 1), pack-reused 0 展開物件中: 100% (2/2), 205 位元組 | 205.00 KiB/s, 完成. 來自 https://github.com/yillkid/ntc-git * [新分支] B -> origin/B yillkid@Orz:~/workspace/ntc-git$ git branch -a a b c * main remotes/origin/bbb test remotes/origin/B remotes/origin/a remotes/origin/bbb remotes/origin/c remotes/origin/d remotes/origin/k remotes/origin/kkk remotes/origin/main remotes/origin/new remotes/origin/new2 remotes/origin/test yillkid@Orz:~/workspace/ntc-git$ ls c d file0 file1 kkk.txt README.md yillkid@Orz:~/workspace/ntc-git$ git merge remotes/origin/B Merge made by the 'ort' strategy. B.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 B.txt yillkid@Orz:~/workspace/ntc-git$ ls B.txt c d file0 file1 kkk.txt README.md yillkid@Orz:~/workspace/ntc-git$ B.txt git merge 用於將一個分支的變更合併到另一個分支。它將當前分支的變更與指定的目標分支合併,創建一個新的提交,包含兩個分支的變更。 git checkout main git merge feature 這將將 feature 分支的變更合併到 main 分支。 git fetch origin //這將從遠端存儲庫 origin 獲取最新的變更。 ## amend練習 請注意,使用 git push -f 命令強制推送(force push)是一個具有風險的操作,因為它會覆蓋遠端儲存庫的歷史記錄。這樣的操作應該謹慎使用,並且只應該在你確定自己的操作不會對他人或合作夥伴產生負面影響的情況下才使用。 在你的本地儲存庫目錄中,運行以下命令將當前的變更加入暫存區: git add . 執行以下命令來修改最新的提交訊息: git commit --amend -m "xxx" 最後,使用以下命令強制推送到遠端儲存庫: git push -f ## amend練習 git add . # 將變更加入暫存區 git commit -m "xxx" git commit --amend -m "xxx" # 修改最新的提交訊息 git push git log git revert <SHA-1> //<SHA-1>為log的編碼 git push ## github 架設自己的靜態網站 ![](https://hackmd.io/_uploads/BkOv-7Jwh.png) ![](https://hackmd.io/_uploads/B1KObm1wn.png) ![](https://hackmd.io/_uploads/SytY-myP2.png)