Git - 如何在分支之间移动提交

标签 git

我有以下内容:

branch0  ---  commit1 --- commit2 --- branch2 --- commit3
   |
branch1
   |
commit4
   |
commit5

我想将其更改为以下内容(即从 commit1 移动所有内容):

branch0
   |
branch1
   |
commit4
   |
commit5  ---  commit1 --- commit2 --- branch2 --- commit3

我尝试过使用rebasecherry-pick,但我的知识还不够,所以欢迎任何建议。

谢谢

更新

根据下面 mimikrija 的回答,我尝试了以下操作:

git checkout branch0
git checkout -b temp
git rebase branch1
## fix conflicts
git add .
git commit -am "rebase applied"
git rebase --continue
git branch -mv -f branch1

我收到以下错误:

fatal: Invalid branch name: 'HEAD'

git status
rebase in progress; onto 89844e6
You are currently rebasing branch 'temp' on '89844e6'.
  (all conflicts fixed: run "git rebase --continue")

最佳答案

假设这些都是本地分支(尚未发布,或已发布但未被其他任何人使用),这些将是要采取的步骤。

git checkout branch2

git checkout -b temp 根据branch2创建一个临时分支

git rebase branch1 现在你有了你在图表中画的东西,但它叫做 temp

git branch -mv -f branch1 强制重命名分支

最后,从 branch2 中删除提交:

git checkout branch2

git reset --hard branch0

关于Git - 如何在分支之间移动提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58408550/

相关文章:

git - 重写 Git 历史的实际后果是什么?

git - 使用 git 查找特定代码部分随时间的变化

git - 如果我在 git 中删除父分支,子分支会发生什么

git - 如何创建多个 GIT 实例?

git - 我怎样才能回去测试哪里出了问题?

git - 在推送后恢复空的 merge 提交

git - 如何查看git中第一次和第三次提交的区别?

c++ - 从 github 编译 c++ boost 库时出错

git - 我应该在哪个分支中构建 docker 镜像?

git - 使用 Git 强制远程 repo 压缩 (GC)