我如何将我最近的承诺转移到一个新的分支机构,并在这些承诺完成之前重新定义主? 例如:
master A - B - C - D - E
为此:
newbranch C - D - E
/
master A - B
我如何将我最近的承诺转移到一个新的分支机构,并在这些承诺完成之前重新定义主? 例如:
master A - B - C - D - E
为此:
newbranch C - D - E
/
master A - B
当前回答
大多数以前的答案都是危险的错误!
不要这样做:
git branch -t newbranch
git reset --hard HEAD~3
git checkout newbranch
git reset --keep HEAD~3
git checkout -t -b newbranch
git cherry-pick ..HEAD@{2}
首先,它放弃了3个最新的承诺( - 保持像 - 硬,但更安全,因为它失败,而不是扔掉未承诺的变化)。 然后它放弃了新分支. 然后它将这些3个承诺返回新分支. 因为它们不再被一个分支提到,它这样做,使用 git 的回归: HEAD@{2} 是 HEAD 使用的承诺提到 2 个操作之前,即在我们 1 检查之前。
# newbranch will omit the 3 most recent commits.
git checkout -b newbranch HEAD~3
git branch --set-upstream-to=oldbranch
# Cherry-picks the extra commits from oldbranch.
git cherry-pick ..oldbranch
# Discards the 3 most recent commits from oldbranch.
git branch --force oldbranch oldbranch~3
技术解释
假设你分离起源 / 主,当它包含的承诺M1,M2,M3,然后做三个承诺自己:
M1--M2--M3 <-- origin/master
\
T1--T2--T3 <-- topic
但然后有人用强迫起源/大师重新编写历史,以删除M2:
M1--M3' <-- origin/master
\
M2--M3--T1--T2--T3 <-- topic
M1--M3' <-- origin/master
\
T1'--T2'--T3' <-- topic (rebased)
因此,下列命令失败的原因:
git branch -t newbranch
git reset --hard HEAD~3
git checkout newbranch
但在这个特殊情况下,我们希望这些3个承诺被视为主题分支的一部分,为了实现这一点,我们需要在以前的修订中排除上流,这不包括3个承诺。
其他回答
對於那些想知道為什麼它工作(正如我開始的那樣):
你想回到C,然后移动D和E到新分支。
A-B-C-D-E (HEAD)
↑
master
在Git分支新Branch之后:
newBranch
↓
A-B-C-D-E (HEAD)
↑
master
此分類上一篇: git reset --hard HEAD~2:
newBranch
↓
A-B-C-D-E (HEAD)
↑
master
因为一个分支只是一个指标,大师指向最后一个承诺。当你做了新的Branch,你只是做一个新的指标到最后一个承诺. 然后使用Git重新设置你移动了大师指标回两个承诺. 但因为你没有移动新的Branch,它仍然指向承诺它最初做了。
这不是在技术上“移动”它们,但它有相同的效果:
A--B--C (branch-foo)
\ ^-- I wanted them here!
\
D--E--F--G (branch-bar)
^--^--^-- Opps wrong branch!
While on branch-bar:
$ git reset --hard D # remember the SHAs for E, F, G (or E and G for a range)
A--B--C (branch-foo)
\
\
D-(E--F--G) detached
^-- (branch-bar)
Switch to branch-foo
$ git cherry-pick E..G
A--B--C--E'--F'--G' (branch-foo)
\ E--F--G detached (This can be ignored)
\ /
D--H--I (branch-bar)
Now you won't need to worry about the detached branch because it is basically
like they are in the trash can waiting for the day it gets garbage collected.
Eventually some time in the far future it will look like:
A--B--C--E'--F'--G'--L--M--N--... (branch-foo)
\
\
D--H--I--J--K--.... (branch-bar)
git reset HEAD~3
git stash
git checkout newbranch
git stash pop
如果你的主要目标是旋转回主 你想保持文件变更 你不关心错误的命令上的消息 你没有推,但 你想这很容易记住 你不想要如临时/新分支, 查找和复制命令, 和其他头痛等并发症
这是什么不做
目标
OP表示,目标是“在这些承诺完成之前重新掌握”而不失去变化,而这个解决方案就是这样。
不要这样做,如果你推了大师的变化
如果你只是重新编写你的本地大师没有影响,当它被推向上流,但推一个重新编写的历史给合作伙伴可能会导致头痛。
從其他文章中採取一些想法,避免任何與重新安裝有關的事情,並且非常偏見,我的解決方案是:
git 分支 # 更改可在新分支 git 推 # 上传,您可能需要与“--set-upstream”混淆,例如 git 推 --set-upstream https:/// 检查新分支在 git 通过 GUI 破坏现行目录从 git 存储库重新克隆
我不自豪,但我保留了我的数据。
这里的大多数解决方案计算了你想回去的承诺数量,我认为这是一个错误的方法,计算需要计算。
你可以简单地通过你想要的承诺的承诺,或者换句话说,你想要的承诺是最后的承诺:
(重定向自Commit Hash)
为了避免这一点:
1) git checkout master
2) git branch <feature branch> master
3) git reset --hard <commit hash>
4) git push -f origin master