当 master 和分支在当前提交之前时,Git rebase 到标签上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39768124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:19:03  来源:igfitidea点击:

Git rebase onto a tag when master and a branch is ahead of the current commits

gitrebase

提问by deepnote

I am having difficulties with Git. I am following a remote repo on my local machine. The master branch is being updated from the remote and I always sync with it to get the updates. And then my local branch ("feature" branch) is rebased onto the master branch to apply my local changes. The issue is I forgot to rebase my local branch onto master branch's version tag. instead I rebased onto the latest commit of master branch. Now I want to rebase my local branch to master's version tag branch without losing my local changes. Any ideas how to do it?

我在使用 Git 时遇到了困难。我正在本地机器上跟踪远程仓库。master 分支正在从远程更新,我总是与它同步以获取更新。然后我的本地分支(“功能”分支)重新基于主分支以应用我的本地更改。问题是我忘记将本地分支重新设置为 master 分支的版本标记。相反,我重新基于 master 分支的最新提交。现在我想在不丢失本地更改的情况下将我的本地分支重新设置为 master 的版本标记分支。任何想法如何做到?

                          o -- o
                         /     ^ feature branch
o --- o --- o --- o --- o --- o --- o 
      ^ V2.0                        ^ master (origin/master)

I want to rebase my "feauture" branch like this:

我想像这样重新设置我的“功能”分支:

        o -- o
       /     ^ feature branch
o --- o --- o --- o --- o --- o --- o 
      ^ V2.0                        ^ master (origin/master)

回答by somnium

Checkout the git rebase manpage https://git-scm.com/docs/git-rebase.

查看 git rebase 联机帮助页https://git-scm.com/docs/git-rebase

The remote branch origin/master(assuming your remote is called origin) is the upstream master. Rebase works by selecting a common ancestor between two branches and then cut off one part:

远程分支origin/master(假设您的远程分支称为 origin)是上游主分支。Rebase 的工作原理是在两个分支之间选择一个共同的祖先,然后剪掉一个部分:

                          o -- o
                         /     ^ feature branch
o --- o --- o --- o --- C --- o --- o 
      ^ V2.0                        ^ master (origin/master)

Note that Cis the common ancestor between the feature-branchand origin/masterThis is what git rebase later will use when we use git rebase origin/master feature-branch.

请注意,这是和C之间的共同祖先这就是 git rebase 以后在我们使用.feature-branchorigin/mastergit rebase origin/master feature-branch

Let's cut off the following:

让我们切断以下内容:

                        [ X -- X ]
                         /     ^ feature branch
o --- o --- o --- o --- C --- o --- o 
      ^ V2.0                        ^ master (origin/master)

and attach it to v2.0. So let's do it:

并将其附加到 v2.0。所以让我们这样做:

git stash          #  move local uncomitted changes away
git checkout feature-branch
git rebase --onto v2.0 origin/master
git stash apply    # reapply uncommitted changes

Rebase

变基

The rebase command will calculate the common ancestor between origin/masterand your current branch. It will then cut off all the commits that are unique to your branch. (think about it as asking rebase for everything on feature-branchthat is not also on origin/master). Now that we know what we cut off, we use the the --ontoto specify the target. In our case a tag.

rebase 命令将计算origin/master您当前分支之间的共同祖先。然后它会切断你的分支独有的所有提交。(将其视为要求 rebase 上所有feature-branch不在 上的内容origin/master)。现在我们知道我们切断了什么,我们使用--onto来指定目标。在我们的例子中是一个标签。

A good explanation about rebase can be found here

关于 rebase 的一个很好的解释可以在这里找到