简体   繁体   中英

Git: Reset to old commit

I know this question has been answered a couple of times, but so far, every posted solution I tried did not work for me :(

The situation is: We have a working commit in the master branch we want to go back to. Unfortunately, multiple changes (including merges with other branches) have been pushed to master since then.

So what I need to do is to get the master branch to that last working commit again - or checkout the last working commit and "overwrite" the last commits on master with it - on the server (github) repo. I don't really care if the changes made to the last working commit are lost, but It wouldnt hurt if they are kept intact.

Important is that other branches are not touched!!!

git revert did not do the job in multiple ways...

We can try replacing the master branch with a new branch, slightly fishy :)

Create a new branch from the working commit that you know

  • if commit is nth level down from head use git branch master-temp HEAD~n . replace n with level.

    • if you have commit hash use git branch master-temp <sha1>
  • Rename the current master branch to new name master-old or master-deprecated
  • Rename the new branch master-temp to master

This would remain the changes, create a branch of working code. All the team members can continue their work without any changes.

If any other commit is required from the old branch, cherry-pick the commit to new master branch.

As @Flows added,

  • Need to use git push -force <branch> if need to push to master.

Local master branch has to be updated with the changes and dev cycle follows as designed.

I would suggest you find out the commit id of the last good commit that you wish to switch to using:

git log --stat

Then, provided you're in the master branch do a git checkout to that commit id

git checkout <commit id>

This may land you to a detached state, now ensure that you're back to where you wished and do a final commit and push to master. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM