# 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
----

----
```shell=
git commit
```
----

---
### branch
> 早點建立branch
> 經常建立bracnh
----

----

```shell=
git branch newImage
# newImage - branch name
```
----

----
```shell=
git commit
# 現在還是在master上
```

----
```shell=
git checkout newImage #切換到newImage了
git commit #現在在newImage上
```

----
補充
```shell=
git checkout -b bugFix #建立分支同時跳過去該分支
```

---
### merge
----

----

```shell=
git merge bugFix #現在我們在master上,所以是站在master上面去把bugFix "合併過來" 英文可以想成 merge from
```
----

```shell=
git checkout bugFix
git merge master
#猜猜看會發生什麼?
```
----

---
### Q:

----

----
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 (少使用)
----

> 優點:讓repository tree較為美觀,但專案使用以merge為主
----

----
```shell=
git rebase master #我們在bugFix上,把自己貼到master現在的點上面
```

----
```shell=
git checkout master
git rebase bugFix
```

> [C3]並沒有消失!
---
### about remote
----

---
### clone
----

----
```shell=
git clone repositoryURL
```

----
## 關於 remote 命名

---
### fetch (更新remote)
----

----

> 左邊的tree代表local | 右邊的tree代表remote端
```shell=
git fetch
```
----

> git fetch 通常是透過網路來跟 remote 溝通(透過一個 protocol (協定),例如 http:// 或者是 git://)
----
`fetch` 會下載同步所需的資料,但是不會更新任何的檔案
可以把 `git fetch` 想成是在下載資料
---
### pull
----

----

```shell=
git fetch
git merge o/master
```
----

同等於執行
```shell=
git pull
```
---
### push
----
與 pull 相反。git push 負責上傳你的 commit 到特定 remote 上面並且做出相對應的更新,只要做完了 git push,所有你的team member都可以從 remote 上面下載你所送出去的 commit。
你可以把 git push 當作是一個"發佈"你的工作進度的指令
----

```shell=
git push
```
----

----
`git push` 僅針對於現在的分支&remote的分支
若需要對其他branch push 除了checkout 到該分支以外 還有以下做法

---
### 關於 diverged & conflict
----

----
圖例

remote C2 -> 別的同事的push code
----
solution 1:
```shell=
git fetch
git rebase o/master
git push
#同等於
git pull --rebase
git push
```

----
solution 2:

----
solution 2:
```shell=
git fetch
git merge o/master
git push
#同等於
git pull
git push
```

---
### 實務範例
----
類型1

----

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
```
----

----
類型2

----
```shell=
git checkout master
git pull
git merge side1
git merge side2
git merge side3
git push
```
----

----
> gitlab github 使用MR (merge request) 較為常見
---
# :bulb: source tree
---

---
### 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}]"}