Reverting to a specific commit without losing history

前端 未结 3 1456
孤城傲影
孤城傲影 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:37

    If you're sure that neither soft reset nor creating multiple branches work for your use case, you could do

    git diff HEAD commit_hash_to_go_to | git apply
    

    This will create a diff of changes between the latest commit on your branch and the commit with the desired state and automatically apply it. That will simply change the files, it's your job to add them to staging and commit the result. Might be useful if you want to try out different solutions and keep the history of your changes WITHIN the same branch or avoid multiplying local branches.

    If you encounter "cannot apply binary patch to without full index line" error, add --binary flag:

    git diff HEAD commit_hash_to_go_to --binary | git apply
    

    Before doing this ensure that you've got no uncommitted changes - otherwise the patch won't be applied (it's atomic so either all changes go through or none, so you won't end up in an inconsistent state)

    NOTE: this simply changes the files and marks them as modified. It does NOT alter commit history or create new commits

提交回复
热议问题