1

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...

2
  • Did you try that : stackoverflow.com/a/4114122/2394026. Look the 2nd solution of the paragraph "Undo published commits with new commits" Commented Sep 7, 2016 at 11:52
  • I can think of at least two questions here on SO that seem to solve your problem. What exactly have you already tried? Commented Sep 7, 2016 at 12:07

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

After that, you should be able to push --force to the repository in order to provide update for all users. But the force is not always available and need to be done carefully.
@Flows, Thanks for the addition, Yes, this has to be executed carefully for the first time. force push to be made for the first time and all the other users have to update their respective master branches
1

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.

2 Comments

Thanks. Yet, I get a "HEAD detached at 761f50b" after I want to commit again. How can I commit and then push to the master branch?
From this point there are 2 things that you can do, since your head is now detached to the desirable commit: 1. Create a new branch: git checkout -b <new branch name> This would create a new branch from the detached head state. 2. If, however you with to permanently set the master at that state then simply reset the head of master to that commit id. Follow these steps: git checkout master You will switch to master branch again with all the undesirable changes. From there you need to reset the HEAD to the desirable commit id: git reset 761f50b

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.