恢复到特定的提交而不会丢失历史记录

恢复到特定的提交而不会丢失历史记录

本文介绍了恢复到特定的提交而不会丢失历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这类问题重复很多,但我想打开一个新的问题,因为在所有其他问题中都没有找到我想要的最佳解决方法的说明.

I know this type of a question has a lot duplicates, but I wanted to open a new one because I didn't found in all of the other questions the explaination of the best way to do it as I want.

我知道我可以通过以下方式恢复并保留历史记录:

I know i can revert and keep the history by doing:

git reset --soft c14809fa

我想还原development分支并将历史记录保留在另一个分支上.

I want to revert the development branch and keep the history on a different branch.

如果我在还原提交之前将development检出到新分支-例如

If I checkout the development to a new branch before I revert the commits - For example

git checkout -b beforeRevert

然后,我将签出回到开发分支并进行搜索(因为我想继续处理我恢复到的提交中的数据)

Than I will checkout back to the development branch and do the reveting ( because I want to continue working on the data from the commits i had revert to )

另一个分支beforeRevert分支将保留恢复前"的所有历史记录和数据,这些历史记录和数据将在某天再次使用,但不包括在当前的development分支中吗?还是development分支上的还原会以某种方式影响beforeRevert分支?

The other branch, beforeRevert branch, will keep all the history and data of the "before reverting" that will use again someday, but won't include in the current development branch? Or the reverting on the development branch will somehow effects the beforeRevert branch?

推荐答案

最简单的方法就是创建一个新分支(HEAD所在的位置),然后将development还原为您所提交的内容,就像您说的那样想从以下位置恢复工作:

The easiest thing to do, like you say, would be to simply create a new branch where HEAD is and then revert development to the commit you want to resume work from:

git checkout development   # Make HEAD point to the 'development' branch
git branch beforeRevert    # Create a new branch reference pointing to HEAD
git reset --hard c14809fa  # Move HEAD, the index and your working copy to c14809fa

以下是将要发生的情况的图形表示:

Here's a graphical representation of what will happen:

    Step 1               Step 2                  Step 3

            develop    develop, beforeRevert   develop   beforeRevert
           /                    /             /         /
A-B-C-D-E-F          A-B-C-D-E-F             A-B-C-D-E-F
          ^                    ^             ^
         HEAD                 HEAD          HEAD

这里重要的是,HEAD始终指向development分支,因此这是运行git reset --hard c14809fa时移动的分支.新的beforeRevert分支仍将指向HEAD还原之前的位置.

The important thing here is that HEAD is always pointing to the development branch, so that's the branch that gets moved when you run git reset --hard c14809fa. The new beforeRevert branch will still point to where HEAD was before the revert.

这篇关于恢复到特定的提交而不会丢失历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 20:17