简体   繁体   中英

How to go back to the previous master branch

I have a serious problem with git. One of my team members decided to do a Git commit without before doing a git pull command so the master head was behind so I decided to do a git push --force. Now the git branches are all messed up and I don't know how to change it. I have attached a screenshot to show what I am talking about.

在此处输入图片说明

First off, don't panic!

Next, ask that no one else push or pull until the repo you pushed to is neat and tidy again. If you go to that particular machine, you can use git reflog to see where it was before this happened. Other co-workers might also have in their repo a copy of this remote repo's master branch where it used to be, if they haven't pulled in the messy version. Use either method to find the hash of the last sensible commit.

If the remote server's a regular repo, not headless, then you can simply use git checkout master to make sure you're on the master branch, then git reset [last known good commit hash] --hard to set it back to how it was. This simply pretends that everything since that commit didn't happen. This kind of thing can be dangerous if you're not sure what you're doing, so make sure you make a backup first!

Now that repo's clean again, everyone else can pull from it and push to it again.

Next up is the person who made the problematic commit. They can do the same thing: git checkout master to go to their local copy of the master branch, then git reset [last known good commit hash that they've already pulled in] --hard to move it back to where it originally was, then git pull [remote server name] master to get the subsequent changes.

Then they'll have to merge in their changes again.

Key points to remember are:

  • When things go wrong, make a backup before doing anything drastic!
  • Don't panic, you can use Git to essentially rewrite history
  • git reflog tells you what you've had checked out, which is useful for undoing mistakes
  • git reset [hash] --hard pretends that everything since that hash didn't happen.

In the future, if you need to force something, think if there's something you should check to do first. If you're fighting against Git, it's usually a sign you've overlooked something.

Since you're a team, there's a chance that one of your team members have the old master in their local system. Simply force push from that system to restore your remote Git repo.

Then go back to the team member who created the recent commits, pull the old master onto his system, resolve conflicts if any and push like normally done.

The easiest for you, is go back by one commit with your master branch, which would be something like:

git revert HEAD~1

Check also the answer given here for a detailed description of different revert commits.

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