Git: How do you replace your current working directory with a previous commit without branching or git revert?

后端 未结 3 1244
礼貌的吻别
礼貌的吻别 2020-12-18 08:35

Is there an easy way to the following with git?

Basically I want to create a new commit at the top of my commit history that is equivalent to a previous com

3条回答
  •  感情败类
    2020-12-18 09:07

    In situation when you want to return to some state you do following:

    git reset --hard 49a732c

    This step put your master branch into desired state. If you want to save you previous branch state:

    git checkout 48ah14s -b archive/my-unrecognized-experiments

    You still can do it after reset because reset doesn't delete commits.

    PS Branching is essential part of git. It is better to teach branching then teach such complicated (in git) things as you pictured.

    EDIT If you want your master to stay consistent with remotes:

    git reset 49a732c # move HEAD back, all changes still in working tree
    git commit -am "My unrecognized experiments" # save all changes as one commit
    git reset --hard 48ah14s  # restore master HEAD
    git revert  # apply reverted changes to master
    

提交回复
热议问题