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

master A - B - C - D - E

为此:

newbranch     C - D - E
             /
master A - B 

当前回答

git reset HEAD~3
git stash
git checkout newbranch
git stash pop

如果你的主要目标是旋转回主 你想保持文件变更 你不关心错误的命令上的消息 你没有推,但 你想这很容易记住 你不想要如临时/新分支, 查找和复制命令, 和其他头痛等并发症

这是什么不做

目标

OP表示,目标是“在这些承诺完成之前重新掌握”而不失去变化,而这个解决方案就是这样。

不要这样做,如果你推了大师的变化

如果你只是重新编写你的本地大师没有影响,当它被推向上流,但推一个重新编写的历史给合作伙伴可能会导致头痛。

其他回答

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

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)

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

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

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

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,我们清理后分裂列表项目后最后的承诺

还有另一种方法来做到这一点,只使用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 

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

我怎么能离开这

A - B - C - D - E 
                |
                master

到此?

A - B - C - D - E 
    |           |
    master      newbranch

兩個命令

吉特分支 -m 主新分支

给予

A - B - C - D - E 
                |
                newbranch

吉特分支大师B

给予

A - B - C - D - E
    |           |
    master      newbranch