简体   繁体   中英

How to rebase a branch to master while keeping the original commit history?

I have read this http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html . Here is I need to achive:

1> squash my development branch commit and keep the master branch clean

2> keep commit history with development branch for easy checking.

The the above link, the author introduces a workflow that will collapse the commit history of local master branch. In order to archive my goal, I have done the following test workflow. However, I think there is a better way to do the same thing.

<< Step 1 >> : house keeping

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

<< Step 2 >> : create a duplicate branch based on develop so that the original commit history of develop can be kept after 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

<< Step 3 >> : rebase the new branch to master

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

<< Step 4 >> : merge the new change to master

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

<< Step 5 >> : delete the temporary branch

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

Question

Can someone give me a better way to do the same thing?

In step 2,

git checkout develop
git checkout -b developRebase

can be done in a single call with

# Create a new branch called 'developRebase' based on branch 'develop'
# and immediately check out that new branch:
git checkout develop -b developRebase

In step 3, you could shorten

git rebase -i master developRebase

to

git rebase -i master

because you already are on branch developRebase (you checked at the end of step 2) and the current branch is the default for the to-be-rebased branch.

I don't think there's much to improve other than that. Your approach seems fine. (I guess the many calls to git lg2 and git branch are simply illustrative and/or for checking that each step yields the expected result.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM