当 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
Git rebase onto a tag when master and a branch is ahead of the current commits
提问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 C
is the common ancestor between the feature-branch
and origin/master
This is what git rebase later will use when we use git rebase origin/master feature-branch
.
请注意,这是和C
之间的共同祖先这就是 git rebase 以后在我们使用.feature-branch
origin/master
git 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/master
and 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-branch
that is not also on origin/master
). Now that we know what we cut off, we use the the --onto
to 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 的一个很好的解释可以在这里找到