Git 小劇場:如何合併開發分支到主分支

How to Merge Dev Branch into Master


最近逐漸在自己開發的 side project 也用上 git ,把 .bak 的習慣改掉

  • 在 dev branch 上開發, rebase 回 master 所在的 commit
  • (在 public repo 上 repo 要用 rebase 的話要注意跟別人 code 的 conflict)
  • checkout 回 master,merge
  • push master to remote repo
  • 刪除 local dev branch

memo

1
2
3
4
5
6
7
8
(on dev branch)
$ git lg
* 13a704f (HEAD, dev) plaintext echo bot prototype done
* cb876e8 (master) initail commit
(on dev branch)
$ git rebase master
Current branch dev is up to date.

這篇範例是比較簡單的狀況,dev 的根本來就是 master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(on dev branch)
$ git checkout master
Switched to branch 'master'
(on master branch)
$ git lg
* 13a704f (dev) plaintext echo bot prototype done
* cb876e8 (HEAD, master) initail commit
(on master branch)
$ git merge dev
Updating cb876e8..13a704f
Fast-forward
(on master branch)
$ git lg
* 13a704f (HEAD, master, dev) plaintext echo bot prototype done
* cb876e8 initail commit
(on master branch)
$ git branch
dev
* master
(on master branch)
$ git branch -d dev
Deleted branch dev (was 13a704f).
(on master branch)
$ git branch
* master

注意的點

  • $ git merge or $ git rebase
    • public: merge
      • 理由為保留紀錄
    • private or not affect other: rebase
      • 理由為線性、乾淨
  • $ git merge or $ git merge --no-ff

Ref