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

master A - B - C - D - E

为此:

newbranch     C - D - E
             /
master A - B 

当前回答

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

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

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

其他回答

最简单的方式就是这样做:

任命主分支到您的新分支(假设您在主分支):

git branch -m newbranch

2、从你想要的承诺中创建主分支:

git checkout -b master <seven_char_commit_id>

例如 git checkout -b master a34bc22

注意:新行业的上流将是起源/主流。

只有在这种情况下:

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

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

这里的大多数解决方案计算了你想回去的承诺数量,我认为这是一个错误的方法,计算需要计算。

你可以简单地通过你想要的承诺的承诺,或者换句话说,你想要的承诺是最后的承诺:

(重定向自Commit Hash)

为了避免这一点:

1) git checkout master

2) git branch <feature branch> master

3) git reset --hard <commit hash>

4) git push -f origin 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 

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