我如何将我最近的承诺转移到一个新的分支机构,并在这些承诺完成之前重新定义主? 例如:
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
当前回答
我不得不将7个承诺从一个旧行业转移到一个新行业。
git checkout old-branch # in the example, master
git reset --hard h4sh # h4sh is the hash for the commit
git checkout -b new-branch
git push origin new-branch
在 git checkout new-branch 之后,我得到了好 git log 和 git 状态,但是,当我访问旧行(git checkout old-branch)时,我得到了“git 落后于 7 行,可以快速推进”。
git checkout old-branch
git status
> git is behind by 7 commits and can be fast-forwarded
git push origin old-branch -f
在这一步之后,最后7个承诺仅适用于新分支,而以前的承诺则适用于Bitbucket树的旧分支和新分支。
其他回答
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)
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}
最好的工作是识别所涉及的承诺哈希。
还有另一种方法来做到这一点,只使用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