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

前端 未结 7 1862
栀梦
栀梦 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)
    
    0 讨论(0)
  • 2020-12-12 11:48

    use git reset --hard <old commit number>

    it will reset the HEAD to this old commit.

    additionally, you need to use git push -f origin to alter the remote repo too.

    0 讨论(0)
  • 2020-12-12 11:49

    Your question is unclear. I think what you are asking for is this:

    git push -f origin $old_commit_id:master

    What will this do? It will push the $old_commit_id commit to origin as the new head of origin’s master branch.

    If that is what you wanted, you do not need to touch your local master branch at all.

    0 讨论(0)
  • 2020-12-12 11:49

    To move to a previous version:

    git checkout <version hash>

    do your work here and commit it with

    git commit --amend

    To go back to master:

    git checkout master

    0 讨论(0)
  • 2020-12-12 12:03

    If you want to do this and revert the master to the previous commit:

    git checkout master~1            # Checkout previous commit on master
    git checkout -b new_master       # Create branch for new master
    git branch -D master             # Delete old master
    git branch -mv new_master master # Make new_master master
    

    Alternatively:

    git reset --hard master~1        # Reset current branch to one commit ago on master
    
    0 讨论(0)
  • 2020-12-12 12:06

    If you want to avoid force pushing, here's how to revert your repo to an older commit and preserve all intervening work:

    git checkout 307a5cd        # check out the commit that you want to reset to 
    git checkout -b fixy        # create a branch named fixy to do the work
    git merge -s ours master    # merge master's history without changing any files
    git checkout master         # switch back to master
    git merge fixy              # and merge in the fixed branch
    git push                    # done, no need to force push!
    

    Done! Replace 307a5cd with whatever commit you want in your repo.

    (I know the first two lines can be combined, but I think that makes it less clear what's going on)

    Here it is graphically:

    c1 -- c2 -- c3 -- c4 -- c2' -- c5 ...
            \              /
             '------------'
    

    You effectively remove c3 and c4 and set your project back to c2. However, c3 and c4 are still available in your project's history if you ever want to see them again.

    0 讨论(0)
提交回复
热议问题