如何在保留原始提交历史的同时将分支重新绑定到主分支?

时间:2016-01-22 22:18:51

标签: git

我已阅读此http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html。这是我需要达成的目标:

1>压缩我的开发分支提交并保持主分支干净

2 - ;使用开发分支保持提交历史记录以便于检查。

在上面的链接中,作者介绍了一个工作流,它将折叠本地主分支的提交历史记录。为了存档我的目标,我完成了以下测试工作流程。但是,我认为有更好的方法可以做同样的事情。

<<步骤1> :家务管理

git branch
#   develop
# * master

git lg2
# * 5a8b846 - Fri, 22 Jan 2016 15:16:37 -0600 (31 minutes ago) (develop)
# |           Moving license into its own file - Mark
# * 9e8bc03 - Fri, 22 Jan 2016 15:16:05 -0600 (32 minutes ago)
# |           Adding license - Mark
# * 8c7b282 - Fri, 22 Jan 2016 15:15:15 -0600 (33 minutes ago)
# |           Removing some bad formatting in the README - Mark
# * 3797e5b - Fri, 22 Jan 2016 15:14:39 -0600 (33 minutes ago) (HEAD -> master)
#             add README - Mark

<<步骤2> :基于develop创建一个重复的分支,以便在rebase

之后保留开发的原始提交历史
git checkout develop
# Switched to branch 'develop'

git checkout -b developRebase
# Switched to a new branch 'developRebase'

git branch
#   develop
# * developRebase
#   master

git lg2
# * 5a8b846 - Fri, 22 Jan 2016 15:16:37 -0600 (41 minutes ago) (HEAD -> developRebase, develop)
# |           Moving license into its own file - Mark
# * 9e8bc03 - Fri, 22 Jan 2016 15:16:05 -0600 (42 minutes ago)
# |           Adding license - Mark
# * 8c7b282 - Fri, 22 Jan 2016 15:15:15 -0600 (42 minutes ago)
# |           Removing some bad formatting in the README - Mark
# * 3797e5b - Fri, 22 Jan 2016 15:14:39 -0600 (43 minutes ago) (master)
#             add README - Mark

<<步骤3> :将新分支重新绑定为主

git rebase -i master developRebase
# [detached HEAD 04c1710] Removing some bad formatting in the README
#  Date: Fri Jan 22 15:15:15 2016 -0600
#  1 file changed, 5 insertions(+), 1 deletion(-)
# Successfully rebased and updated refs/heads/developRebase.

git lg2
# * 04c1710 - Fri, 22 Jan 2016 15:15:15 -0600 (48 minutes ago) (HEAD -> developRebase)
# |           Removing some bad formatting in the README - Mark
# | * 5a8b846 - Fri, 22 Jan 2016 15:16:37 -0600 (46 minutes ago) (develop)
# | |           Moving license into its own file - Mark
# | * 9e8bc03 - Fri, 22 Jan 2016 15:16:05 -0600 (47 minutes ago)
# | |           Adding license - Mark
# | * 8c7b282 - Fri, 22 Jan 2016 15:15:15 -0600 (48 minutes ago)
# |/            Removing some bad formatting in the README - Mark
# * 3797e5b - Fri, 22 Jan 2016 15:14:39 -0600 (48 minutes ago) (master)
#             add README - Mark

<<步骤4> :将新更改合并到主

git checkout master
# Switched to branch 'master'

git merge developRebase
# Updating 3797e5b..04c1710
# Fast-forward
#  README | 6 +++++-
#  1 file changed, 5 insertions(+), 1 deletion(-)

git lg2
# * 04c1710 - Fri, 22 Jan 2016 15:15:15 -0600 (49 minutes ago) (HEAD -> master, developRebase)
# |           Removing some bad formatting in the README - Mark
# | * 5a8b846 - Fri, 22 Jan 2016 15:16:37 -0600 (47 minutes ago) (develop)
# | |           Moving license into its own file - Mark
# | * 9e8bc03 - Fri, 22 Jan 2016 15:16:05 -0600 (48 minutes ago)
# | |           Adding license - Mark
# | * 8c7b282 - Fri, 22 Jan 2016 15:15:15 -0600 (49 minutes ago)
# |/            Removing some bad formatting in the README - Mark
# * 3797e5b - Fri, 22 Jan 2016 15:14:39 -0600 (49 minutes ago)
#             add README - Mark

<<步骤5> :删除临时分支

git branch -d developRebase
# Deleted branch developRebase (was 04c1710).

git lg2
# * 04c1710 - Fri, 22 Jan 2016 15:15:15 -0600 (49 minutes ago) (HEAD -> master)
# |           Removing some bad formatting in the README - Mark
# | * 5a8b846 - Fri, 22 Jan 2016 15:16:37 -0600 (48 minutes ago) (develop)
# | |           Moving license into its own file - Mark
# | * 9e8bc03 - Fri, 22 Jan 2016 15:16:05 -0600 (48 minutes ago)
# | |           Adding license - Mark
# | * 8c7b282 - Fri, 22 Jan 2016 15:15:15 -0600 (49 minutes ago)
# |/            Removing some bad formatting in the README - Mark
# * 3797e5b - Fri, 22 Jan 2016 15:14:39 -0600 (50 minutes ago)
#             add README - Mark

问题

有人可以给我一个更好的方法来做同样的事情吗?

1 个答案:

答案 0 :(得分:1)

在第2步中,

git checkout develop
git checkout -b developRebase

可以使用

在一次通话中完成
# Create a new branch called 'developRebase' based on branch 'develop'
# and immediately check out that new branch:
git checkout develop -b developRebase

在第3步中,您可以缩短

git rebase -i master developRebase

git rebase -i master

因为您已经在分支developRebase上(您在步骤2结束时检查过)并且当前分支是待重组分支的默认分支。

我认为除此之外还有很多改进。你的方法似乎很好。 (我想对git lg2git branch的多次调用只是说明性和/或用于检查每一步是否产生预期结果。)