Checkout old commit and make it a new commit

This question already has an answer here:

  • How to revert Git repository to a previous commit? 38 answers

  • git rm -r .
    git checkout HEAD~3 .
    git commit
    

    提交后,新HEAD文件将与修订HEAD~3中的文件相同。


    It sounds like you just want to reset to C; that is make the tree:

    ABC

    You can do that with reset :

    git reset --hard HEAD~3
    

    (Note: You said three commits ago so that's what I wrote; in your example C is only two commits ago, so you might want to use HEAD~2 )


    You can also use revert if you want, although as far as I know you need to do the reverts one at a time:

    git revert HEAD     # Reverts E
    git revert HEAD~2   # Reverts D
    

    That will create a new commit F that's the same contents as D, and G that's the same contents as C. You can rebase to squash those together if you want


    This is exactly what I wanted to do. I was not sure of the previous command git cherry-pick C , it sounds nice but it seems you do this to get changes from another branch but not on same branch, has anyone tried it?

    So I did something else which also worked : I got the files I wanted back from the old commit file by file

    git checkout <commit-hash> <filename>
    

    ex : git checkout 08a6497b76ad098a5f7eda3e4ec89e8032a4da51 file.css

    -> this takes the files as they were from the old commit

    Then I did my changes. And I committed again.

    git status (to check which files were modified)
    git diff (to check the changes you made)
    git add .
    git commit -m "my message"
    

    I checked my history with git log , and I still have my history along with my new changes made from the old files. And I could push too.

    Note that to go back to the state you want you need to put the hash of the commit before the unwanted changes. Also make sure you don't have uncommitted changes before you do that.

    链接地址: http://www.djcxy.com/p/410.html

    上一篇: 我可以将.git文件夹存储在我想跟踪的文件之外吗?

    下一篇: 签出旧的提交并使其成为新的提交