将整个开发分支改组为新的主分支 [英] Rebase entire development branch onto new master branch

查看:98
本文介绍了将整个开发分支改组为新的主分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个理论上应该遵循Gitflow工作流的存储库(请参阅

I'm working with a repository that in theory should be following the Gitflow Workflow (see A successful git branching model by Vincent Driessen). However, the initial commit on the repository was made on the develop branch and there is no master branch to be seen. It's nearing release time and I need to create a master branch that reflects the production-ready state of the project which should've been there from the start. Keep in mind that the develop branch has multiple feature branches coming off of it. The repository is entirely local and hasn't been pushed.

我的想法是创建一个孤立分支master并将develop分支重新建立到该分支上,但是我不知道该怎么做.

My idea was to create an orphan branch master and rebase the develop branch onto it, but I don't know how I'd go about doing that.

那么,如何创建master分支,就好像它是一开始创建的一样?

So, how can I create the master branch as if it was created from the start?

更新:就我而言,对develop的第一次提交不是应该被认为适合生产的提交,因此将其用作初始master提交是不明智的.该项目处于这种状态的原因是,当决定使用Git时,它最初并未使用VCS.

Update: In my case, the first commit on develop is not a commit that should be considered suitable for production, so using this as the initial master commit would be unwise. The reason that the project is in this state is because it was originally not using a VCS when it was decided to use Git.

推荐答案

经过一番摆弄,这就是我的想法.这是 VonC的答案的一种简单,手动的方法.

After some fiddling, this is what I came up with. This is a simpler, manual approach to VonC's answer.

假设您有一个分支develop,其中包含存储库的初始提交,并且您想要重写历史记录,以使master分支中包含新的初始提交.

Let's assume you have a branch develop which contains the initial commit of your repository, and you'd like to rewrite history such that a master branch contains the new initial commit instead.

首先,如果develop分支上的初始提交适合作为新master分支上的初始提交,那么您只需在其中创建master分支就可以了:

First off, if the inital commit on your develop branch is suitable as the initial commit on the new master branch, then you can simply create the master branch there and you're done:

$ git branch master <sha1-of-initial-commit-on-develop>

如果您没有那么奢侈的话,则需要创建一个新的空提交作为master的初始提交.

If you don't have that luxury, then you'll need to create a new empty commit that will serve as the initial commit of master.

# Create the new master branch
$ git checkout --orphan master
# Clear the working directory (we want the initial commit to be empty)
$ git rm -rf .
# Create the initial commit on master
$ git commit --allow-empty -m "Initial commit"
# Rebase the entire develop branch onto the new master branch
$ git rebase --onto master --root develop

如果develop分支上有任何分支离开,它们将被严重地弄乱了".这是因为那些分支(我们将其称为主题分支)在重新建立基础之前仍指向旧的develop分支.如果develop分支没有分支,则说明您已经完成.

If there were any branches coming off of the develop branch, they would've been "majorly messed up". This is because those branches (we'll call them topic branches) are still pointing to the old develop branch before it was rebased. If you had no branches coming off of the develop branch, then you're done.

每个主题分支都必须重新基于新的develop分支.为此,我们将遵循另一个问题中概述的步骤( Git:如何基于特定的提交?).对于每个主题分支,请按照下列步骤操作.

Each topic branch is going to have to be rebased onto the new develop branch. To do this, we're going to follow the steps outlined in another question (Git: How to rebase to a specific commit?). For each topic branch, follow these steps.

用提交的sha1替换<common-ancestor>在新创建的develop分支上,主题分支应从该分支分支.

Replace <common-ancestor> with the sha1 of the commit on the newly created develop branch where the topic branch should branch off of.

$ git branch temp <common-ancestor>
$ git checkout <topic-branch>
$ git rebase temp
$ git branch -d temp

就是这样!请记住,您不应基于与其他人协作的分支.

And that's it! Keep in mind that you should not rebase on a branch that you're collaborating on with someone else.

这篇关于将整个开发分支改组为新的主分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆