Git:如何 rebase 到过去的特定提交?

标签 git version-control git-rebase

我想重新定位到一个特定的提交,它不是另一个分支的 HEAD,而是倒退:

A --- B --- C          master
            \
             \-- D --- E   topic


A --- B --- C          master
      \
       \-- D --- E   topic

我如何以优雅和通用的方式实现这一目标?

一般而言,我的意思是目标提交 (B) 可能不一定是 HEAD 的直接祖先(我也可能会 rebase 到 A 或之前的提交),并且主题分支上可能有不止两个提交。我可能还想从 B rebase 到 A。

最佳答案

使用 git cherry-pick
git rebasegit cherry-pick类固醇。

如果您只有几个提交要移动:您可以使用 git cherry-pick一一挑选

# move back to B
git checkout B

# start a branch here, and use it as the active branch
git checkout -b wip

# cherry-pick the commits you want to have
git cherry-pick D
git cherry-pick E

# if all went well : move the 'topic' branch to the current branch :
git checkout topic
git reset --hard wip

# delete the temporary 'wip' branch
git branch -d wip

使用 git rebase

如评论中所述:git rebase可以采取额外的选项来仅移动一系列提交。

你可以做和上面一样的cherry-pick序列使用:
git rebase --onto B master topic

额外说明: git rebase -i
git rebase还有一个--interactive|-i旗帜 :
git rebase -i --onto B master topic

-i flag :在做任何事情之前,git 会为你打开一个文本编辑器,其中将列出所有重新提交的提交。

这一步的明显好处是向您展示将应用的内容(在实际应用之前),并且还描述了比“选择那个提交”更多的操作。

您可以在 git rebase -i 上搜索网络以获取更完整的教程。 ,这是一个:
Git Interactive Rebase, Squash, Amend and Other Ways of Rewriting History (thoughtbot.com)

关于Git:如何 rebase 到过去的特定提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58763467/

相关文章:

git - 在另一个公共(public) GitHub 存储库更改上触发自动 Docker Hub 构建

git - 如何在 DAGsHub 上创建新的 Git 实验

xcode - 我应该告诉 Mercurial 忽略 Xcode iOS 项目中的哪些文件?

Git:将现有存储库从 PC 移动到服务器,从服务器克隆

windows - 什么是 Git 的好(免费)可视化 merge 工具? (在 window 上)

git - Azure git 部署失败

git - 执行多个 git 远程推送的正确过程

git - Git rebase 的 --merge 选项有什么作用?

git - 什么是正确的 Git p4 工作流程?

git - .git/rebase-apply 的内容是否记录在案?