Skip to content

Commit 66d3c71

Browse files
committed
pr & github cli tutorial
1 parent 00ededf commit 66d3c71

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,14 @@ git remote set-url origin git@blue.github.com:blue-rubiks/t11.git
18471847

18481848
[Git-Flow 基本教學以及概念](https://github.com/twtrubiks/Git-Tutorials/tree/master/Git-Flow)
18491849

1850+
## PR (Pull Request) 教學
1851+
1852+
* [Youtube Tutorial - github PR (Pull Request) 教學(等待新增)]() - [文章快速連結](https://github.com/twtrubiks/Git-Tutorials/tree/master/pr-tutorial#github-pr-pull-request-%E6%95%99%E5%AD%B8)
1853+
1854+
* [Youtube Tutorial - github CLI PR 教學(等待新增)]() - [文章快速連結](https://github.com/twtrubiks/Git-Tutorials/tree/master/pr-tutorial#github-cli-pr-%E6%95%99%E5%AD%B8)
1855+
1856+
[PR (Pull Request) 教學](https://github.com/twtrubiks/Git-Tutorials/tree/master/pr-tutorial)
1857+
18501858
## Linux 注意事項
18511859

18521860
* [Youtube Tutorial - Linux 教學 - git 乎略 file mode (chmod) 改變](https://youtu.be/QCh2k903Yak)

pr-tutorial/README.md

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
# PR (Pull Request) 教學
2+
3+
先來說說甚麼時候可以使用 PR (Pull Request),
4+
5+
* 情境一
6+
7+
你在網路上看到一個很棒的專案, 然後你很想要為這個專案盡一份心力, 這時候就你就可以透過 PR 這個機制,
8+
9+
通常你不會有這個專案的權限, 所以一定是 Fork 這個專案到自己的 repo 下修改 -> 修改完後推回自己的分支
10+
11+
-> 再對對應的原專案發 PR(注意對方的以及自己的 branch ) -> 等作者測試覺得沒問題後合併進主分支
12+
13+
-> 恭喜你對社群盡了一分心力:smile:
14+
15+
* 情境二
16+
17+
公司也可以使用 PR 的模式進行多人開發, 但通常在這個情況下你都會有專案的權限(和情境一不同), 所以你就
18+
19+
不需要 Fork, 直接開 branch 後對特定的 branch(可能是 develop) 發 PR 即可.
20+
21+
22+
**情境一和情境二基本上都是一樣的概念, 唯一差別就是是否有 repo 的權限, 如果沒有就需要 Fork.**
23+
24+
今天主要會透過 情境二 和大家簡單介紹 PR 的操作.
25+
26+
* [Youtube Tutorial - github PR (Pull Request) 教學(等待新增)]() - [文章快速連結](https://github.com/twtrubiks/Git-Tutorials/tree/master/pr-tutorial#github-pr-pull-request-%E6%95%99%E5%AD%B8)
27+
28+
* [Youtube Tutorial - github CLI PR 教學(等待新增)]() - [文章快速連結](https://github.com/twtrubiks/Git-Tutorials/tree/master/pr-tutorial#github-cli-pr-%E6%95%99%E5%AD%B8)
29+
30+
## github PR (Pull Request) 教學
31+
32+
使用 [Git-Flow 基本教學](https://github.com/twtrubiks/Git-Tutorials/tree/master/Git-Flow) 的概念來做教學,
33+
34+
為了簡單一點, 在這邊就只有 main(master), develop, feature 分支.
35+
36+
今天有個需求來了, 我首先先從 develop 開一個 feature 分支,
37+
38+
假設做完了需求, 就把 feature 分支 push 上去, 然後開始發 PR.
39+
40+
現在要對 develop 發 PR, 要發的分支是 feature. (如下圖)
41+
42+
![alt tag](https://i.imgur.com/XfTq0hc.png)
43+
44+
選好之後就可以按下 Create pull request,
45+
46+
接著你會看到專案下有一個 PR,
47+
48+
![alt tag](https://i.imgur.com/ad8BM6T.png)
49+
50+
這時候可能是技術主管(依照各公司的流程)看到這個 PR, 如果沒有甚麼問題,
51+
52+
可以選擇要使用哪種的 Merge pull request 的方式.
53+
54+
![alt tag](https://i.imgur.com/JX9pQDU.png)
55+
56+
有三種 Merge pull request 可以選擇,
57+
58+
* Create a Merge Commit
59+
60+
基本上這個就是一般的 merge, 但我通常不喜歡這個, 因為會有無意義的 merge commit.
61+
62+
* Squash and Merge
63+
64+
這個就是假如你有一個功能, 總共使用了 10 個 commit 才完成這個功能, 但是這些 commit
65+
66+
其實都沒有太重要的資訊, 這時候你可以選擇使用 Squash 的方式, 把這 10 個 commit 合併成
67+
68+
一個 commit. (其他的 commit 會變成你的 message).
69+
70+
* Rebase and Merge
71+
72+
這個和 Create a Merge Commit 的差別就是他不會有無意義的 merge commit, 他有移花接木
73+
74+
的概念, 你的分支上有3個 commit, 使用這方法合併後就是會多出 3個 commit.
75+
76+
至於要使用哪一種, 就看各公司的需求了:smile:
77+
78+
決定好 Merge pull request 的方式後就可以直接按下了, 你會發現他顯示 Merged,
79+
80+
並且也可選擇是否刪除該分支.
81+
82+
![alt tag](https://i.imgur.com/ZuJ2eh1.png)
83+
84+
基本上整個流程就是這樣, develop 向 main 發送 PR 也是一樣的概念.
85+
86+
## github CLI PR 教學
87+
88+
如果你有看前面的文章, 你就會覺得很麻煩:expressionless:
89+
90+
因為每次都要手動去網頁上點, 工程師肯定要使用 CLI,
91+
92+
今天我就來教大家這個 [github CLI](https://cli.github.com/):laughing:
93+
94+
連 github 的網頁都不需要開:satisfied:
95+
96+
首先是安裝方法
97+
98+
[https://github.com/cli/cli/blob/trunk/docs/install_linux.md](https://github.com/cli/cli/blob/trunk/docs/install_linux.md)
99+
100+
接著是登入的方法 [gh_auth_login](https://cli.github.com/manual/gh_auth_login)
101+
102+
```cmd
103+
gh auth login
104+
```
105+
106+
選擇你要的登入方式即可.
107+
108+
順便一提, github CLI 的文件寫的很好, 大家可以參考他的文件玩玩看 [GitHub CLI document](https://cli.github.com/manual/).
109+
110+
接下來, 就要來體驗 github CLI 的強大了:satisfied:
111+
112+
現在有一個 `feature_cli` 分支, 當 push 分支之後,
113+
114+
(溫馨提醒, 要發一個 PR, 這個 branch 一定要 push 到 remote :exclamation:)
115+
116+
(也就是說, 你沒辦法去發送一個 PR 只存在 local 端但卻不存在 remote 端)
117+
118+
(如果你只想要 PR 特定的 commit, 請使用 `git checkout -b [branch] [commit_id]` 再發送 PR)
119+
120+
就可以透過 github CLI 發 PR, 要向 develop 分支發 PR, 指令如下
121+
122+
文件可參考 [https://cli.github.com/manual/gh_pr_create](https://cli.github.com/manual/gh_pr_create)
123+
124+
```cmd
125+
gh pr create --base develop --head feature_cli
126+
```
127+
128+
如果你目前就在 `feature_cli` 分支底下, 也可以只輸入
129+
130+
```cmd
131+
gh pr create --base develop
132+
```
133+
134+
成功透過 github CLI 發送 PR
135+
136+
![alt tag](https://i.imgur.com/sZF3SuH.png)
137+
138+
github 上面也確實有這個 PR
139+
140+
![alt tag](https://i.imgur.com/7atBIzY.png)
141+
142+
除了發 PR 之外, 也可以透過 github CLI 接受 PR, 指令如下
143+
144+
文件可參考 [https://cli.github.com/manual/gh_pr_merge](https://cli.github.com/manual/gh_pr_merge)
145+
146+
```cmd
147+
gh pr merge -s 2
148+
```
149+
150+
參數說明,
151+
152+
`-m` Create a Merge Commit.
153+
154+
`-s` Squash and Merge.
155+
156+
`-r` Rebase and Merge.
157+
158+
數字 2 則代表要合併的 PR 編號.
159+
160+
![alt tag](https://i.imgur.com/hedvtIi.png)
161+
162+
github 上面確實已經合併這個 PR
163+
164+
![alt tag](https://i.imgur.com/h6akTEd.png)
165+
166+
還有非常多的指令, 就不一一介紹給大家了, 大家請自行研究:relaxed:
167+
168+
基本上, github CLI 功能是非常強大的:smile:
169+
170+
### 其他
171+
172+
gh 預設的 editor 是 nano, 以下指令為修改成 vim
173+
174+
```cmd
175+
gh config set editor vim
176+
```

0 commit comments

Comments
 (0)