Swift Notes #4 — Learning Git Branching
這次紀錄的目的,主要是看到Jason同學有分享一個如何使用Git的學習網站,我覺得蠻不錯的,裡面有中文字幕,以及可以透過視覺化的方式,來認識如何使用Git 做Version Control ; 因為在Paul大大的這部影片How to become an iOS developer in 2021 建議當中,有提到Git對於在developer工作中的重要性,以及來自彼得潘課程已經找到工作的同學回饋,建議工作初期,建議要把Git學好。
學習資源:
- Learn Git Branching:
- 六角學院題庫參考:
這是一開始的學習畫面,初始頁面會有學習road map,可以知道大概要學習哪些內容,目前是做到 調整提交順序(Moving Work Around),之後會把整個主要(Main)都補上。
- 語言切換:
1. Git commit (提交)
輸入兩次git commit
,就過關。
- Command Line 指令
git commit // First
git commit // Second
2. Git branch (分支)
先建立一個git branch
,叫bugFix
,並且用git checkout
切換到bugFix
。
- Command Line 指令
git branch bugFix
git checkout bugFix
3. Git merge (合併)
git merge
是將兩個不同的branch
(分支)合併在一起,然後產生新的main branch
。
- Command Line 指令:
git branch bugFix
git checkout bugFix
git commit // commit新的branch叫bugFix的branch,建立C2
git checkout main // 用checkout切換到main branch
git commit // commit main branch,這時建立C3,記得這時候main在C3
git merge C2 // 輸入git merge C2,把C2的狀態移到main branch(C3),並產生C4
4. Git rebase (重新定義branch基準)
git rebase
不管是新增、修改、刪除 Commit
都相當方便,用來整理、編輯還沒有推出去的 Commit
相當方便,但通常也只適用於尚未推出去的 Commit
。
- Command Line 指令:
git branch bugFix // 建立bugFix branch
git checkout bugFix // 用checkout轉換到bugFix
git commit // 提交bugFix,建立C2
git checkout main // 用checkout切換到main branch
git commit // 提交main branch,建立C3
git checkout bugFix // 用checkout轉換到bugFix
git rebase C3 // 用rebase 將C2的bugFix,移到C2'
5. Git checkout 移動 HEAD 指標
這個練習主要是練習git
的前後移動,會用到git checkout
,主要是利用HEAD
為了找到版本的不同,並且知道不同版本的狀態。
其實主要目的是要將HEAD
移到bugFix
,就是把main
的內容移到bugFix
,因為HEAD
一直藏在main
裡面,所以只要將藏在main
裡面的HEAD
,用git checkout
移到bugFix
就行。
可參考下方程式:
- Command Line 指令:
git checkout C4
- Reference:
6. 相對引用1 ( ^)
- Command Line 指令:
git checkout C4^
- Reference:
7. 相對引用2 ( -~)
這次就是要把^,更改成~,並且可以按照你要到的位置,輸入數字就可以了
- Command Line 指令:
git branch -f main C6 // 用git branch main移到C6
git checkout HEAD~1 // 用git checkout把HEAD移到起點
git branch -f bugFix HEAD~1 // 用git branch 找到bugFix並用HEAD~1把bugFix移到C0
8. Git revert(反轉) & Git reset (復原)
- Command Line 指令:
git reset local^ // 用git reset 返回從C3移到main
git checkout pushed // 用git checkout移到pushed
git revert C2 // 用git revert 提交C2並產生C2'
- Reference:
9. Cherry-Pick (1)
git cherry-pick
顧名思義就是只 撿取 想要的東西(在git flow中就是commit),放到目前分支上。
輸入git cherry-pick
C3, C4, C7提交到 main branch
。
- Command Line 指令:
git cherry-pick C3 C4 C7
- Reference:
10. Git interactive rebase (2)
就是用git rebase -i
輸入你的目的地,並且選取要的commit
,就可以產生新的main branch
。
- Command Line 指令:
git rebase -i C1
11. 在 local 的堆疊的 commit
- Command Line 指令:
git rebase bugFix main
git rebase -i C1
12. commit 的戲法(1)
amend
的英文叫修改,那git commit --amend
是一個 Git 命令,用於修改最後一次提交。
這個命令可以讓你更改上一次commit
,或者將新的commit
加入到上一次的提交中,而不需要新建一個新的commit
。
git rebase -i HEAD~2
:
git rebase -i
是進行互動式變基的指令,允許你對過去的一系列提交進行修改。
HEAD~2
表示從當前的branch
最新提交回溯到第二個commit
,意味著這條指令將最後兩次的commit
納入操作的範圍內。
在互動模式下,你會進入一個文字編輯器介面,列出了這兩次commit
,你可以選擇重新排序、修改commit
、合併提交等操作。
2. git commit --amend
:
這個指令用於修改最後一次commit
。你可以更改commit
或將新的變更加入到最後一次提交中。這不會創建一個新的commit
,而是替換掉最後一次的commit
。
3.git rebase -i C1
:
這裡的 C1
應該是一個commit
的hash
的示例。使用這條指令時,你會將從 C1
以後的所有提交納入互動式變基的操作範圍。
這允許你修改從某一特定commit
開始的所有commit
記錄。
4. git rebase caption main
(你可能是指 git rebase --onto caption main
):
這個指令的作用是將當前branch
上從 main branch
之後的commit
,變到 caption branch
上。
目的是為了保持歷史線性,或者將一系列的改動應用到另一個branch
上。
- Command Line 指令:
git rebase -i HEAD~2
git commit --amend
git rebase -i C1
git rebase caption main
13. commit 的戲法(2)
- Command Line 指令:
git checkout main
git cherry-pick C2
git commit --amend
git cherry-pick C3
- Reference:
14. git tag (在Commit 上留下紀錄)
- 基本上
git tag
會跟著HEAD
走,所以如果要做git tag
,必須先用git checkou
t切換
- 先用
git checkout
切換到C1 - 標註
git tag v0
- 再用
git checkout
切換到C2 - 再標註
git tag v1
- Command Line 指令:
git checkout C1
git tag v0
git checkout C2
git tag v1
- Reference:
Git - 標籤
跟大多數的版本管理系統一樣,Git 有能力對專案歷史中比較特別的時間點貼標籤,來表示其重要性。 通常大家都會用這個功能來標出發行版本,如 v1.0 ...等等。…
git-scm.com
15. git describe(顯示commit的位置)
<ref>
是任何一個可以被 git 解讀成 commit 的位置,如果你沒有指定的話,git
會以你目前所在的位置為準(HEAD
)。
<tag>
表示的是離 <ref>
最近的 tag, numCommits
是表示這個 tag 離 <ref>
有多少個 commit, <hash>
表示的是你所給定的 <ref>
所表示的 commit 的前七個 id。(有點饒舌,但先記著就是)
- Command Line 指令:
git describe main
v0_2_gC2 // 這時候會產生版本
git commit // 輸入完這個遊戲就結束了
16. N個rebase
我原本是用17步一個一個切換走完,但後來看完解答之後,才發現原來可以直接用天龍4步就可以走完,看來當下我不是很了解如何快速地使用rebase做切換,不過透過嘗試不停的錯誤,就知道可以怎麼進步!!!
- Command Line 指令:
git rebase main bugFix
git rebase bugFix side
git rebase side another
git rebase another main
17. 多個parent commit
- 教學1:
- 教學2:
我原本寫的版本:
git checkout C2
git branch bugWork
git checkout main
- 正確Command Lin指令:
我翻譯一下,先用git branch
寫bugWork做為註記,用main^^2^
回到parent commit(父提交)
再回到上一階的parent commit(父提交)
,再回到上兩個commit
,這樣的寫法就是比較簡潔,可以一次到位。
git branch bugWork main^^2^
18. Branch Spaghetti (Branch醬糊)
- Command Lin指令:
git checkout one
git cherry-pick C4 C3 C2
git checkout two
git cherry-pick C5 C4 C3 C2
git branch -f three C2
19. Git Clone
第一,remote 是用來備份的!
local的 git 有能力可以回復文件到前一個狀態 (你知道的),但是全部的資訊還是儲存在本地端。
如果你在其它的電腦上面有你的 git repository
的副本,則你可以在資料不小心遺失的時候進行救援備份。
git clone
表示你想要把遠端的 repository
複製一份下來放在本地端( 例如從 github 複製)。
雖然 git clone
實際上是把遠端的 repository
複製下來放在本地端,在 Learn Git Branching 中,我們用的這個指令會有一點不同。雖然他跟真實的指令的意思相反,但是它可以建立起本地端以及遠端的一個連結,現在讓我們看看如何使用它吧。
輸入git clone
,即完成。
- Command Lin指令:
git clone
20. Git remote clone
什麼是 o/
?
remote branch
也(需要) 一個命名法則,通常是表示一般的 remote
branch
格式。
- Command Lin指令:
git commit
git checkout o/main
git commit
21. Git Fetch
git fetch
這個指令是用來remote repository
用fetch (抓取)資料。
git fetch
基本上同步了我們的 local repository 以及 remote repository 的最新狀態。
git fetch
通常是透過網路來跟 remote 溝通(透過一個 protocol (協定),例如 http://
或者是 git://
)。
git fetch
並不會影響到在 local repository 中的 main branch
,他並不會將main branch
更新到最新的狀態。
因此,你可以把 git fetch
想成是在下載資料。
- Command Lin指令:
git fetch //資料同步
22. Git pull
git fetch
就是從遠端抓取commit
。
- Command Lin指令:
git fetch
git pull
23. 模擬合作
了解模仿團隊合作的運作模式,並且先用git clone
追蹤版本,再用git pull
將團隊的版本更新上去。
- Command Lin指令:
git clone // local branch "main" set to track remote branch "o/main"
git fakeTeamwork 2 // 用fakeTeamwork送兩次commit上去
git commit // 因為已經local branch已經有被追蹤,所以先拉一次commit,然後推上去
git pull // 用pull再拉一次,直接接到fakeTeamwork的C5
24. Git Push (推送)
git push
負責上傳 你的 commit 到特定 remote 上面並且做出相對應的更新,只要做完了 git push
,所有你的朋友都可以從 remote 上面下載你所送出去的 commit。
- Command Lin指令:
git commit
git commit
git push
25. Diverged Work (分歧工作)
- Command Lin指令:
git clone // 先取得相同的respository
git commit // 用git commit提交一次
git commit // 用git commit提交一次
git rebase -i C2 // 用git rebase -i C2將內容重新排序提交
git push // 用git push推送讓資料同步
26. 遠端伺服器拒絕!(Remote Rejected!)
-b
參數表示如果指定的branch
不存在,則新增一個新branch
。- Command Lin指令:
git checkout -b feature C2 // 創建一個為 feature 的new branch,並且將這個branch的起點設定在提交 C2 上。如果沒有提供 C2,則基於當前branch創建
git push origin feature // 將local的 feature 分支推送到remote repository (通常是 GitHub 或 GitLab),並且在remote repository中也創建一個名為 feature 的branch,其內容與本地的 feature branch相同。
#Git #VersionControl #LearnGitBranching