git rebase合并提交记录
作者:互联网
1. 本地整理提交记录(压缩提交记录)
https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%8F%98%E5%9F%BA
变基操作的实质是丢弃一些现有的提交,然后相应地新建一些内容一样但实际上不同的提交。 如果你已经将提交推送至某个仓库,而其他人也已经从该仓库拉取提交并进行了后续工作,此时,如果你用 git rebase 命令重新整理了提交并再次推送,你的同伴因此将不得不再次将他们手头的工作与你的提交进行整合,如果接下来你还要拉取并整合他们修改过的提交,事情就会变得一团糟。
这里讲到的提交记录不是合并日志,而是合并commit.
git在一个分支上的推进就是以提交为单位,但是小步代码提交的逻辑一个功能能会引入很多的杂乱的提交记录.通过分支内的rebase可以将多次的提交(文件的增删改)在起点位置进行回放,重新设置回放的方法,比如删除某条提交记录,合并多次的提交为一个提交等.
进行一个项目的提交示例.
(1)在已有项目上搞一个新的分支
git checkout -b git_rebase_test
(2)进行五次代码提交,分别在gitrebase.py下提交1,2,3,4,5
commit 409f15f56221ab67eda2fcdb7089bc4a85062990 (HEAD -> git_rebase_test)
Author: xxx <xxx@tencent.com>
Date: Thu May 6 11:50:32 2021 +0800
5
commit 3b026eacc3c01f52433fa42f0685081dca50893a
Author: xxx <xxx@tencent.com>
Date: Thu May 6 11:50:10 2021 +0800
4
commit 964e0f1acc67f721a6d58392e9a4d4dcd64ccd62
Author: xxx <xxx@tencent.com>
Date: Thu May 6 11:50:02 2021 +0800
3
commit 9457fc04029efa4f2b3fa8fa6e2e1404ba4e7c9e
Author: xxx <xxx@tencent.com>
Date: Thu May 6 11:49:55 2021 +0800
2
commit 600c5996d242cdde347ca39c2296507943f6308b
Author: xxx <xxx@tencent.com>
Date: Thu May 6 11:49:47 2021 +0800
1
(3)合并五次代码提交为一次提交
rebase 开始
git rebase -i HEAD~5
可以查看到最近五次的提交记录分别为1,2,3,4,5.变基的核心就是对已有的提交进行重新设置.可以看到针对每一个commit可用的方法有多个
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
这里给出两个提交逻辑.(因为对第一条提交记录使用squash命令会提示error: cannot 'squash' without a previous commit
)
- 历史提交日志信息都不要了,重新写一份整合好的提交日志
r 600c599 1
f 9457fc0 2
f 964e0f1 3
f 3b026ea 4
f 409f15f 5
- 保留所有的历史信息,但是合并成为一份
r 600c599 1
s 9457fc0 2
s 964e0f1 3
s 3b026ea 4
s 409f15f 5
填写rebase commit信息
完成了以上操作,因为选择了reword的操作,所以需要重新编辑一次提交信息.
使用方法1可以得到如下的结果
commit 3ce87d037d78410989188c60ec7378ce508dd4b0 (HEAD -> git_rebase_test)
Author: xxx <xxx@tencent.com>
Date: Thu May 6 10:53:32 2021 +0800
we have a clear commit log!
commit 1939a9ffe38eb88501382eaa749c8e326ef05198
Author: xxx <xxx@tencent.com>
Date: Thu Apr 29 10:26:38 2021 +0800
fix
使用方法2可以得到如下的结果
commit 9e08b4e5e93328bcaf14000a49faa7f4f297c682 (HEAD -> git_rebase_test)
Author: xxx <xxx@tencent.com>
Date: Thu May 6 11:49:47 2021 +0800
we finished our first rebase operation!
2
3
4
5
commit 1939a9ffe38eb88501382eaa749c8e326ef05198
Author: xxx <xxx@tencent.com>
Date: Thu Apr 29 10:26:38 2021 +0800
fix
2. 分支变基
考虑一种情况:从主分支生成了新的分支后进行开发,后续主分支合并了其他的代码,前进了一步,为了避免从主分支合并的merge信息污染了我们这个分支的提交记录(当然我们也可以用提交记录整理的方法合并提交记录,但是不够优雅),我们可以使用分支变基的方法:提取变更记录,在指定的分支上进行一次回放,得到干净的提交记录.
(1)这是我们的提交信息起点,提交了一份we have a clear commit log!
commit 680aa0ab8aad5eadec844417d362e5c018ed73d1 (HEAD -> git_rebase_test)
Author: xxx <xxx@tencent.com>
Date: Thu May 6 10:53:32 2021 +0800
we have a clear commit log!
commit 303ed5f18498974d046b640fc6e8c4f836cddcab (origin/release, release)
Author: xxx <xxx@tencent.com>
Date: Thu Apr 29 10:26:38 2021 +0800
fix
(2)release分支进行了一次提交,信息为release_info
,我们进行rebase操作git rebase release
,可以看到release的信息已经在我们的log中了.
commit 44531cf38d0a210021d320125522a9d325821017 (HEAD -> git_rebase_test)
Author: xxx <xxx@tencent.com>
Date: Thu May 6 10:53:32 2021 +0800
we have a clear commit log!
commit cfc498ddd64c5c440fd86ded1592a6fbd6110867 (release)
Author: xxx <xxx@tencent.com>
Date: Thu May 6 14:24:18 2021 +0800
release_info
commit 303ed5f18498974d046b640fc6e8c4f836cddcab (origin/release)
Author: xxx <xxx@tencent.com>
Date: Thu Apr 29 10:26:38 2021 +0800
fix
(3)rebase也可能存在冲突的情况,需要进行处理冲突,处理完后提交新的记录,但是不需要commit -m
,最后使用git rebase --continue
完成提交
>>> git rebase release
First, rewinding head to replay your work on top of it...
Applying: we have a clear commit log!
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
CONFLICT (add/add): Merge conflict in rebase.py
Auto-merging rebase.py
error: Failed to merge in the changes.
Patch failed at 0001 we have a clear commit log!
hint: Use 'git am --show-current-patch' to see the failed patch
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
>>> git add rebase.py
>>> git rebase --continue
Applying: we have a clear commit log!
标签:git,Author,rebase,提交,commit,Thu 来源: https://blog.csdn.net/u013992365/article/details/116450515