使用GitRebase合并Commit和解决分支冲突

Rebase 场景一:合并多次提交纪录

创建文件并分别提交

1
2
3
4
5
6
7
8
9
10
11
$ touch demo.txt
$ git add demo.txt
$ git commit -m "add demo.txt"

$ touch function.txt
$ git add function.txt
$ git commit -m "add function.txt"

$ touch class.txt
$ git add class.txt
$ git commit -m "add class.txt"

查看Git提交记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ git log
commit 1c643cac08aa8cc1daf7d4c71dc46fd46a70b722 (HEAD -> master)
Author: Janko <shuaijinchao@gmail.com>
Date: Sat Aug 1 12:43:49 2020 +0800

add class.txt

commit 187ee149a35f3eca37dfebd42c0f46fe0a37eaaf
Author: Janko <shuaijinchao@gmail.com>
Date: Sat Aug 1 12:42:55 2020 +0800

add function.txt

commit 2bf1c7673ada887dfb46e7bed1a982b5ff2c533a
Author: Janko <shuaijinchao@gmail.com>
Date: Sat Aug 1 12:41:36 2020 +0800

add demo.txt

合并最近三次提交记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ git rebase -i HEAD~3
pick 2bf1c76 add demo.txt
pick 187ee14 add function.txt
pick 1c643ca add class.txt

# Rebase 648f34f..1c643ca onto 648f34f (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

此时会弹出交互界面,我们可以看到交互界面最上方有我们最近的三次提交记录,我们看到每个 commit 前都有一个 pick 标识,如果使用第一次的 2bf1c76 add demo.txt 这次提交记录作为最终的提交记录,只需要把后两次 commit 记录前的 pick 改为 squash 即可。

1
2
3
pick 2bf1c76 add demo.txt
squash 187ee14 add function.txt
squash 1c643ca add class.txt

保存 :wq 操作。

执行保存操作记录

1
$ git rebase --continue

再次查看Git提交记录

1
2
3
4
5
6
7
8
9
10
$ git log
commit ac4b58b641545a41478267624787e08824f3100a (HEAD -> master)
Author: Janko <shuaijinchao@gmail.com>
Date: Sat Aug 1 12:41:36 2020 +0800

add demo.txt

add function.txt

add class.txt

提交到远程仓库

1
$ git push origin master

此时我们在远程仓库查看提交记录,三个文件只有一次 commit add demo.txt

Rebase 场景二:合并分支

从远程主干切版本分支

1
$ git checkout -b v0.1 origin/master

版本分支提交到远程

1
$ git push origin v0.1

切回本地主干

1
$ git checkout master

主干增加文件并提交

1
2
3
4
$ touch v0.1.txt
$ git add v0.1.txt
$ git commit -m "add v0.1.txt"
$ git push origin master

查看提交记录。

1
2
3
4
5
6
$ git log
commit 050cf5c8fa4ae3d99aa87381307696a19d785c9e (HEAD -> master, origin/master, origin/HEAD)
Author: Janko <shuaijinchao@gmail.com>
Date: Sat Aug 1 14:17:42 2020 +0800

add v0.1.txt

版本增加文件并提交

1
2
3
4
5
$ git checkout v0.1
$ touch merge.txt
$ git add merge.txt
$ git commit -m "add merge.txt"
$ git push origin v0.1

查看提交记录。

1
2
3
4
5
6
$ git log
commit 2d174dcd1cb501fc2e09e0ab49b5d654489334bc (HEAD -> v0.1, origin/v0.1)
Author: Janko <shuaijinchao@gmail.com>
Date: Sat Aug 1 14:24:32 2020 +0800

add merge.txt

执行合并

1
2
$ git checkout v0.1
$ git rebase origin/master

查看提交记录。

1
2
3
4
5
6
7
8
9
10
11
12
git log
commit 98d2d7623625eca2ea9fba949b78225ce2e9ddfe (HEAD -> v0.7)
Author: Janko <shuaijinchao@gmail.com>
Date: Sat Aug 1 14:24:32 2020 +0800

add merge.txt

commit 050cf5c8fa4ae3d99aa87381307696a19d785c9e (origin/master, origin/HEAD, master)
Author: Janko <shuaijinchao@gmail.com>
Date: Sat Aug 1 14:17:42 2020 +0800

add v0.7.txt

推送到远程版本分支

1
$ git push origin v0.1 -f

参考:

本文主要参考以上文章,一些成果归功于原文作者。