How to go back to previous commit without losing last commit in Git?

后端 未结 3 1132
长情又很酷
长情又很酷 2021-02-07 08:01

Here is what I want to do. I want to go back to 2 commits before, bring back the files that changed in that commit as a new commit maybe. But I do not want to lose my last commi

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-07 08:47

    If you want to go back, say 2 commits previous, you can just do git checkout HEAD~2. This will get you all as it was then. If you were on branch master, git checkout master will bring you back to the present. If, however, you want to keep the current state but start a new developemnt branch there, git checkout -b HEAD~2 will start a new branch there. In case you want to rewind master but not loose your current, unfinished/broken work, do

    git branch wip           # New branch ends a current tip
    git reset --hard HEAD~2  # Old branch rewound, get files from then
    

提交回复
热议问题