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

前端 未结 7 1866
栀梦
栀梦 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 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.

提交回复
热议问题