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

master A - B - C - D - E

为此:

newbranch     C - D - E
             /
master A - B 

当前回答

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

创建一个新的分支从当前一个 :git 分支新分支名 推你的新分支: git 推起源新分支名 转换你的旧(当前)分支到最后一个推 / 稳定状态: git 重新设置 - 硬起源 / 旧分支名

有些人也有其他上流而不是起源,他们应该使用适当的上流。

其他回答

從其他文章中採取一些想法,避免任何與重新安裝有關的事情,並且非常偏見,我的解決方案是:

git 分支 # 更改可在新分支 git 推 # 上传,您可能需要与“--set-upstream”混淆,例如 git 推 --set-upstream https:/// 检查新分支在 git 通过 GUI 破坏现行目录从 git 存储库重新克隆

我不自豪,但我保留了我的数据。

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

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 选项,以避免失去所承担的承诺。

對於那些想知道為什麼它工作(正如我開始的那樣):

你想回到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,它仍然指向承诺它最初做了。

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

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

git branch <branch name> 品牌名称

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

登录

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

git cherry-pick d34bcef232f6c...

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

git cherry-pick d34bcef...86d2aec

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

现在你可以按下你的代码

点击Push

这不是在技术上“移动”它们,但它有相同的效果:

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)