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

master A - B - C - D - E

为此:

newbranch     C - D - E
             /
master A - B 

当前回答

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

1) 创建一个新的分支,将所有更改转移到 new_branch。

git checkout -b new_branch

(二)然后回到旧的分支。

git checkout master

3、做吉·雷巴斯

git rebase -i <short-hash-of-B-commit>

4) 然后打开的编辑器包含最后3个承诺信息。

...
pick <C's hash> C
pick <D's hash> D
pick <E's hash> E
...

5) 更改选项下载所有这些 3 任务. 然后保存并关闭编辑器。

...
drop <C's hash> C
drop <D's hash> D
drop <E's hash> E
...

6) 现在最后3个命令从当前分支(主)中移除。 现在强烈推分支,在分支名称之前 + 标志。

git push origin +master

要做到这一点,而不重写历史(即如果您已经推出了承诺):

git checkout master
git revert <commitID(s)>
git checkout -b new-branch
git cherry-pick <commitID(s)>

那么,两支分支都可以在没有武力的情况下被推动!

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

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

我不得不将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树的旧分支和新分支。