我如何将我最近的承诺转移到一个新的分支机构,并在这些承诺完成之前重新定义主? 例如:
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
当前回答
这不是在技术上“移动”它们,但它有相同的效果:
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)
其他回答
在一般...
由 sykora 曝光的方法是最好的选择,但有时不是最简单的方法,而且不是一般的方法。
为了实现OP想要的,它的2步过程:
步骤1 - 注意你想要的新行业的主人的承诺
执行
git checkout master
git log
在这里我将使用: C 承诺: 9aa1233 D 承诺: 453ac3d E 承诺: 612ecb3
注意:您可以使用前七个字符或整个承诺字符。
步骤2 - 把它们放在新行业
git checkout newbranch
git cherry-pick 612ecb3
git cherry-pick 453ac3d
git cherry-pick 9aa1233
或(在 Git 1.7.2 + 上,使用范围)
git checkout newbranch
git cherry-pick 612ecb3~1..9aa1233
git cherry-pick 适用于新行业的这三个承诺。
對於那些想知道為什麼它工作(正如我開始的那樣):
你想回到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
还有另一种方法来做到这一点,只使用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
能夠推到. 是一個好技巧知道。
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}
最好的工作是识别所涉及的承诺哈希。