Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Git:如何重新设置过去特定提交的基础?_Git_Version Control_Git Rebase - Fatal编程技术网

Git:如何重新设置过去特定提交的基础?

Git:如何重新设置过去特定提交的基础?,git,version-control,git-rebase,Git,Version Control,Git Rebase,我想将基础重新设置为一个特定的提交,该提交不是另一个分支的负责人,而是倒退: A --- B --- C master \ \-- D --- E topic 到 我如何以优雅和一般的方式实现这一点 一般来说,我的意思是,目标提交(B)不一定是HEAD的直接祖先(我也可以重设到A或以前的提交),并且主题分支上可能有不止两个提交。我可能还想使用git cherry pick从B重设到A。 git-rebase是一种git-c

我想将基础重新设置为一个特定的提交,该提交不是另一个分支的负责人,而是倒退:

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

我如何以优雅和一般的方式实现这一点


一般来说,我的意思是,目标提交(B)不一定是HEAD的直接祖先(我也可以重设到A或以前的提交),并且主题分支上可能有不止两个提交。我可能还想使用git cherry pick从B重设到A。

git-rebase
是一种
git-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
标志:在执行任何操作之前,git将为您打开一个文本编辑器,其中将列出所有重定基础的提交

这一步的明显好处是向您展示将应用什么(在实际应用它之前),并描述更多的操作,而不仅仅是“选择提交”

你可以在网上搜索更多关于git rebase-i的完整教程,这里有一个:

我认为
git-rebase--to-B--root-master
应该可以工作。(从B重定基址到A时,只需在A根B上使用
--根B
)。按照@JoachimSauer的建议,只需确保主题在主题分支上即可。@JohnSzakmeister或
git-rebase-master-topic--重定基址到B
。与B有松散的关系吗?可能是。@alfunx那也行。。。虽然我个人不喜欢它——我总是觉得应该交换论点——所以我更喜欢另一个版本,因为我从来都记不清从我的头顶上的顺序。:-)
git rebase -i --onto B master topic