Want to change my master to an older commit, how can I do this?

前端 未结 7 1890
栀梦
栀梦 2020-12-12 11:21

I want to rollback to a previous commit, and then publish that code, then go back to the latest commit.

i.e. so my master is pointing to an older commit version just

7条回答
  •  感动是毒
    2020-12-12 11:46

    Assuming a commit graph like so:

    | (A) ---------> (B) ----------> (C)
    |                                 ^
    |                              (master)
    

    You want to first checkout master and create a branch that points to where master currently is:

    git checkout master
    git branch pointer master
    

    Should look like this now:

    | (A) ---------> (B) ----------> (C)
    |                                 ^
    |                       (HEAD, master, pointer)
    

    Now that you're already on master, we'll tell the master branch to move backward one commit:

    git reset master~1
    

    Now, master should be moved back one space, but the pointer branch is still on the most recent commit :

    | (A) ---------> (B) ----------> (C)
    |                 ^               ^
    |           (HEAD, master)    (pointer)
    

    At this point, you can push master to a remote, or where ever, then fast forward merge it back up to the pointer branch. You can kill the pointer branch at that point :

    git push origin master
    git merge --ff-only pointer
    git branch -D pointer
    

    Final :

    | (A) ---------> (B) ----------> (C)
    |                 ^               ^
    |         [ origin/master ]    (HEAD, master)
    

提交回复
热议问题