Reverting to a specific commit without losing history

前端 未结 3 1458
孤城傲影
孤城傲影 2021-02-19 07:57

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

3条回答
  •  旧巷少年郎
    2021-02-19 08:28

    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
    

    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.

提交回复
热议问题