为什么在重定基时当前分支不会移动? [英] Why current branch is not moved while rebasing?
问题描述
我目前在 dev
分支上,并且在 local/dev
分支上有一些更改.
I am currently on dev
branch and have some changes on local/dev
branch.
...
我确实变基了:
$ git rebase dev local/dev
Created autostash: 92915886
Applied autostash.
Successfully rebased and updated detached HEAD.
提交已重新设置,但 dev
指针未更新:
Commits are rebased but dev
pointer is not updated:
阅读 man git rebase
:
所有由当前分支中的提交所做的更改,但不在< upstream>中.保存到临时区域
All changes made by commits in the current branch but that are not in <upstream> are saved to a temporary area
**这是在第二张图片上标记为>
的提交
**This is the commits marked as >
on second picture
当前分支被重置为< upstream>....
The current branch is reset to <upstream> ...
** 上游
是 dev
.所以我现在在 dev
上
** upstream
is dev
. So I am on dev
at this point
然后将提交重新应用于当前分支
The commits are then reapplied to the current branch
**提交重新应用于 dev
** Commits are reapplied to dev
为什么 dev
指针没有通过 HEAD
移到顶部?
Why dev
pointer is not moved to top with HEAD
?
我应该使用一些选项将当前的 dev
与 HEAD
一起移到顶部吗?
Should I apply some option to move my current dev
on top with HEAD
?
更新
这是非常非常的阶段.如果 dev
和 local/dev
不同,并且可能会被转发,那么我的 dev
就会移到最前面,因为我希望它能正常工作.没有任何额外的
UPDATE
This is very very strage. When dev
and local/dev
differ liner and could be fas forwarded, then my dev
is moved on top as I am expected to work. Without any extra
git branch -f dev
git checkout dev
重新设置基准之前的历史记录如何:以及重新设置后的方式:
How history looks before rebase: and how after rebase:
推荐答案
为什么 dev
指针没有通过 HEAD
移到顶部?--因为在重新设置基准期间不是当前分支.
Why dev
pointer is not moved to top with HEAD
? -- because it is not the current branch during the rebase.
请仔细阅读 git rebase
的文档.它在第一段中进行了解释:
Read the documentation of git rebase
carefully. It explains in the first paragraph:
git rebase [-i | --interactive] [<options>] [--exec <cmd>]
[--onto <newbase> | --keep-base] [<upstream> [<branch>]]`
如果指定了< branch>
,则 git rebase
将在执行其他任何操作之前执行自动的 git开关< branch>
.否则,它将保留在当前分支上.
If <branch>
is specified, git rebase
will perform an automatic git switch <branch>
before doing anything else. Otherwise it remains on the current branch.
用简单的英语来说,如果使用两个分支(< upstream>
和< branch>
)调用 git rebase
切换到第二个分支,然后将其重新放置在第一个分支的顶部. *
In plain English, if git rebase
is invoked with two branches (<upstream>
and <branch>
), it first switches to the second branch then rebase it on top of the first branch.*
这意味着:
git rebase dev local/dev
是以下操作的快捷方式:
is a shortcut for:
git switch local/dev
git rebase dev
rebase期间的当前分支为 local/dev
;这是唯一受rebase影响的分支.
The current branch during the rebase is local/dev
; it is the only branch that is affected by the rebase.
* < upstream>
不必是分支.可以使用任何标识提交的引用(分支,标签,提交, HEAD
, HEAD ^ 2
, HEAD @ {1}
等)作为< upstream>
.
* <upstream>
does not need to be a branch. Any reference that identifies a commit (branch, tag, commit, HEAD
, HEAD^2
, HEAD@{1}
etc) can be used as <upstream>
.
< branch>
必须是一个分支,因为作为重新设置的结果,它将移至其他提交.
<branch>
, on the other hand, needs to be a branch because, as a result of the rebase, it will move to a different commit.
您错误地解释了文档.您在问题中说:
You interpret the documentation incorrectly. You say in the question:
当前分支重置为
< upstream>
...
**上游是dev.所以我现在在开发上
** upstream is dev. So I am on dev at this point
该点上的当前分支不一定是 dev
(实际上是如上所述的 local/dev
).
The current branch on that point is not necessarily dev
(it is, in fact, local/dev
as explained above).
将当前分支重置为< upstream>
" 并不意味着< upstream>
成为当前分支但当前分支( local/dev
)已移至< upstream>
所标识的提交.此时,Git所做的等同于 git reset --hard< upstream>
.
"The current branch is reset to <upstream>
" does not mean that <upstream>
becomes the current branch but that the current branch (local/dev
) is moved to the commit identified by <upstream>
. What Git does at this point is equivalent to git reset --hard <upstream>
.
上面的分析忽略了 local/dev
是一个远程分支的事实.最初我没有注意到.
命名远程 local
有什么意义?这是一种误导.
The analysis above ignores the fact that local/dev
is a remote branch. I didn't notice that initially.
What's the point of naming a remote local
? It is misleading.
因为 local/dev
不是本地分支,所以 git switch local/dev
将存储库引入
Because local/dev
is not a local branch, git switch local/dev
brings the repo in a detached HEAD
state. The rest of the rebase happens as described in the documentation but in the end you have a new history line and no branch that points to it (except for HEAD
). If you switch the branch the result of the rebase is lost.
有两种解决方法:
-
在当前提交上移动分支
dev
.有几种方法可以做到这一点,但是最简单的方法是使用git branch -f dev
.
Move the branch
dev
on the current commit. There are several ways to do this but the easiest way is to usegit branch -f dev
.
放弃所做的更改,切换到 dev
分支并正确进行变基:
Abandon the changes, switch to the dev
branch and do the rebase correctly:
git switch dev
git rebase local/dev
总而言之,由于您在命令行中按错误的顺序放置了分支,因此出现了整个情况.应该是:
All in all, the entire situation appeared because you put the branches in the incorrect order in the command line. It should be:
git rebase local/dev dev
这将检出 dev
分支,然后将可从 dev
访问且不可从 local/dev
访问的提交移到顶部local/dev
,同时在移动的提交上方保留 dev
分支.乍一看,看起来像是从分支 local/dev
派生的分支中剪下了分支 dev
,然后将其移植到了 local/dev的顶部
.
This checks out the dev
branch then moves the commits accessible from dev
and not accessible from local/dev
on top of local/dev
while it preserves the dev
branch on top of the moved commits. On a quick glance it looks like the branch dev
is cut from where it forks from branch local/dev
and the piece is then grafted on top of local/dev
.
这篇关于为什么在重定基时当前分支不会移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!