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

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

其他回答

还有另一种方法来做到这一点,只使用2个命令,同时保持您的当前工作树无缝。

git checkout -b newbranch # switch to a new branch
git branch -f master HEAD~3 # make master point to some older commit

旧版本 - 在我学到 git branch -f 之前

git checkout -b newbranch # switch to a new branch
git push . +HEAD~3:master # make master point to some older commit 

能夠推到. 是一個好技巧知道。

只有在这种情况下:

Branch one: A B C D E F     J   L M  
                       \ (Merge)
Branch two:             G I   K     N

我表演了:

git branch newbranch 
git reset --hard HEAD~8 
git checkout newbranch

我希望承诺我会成为头,但承诺是现在的。

要确保在历史上登上正确的位置,就更容易与承诺的哈希工作。

git branch newbranch 
git reset --hard #########
git checkout newbranch

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}

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

你可以做到这一点只是我使用的3个简单步骤。

1)做一个新的分支,你想承诺你最近的更新。

git branch <branch name> 品牌名称

2) 查找最近的承诺 ID 为承诺新分支。

登录

3) 复制的承诺ID 注意,最最近的承诺列表发生在顶部,所以你可以找到你的承诺。

git cherry-pick d34bcef232f6c...

你也可以提供一些承诺ID的范围。

git cherry-pick d34bcef...86d2aec

现在你的工作完成了,如果你选择了正确的ID和正确的分支,那么你会成功,所以在做这件事之前要小心,否则另一个问题可能会出现。

现在你可以按下你的代码

点击Push

如果你想将你的承诺转移到一个现有分支,它会看起来如下:

git checkout existingbranch
git merge branchToMoveCommitFrom
git checkout branchToMoveCommitFrom
git reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.
git checkout existingbranch

警告: 这个方法工作,因为你正在创建一个新的分支与第一个命令: git 分支新分支. 如果你想将承诺转移到一个现有分支,你需要将你的变化融入现有分支之前执行 git 重新设置 --hard HEAD~3 (见移动到一个现有分支上)。

除非有其他情况涉及,否则可以轻松地通过连接和旋转。

# Note: Any changes not committed will be lost.
git branch newbranch      # Create a new branch, saving the desired commits
git checkout master       # checkout master, this is the place you want to go back
git reset --hard HEAD~3   # Move master back by 3 commits (Make sure you know how many commits you need to go back)
git checkout newbranch    # Go to the new branch that still has the desired commits

git reset --hard a1b2c3d4

最后,您可能需要强迫将最新的更改推到主重复:

git push origin master --force

警告: 使用 Git 版本 2.0 及以后,如果您稍后 git 将新分支转移到原始(主)分支上,您可能需要在转移期间明确的 --no-fork-point 选项,以避免失去所承担的承诺。