# Git Rebasing
###### tags: `Git/GitHub`
## Comparision: `git rebase`, `git rebase -i`
> ### 1. `git rebase <branch>`: as an alternative to merging
> ### 2. `git rebase -i <previousCommit>`: as a cleanup tool
<br/>
---
## `git rebase <branch>`
>除了 merging之外的另一選擇
### `git merge` 在協同開發時的問題
<img src="https://i.imgur.com/UFa4yz2.png" width="400px">
:arrow_up: 同時有很多branch,想要 update 到最新的版本的話,我們的 commit 紀錄會有很多是 merge

:arrow_up: 情境A
<img src="https://i.imgur.com/QK0XSqS.png" width="300px">
:arrow_up: 情境B
### 實際使用 `git rebase <branch>` 遇到 conflict 的處理方式
#### resolve conflict =>`git add` => `git rebase --continue`
<!-- 遇到 conflict,需要解掉,git add 以後,再做 `git rebase --continue` -->
<br>
---
## `git rebase -i` : Interactive Rebasing
> ### as a commit cleanup tool
> #### REWRITE, DELETE, RENAME, or even REORDER commits
### Concepts

- 不是與其他 branch 作用,而是<u>基於 HEAD 所在位置</u>,操作目前所在的 branch 先前的 commits
- 使用 **`git rebase -i`** 會讓我們進入一個 interactive mode
- 我們需要指定從多遠以前的 commit 開始做進一步操作我的的 commit
<br>
### `git rebase -i` 在 interactive mode 的操作關鍵字
- **`pick`** : 使用這個 commit(沒有變動,開始 interactive mode 預設都是 pick)
- **`reword`** : 使用這個 commit,但是 編輯 commit 的資訊
- **`drop`** : 移除這個 commit
- **`squash`** : 使用這個 commit,並且與其他 commit 合併
- **`edit`** (沒用過,之後補)
- **`fixup`**(沒用過,之後補)

### 各種操作關鍵字使用時機
> 個人經驗分享
#### `drop` 使用時機
有某個 commit 不想要了,例如為了要做測試,暫時 hard coded 一些變數,方便後續開發,把這段 hard-coded 的東西單獨做成一個 commit ,可以直接拿掉
#### `squash` 使用時機
有一些暫存的 commit(例如午休或下班) ,並不是真的 code 開發的一個階段,可以合併掉
#### `reword` 使用時機
要發 PR 時候,發現自己的 commit 亂寫,或是有 typo
---
### GitLens 設定

In terminal
```
git config --global core.editor "code --wait"
```