我如何将我最近的承诺转移到一个新的分支机构,并在这些承诺完成之前重新定义主? 例如:

master A - B - C - D - E

为此:

newbranch     C - D - E
             /
master A - B 

当前回答

TLDR

git checkout branch_to_remove_commits
git reset --hard ${hash_of_new_tip}
git checkout -b branch_to_store_commits
# Move commits (single hash, list of hashes or range ffaa..ffoo) 
git cherry-pick ${commit_hash}
git push --set-upstream origin branch_to_store_commits
# Switch back to last branch
git checkout -
git push -f

為我

git log --pretty=oneline -n ${NUMBER}

最好的工作是识别所涉及的承诺哈希。

其他回答

1) 创建一个新的分支,将所有更改转移到 new_branch。

git checkout -b new_branch

(二)然后回到旧的分支。

git checkout master

3、做吉·雷巴斯

git rebase -i <short-hash-of-B-commit>

4) 然后打开的编辑器包含最后3个承诺信息。

...
pick <C's hash> C
pick <D's hash> D
pick <E's hash> E
...

5) 更改选项下载所有这些 3 任务. 然后保存并关闭编辑器。

...
drop <C's hash> C
drop <D's hash> D
drop <E's hash> E
...

6) 现在最后3个命令从当前分支(主)中移除。 现在强烈推分支,在分支名称之前 + 标志。

git push origin +master

如果你只需要将所有的无缝承诺转移到一个新的分支,那么你只需要,

创建一个新的分支从当前一个 :git 分支新分支名 推你的新分支: git 推起源新分支名 转换你的旧(当前)分支到最后一个推 / 稳定状态: 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

我很惊讶,没有人这样推荐:

git checkout master
git checkout <commit hash from which you want to split>
git checkout -b new_branch
git rebase master
git checkout master
git reset --hard <commit hash you splitted>

要解释:

步骤我们检查的承诺,我们想要分裂,然后从这个承诺创建一个新的分支,做 rebase 将同步 new_branch 和 master. 因此,现在我们有两个相同的分支与同样的承诺重新设置在 master,我们清理后分裂列表项目后最后的承诺

大多数以前的答案都是危险的错误!

不要这样做:

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个承诺。