# Git Tutorial!
<!-- Put the link to this slide here so people can follow -->
slide: https://hackmd.io/@36DeTXRjTYmj7xDCsy8agg/SyUINEjJI
---
Distributed Version Control System
---
## Why a Version Control System like Git is needed
- Real life projects generally have multiple developers working in parallel. So a version control system like Git is needed to ensure there are no code conflicts between the developers.
- projects which are being run in parallel involve the same codebase
---
### More than 70% of developers use Git. Developers :heart: Git.
---
### commit
----
![](https://i.imgur.com/FFRI36e.png)
----
```shell=
git commit
```
----
![](https://i.imgur.com/w5y4GUr.png)
---
### branch
> 早點建立branch
> 經常建立bracnh
----
![](https://i.imgur.com/zgcng5m.png)
----
![](https://i.imgur.com/6se1BlD.png)
```shell=
git branch newImage
# newImage - branch name
```
----
![](https://i.imgur.com/1zwG7GF.png)
----
```shell=
git commit
# 現在還是在master上
```
![](https://i.imgur.com/Qs12yFM.png)
----
```shell=
git checkout newImage #切換到newImage了
git commit #現在在newImage上
```
![](https://i.imgur.com/wg9uflc.png)
----
補充
```shell=
git checkout -b bugFix #建立分支同時跳過去該分支
```
![](https://i.imgur.com/mrK0YAH.png)
---
### merge
----
![](https://i.imgur.com/3hbEZjY.png)
----
![](https://i.imgur.com/f5SgOix.png)
```shell=
git merge bugFix #現在我們在master上,所以是站在master上面去把bugFix "合併過來" 英文可以想成 merge from
```
----
![](https://i.imgur.com/OsBNtSa.png)
```shell=
git checkout bugFix
git merge master
#猜猜看會發生什麼?
```
----
![](https://i.imgur.com/ySeWUfg.png)
---
### Q:
![](https://i.imgur.com/nSe1MOB.png)
----
![](https://i.imgur.com/KNZZaux.png)
----
wait for 3 min.
----
Answer
```shell=
git checkout -b bugFix # or git branch bugFix; git checkout bugFix
git commit #現在我們在bugFix上 [C2]
git checkout master #回到master [C1]
git commit #[C3]
git merge bugFix #merge from bugFix
```
Q: 請問bugFix有C3的內容嗎?
---
### rebase (少使用)
----
![](https://i.imgur.com/UTgW5wV.png)
> 優點:讓repository tree較為美觀,但專案使用以merge為主
----
![](https://i.imgur.com/3ooMtLk.png)
----
```shell=
git rebase master #我們在bugFix上,把自己貼到master現在的點上面
```
![](https://i.imgur.com/xyXKIoe.png)
----
```shell=
git checkout master
git rebase bugFix
```
![](https://i.imgur.com/9AJl2UI.png)
> [C3]並沒有消失!
---
### about remote
----
![](https://i.imgur.com/oaWjNkZ.png)
---
### clone
----
![](https://i.imgur.com/sjxPERV.png)
----
```shell=
git clone repositoryURL
```
![](https://i.imgur.com/gJdlKLk.png)
----
## 關於 remote 命名
![](https://i.imgur.com/hGV2zY6.png)
---
### fetch (更新remote)
----
![](https://i.imgur.com/Nb237aD.png)
----
![](https://i.imgur.com/KS1spr6.png)
> 左邊的tree代表local | 右邊的tree代表remote端
```shell=
git fetch
```
----
![](https://i.imgur.com/0DjUXBW.png)
> git fetch 通常是透過網路來跟 remote 溝通(透過一個 protocol (協定),例如 http:// 或者是 git://)
----
`fetch` 會下載同步所需的資料,但是不會更新任何的檔案
可以把 `git fetch` 想成是在下載資料
---
### pull
----
![](https://i.imgur.com/J9TGTXX.png)
----
![](https://i.imgur.com/t3hnDiS.png)
```shell=
git fetch
git merge o/master
```
----
![](https://i.imgur.com/KfYDB0d.png)
同等於執行
```shell=
git pull
```
---
### push
----
與 pull 相反。git push 負責上傳你的 commit 到特定 remote 上面並且做出相對應的更新,只要做完了 git push,所有你的team member都可以從 remote 上面下載你所送出去的 commit。
你可以把 git push 當作是一個"發佈"你的工作進度的指令
----
![](https://i.imgur.com/sRe5mK6.png)
```shell=
git push
```
----
![](https://i.imgur.com/xAwbR1o.png)
----
`git push` 僅針對於現在的分支&remote的分支
若需要對其他branch push 除了checkout 到該分支以外 還有以下做法
![](https://i.imgur.com/TzXgEO0.png)
---
### 關於 diverged & conflict
----
![](https://i.imgur.com/K3T9X5E.png)
----
圖例
![](https://i.imgur.com/HrEOYmu.png)
remote C2 -> 別的同事的push code
----
solution 1:
```shell=
git fetch
git rebase o/master
git push
#同等於
git pull --rebase
git push
```
![](https://i.imgur.com/7S5D3F2.png)
----
solution 2:
![](https://i.imgur.com/hAFbsgm.png)
----
solution 2:
```shell=
git fetch
git merge o/master
git push
#同等於
git pull
git push
```
![](https://i.imgur.com/UpzGLeu.png)
---
### 實務範例
----
類型1
![](https://i.imgur.com/e3jMoQv.png)
----
![](https://i.imgur.com/SEtGrNR.png)
branch side1 / side2 / side3 都想要推code到 remote master,且remote master已經領先一個commit [C8]
----
```shell=
git checkout master
git pull
git rebase master side1
git rebase side1 side2
git rebase side2 side3
git rebase side3 master
git push
```
----
![](https://i.imgur.com/DFmpGGP.png)
----
類型2
![](https://i.imgur.com/jcmx8Kn.png)
----
```shell=
git checkout master
git pull
git merge side1
git merge side2
git merge side3
git push
```
----
![](https://i.imgur.com/ouhOlWD.png)
----
> gitlab github 使用MR (merge request) 較為常見
---
# :bulb: source tree
---
![](https://i.imgur.com/mUbt6Me.png)
---
### Thank you! :sheep:
By Kaya
- source: self & [gitplayground](https://learngitbranching.js.org/)
{"metaMigratedAt":"2023-06-15T03:02:56.847Z","metaMigratedFrom":"YAML","title":"Talk slides template","breaks":"true","description":"View the slide with \"Slide Mode\".","contributors":"[{\"id\":\"dfa0de4d-7463-4d89-a3ef-10c2b32f1a82\",\"add\":7425,\"del\":2421}]"}