ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

git rebase合并提交记录

2021-05-06 15:01:14  阅读:187  来源: 互联网

标签:git Author rebase 提交 commit Thu


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)

  1. 历史提交日志信息都不要了,重新写一份整合好的提交日志
r 600c599 1
f 9457fc0 2
f 964e0f1 3
f 3b026ea 4
f 409f15f 5
  1. 保留所有的历史信息,但是合并成为一份
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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有