1 introduction

I wrote this blog post after doing some research on a remote branch that requires version rollback while using Git recently.

2 questions

If an incorrect version is submitted, how do I roll back the version?

How do I roll back the remote branch version if I commit an incorrect version to the remote branch?

If you commit an incorrect version to a common remote branch, how do you roll back the version?

3 Local branch version rollback method

If you have made an error commit locally, rolling back the version is easy by using the following command to find the commit ID of the version you want to roll back:

git reflog Copy the code

Then back to version:

git reset --hard ObfafdCopy the code

0bfAFd is the first few digits of the commit ID of the version you want to roll back

4 own remote branch version rollback method

If your error commit has already been pushed to your remote branch, then you need to roll back the remote branch. First we roll back the local branch:

git reflog
git reset --hard ObfafdCopy the code

Then force push to remote branch:

git push -fCopy the code

Note: After the rollback, the version of the local branch will lag behind that of the remote branch. You must use force push to override the remote branch; otherwise, it cannot be pushed to the remote branch

5 Common remote branch version rollback problem

Now that you can roll back the version of the remote branch, you might ask, is there a difference between rolling back the public remote branch and rolling back your own remote branch? The answer is, of course.

One obvious question: What if you fall back on the public remote branch and throw someone else’s commit away?

Here’s the analysis:

Suppose your remote master branch looks like this:

A1, A2, B1

Where A and B represent two persons respectively, A1, A2 and B1 represent their respective submissions. And everyone’s local branch has been updated to the latest version, consistent with the remote branch.

If your teammates pull git pull and their master branch is rolled back, they will see the following message:

$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
nothing to commit, working directory cleanCopy the code

In other words, your teammate’s branch did not actively back up, but committed twice before the remote branch because the remote branch did back up.

(1) At this point, you shout: Guys, I’m back to the version. If your teammates are all god teammates, such as Tony(Tencent CTO), then Tony will calmly use the following command to find his submission that you have overwritten after backtracking, i.e. B1 submission:

git reflogCopy the code

Then calmly move your branch back to that commit and pull a branch:

Git checkout tony_branch git reflog git reflog git reflog Git checkout -b tony_branch -b tony_branch -b tony_branch -b tony_branch -b tony_branch -b tony_branch -b tony_branch -b tony_branch Git reset --hard 0bbbbbb // Go back to the front of your branchCopy the code

Tony: yes, the last commit of tony_backup is B1, and the last commit of tony_backup is B1. Tony: The last commit of tony_backup is B1.

git reset --hard origin/masterCopy the code

Tony’s local maser will look like this when Tony’s master branch is rolled back:

A1

Then Tony will merge the discarded B1 submission again:

Git merge master git merge master git merge master git mergeCopy the code

Tony’s master branch looks like this:

A1, B1

Finally lost B1 to find back, then he push, you can also pull synchronization.

Do the same for all teammates, but if the teammate doesn’t commit and you drop it, you can force the remote master to override the local master after pulling git pull:

git reset --hard origin/masterCopy the code

(2) Unfortunately, in reality, we often meet pigs like teammates, they see the following prompt:

$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)
nothing to commit, working directory cleanCopy the code

Is habitual git push once, or they directly use the graphical interface of SourceTree tool, the sight of the interface is displayed on the push of tip point directly the push button, lie & slot, you worked so hard to rollback version so easily by your pig teammates to restore, so, as long as there is a teammate after push, Remote master changed to:

A1 – A2 – B1

This is distributed, everyone has copies. Now you even have the heart to hit him, how to do? You can’t expect everyone on your team to be a Git expert. Here’s another way to back up versions.

Note: the blogger is experiments in the virtual machine, used to simulate the operation of two people, if you are in A machine, use the same account in different directory cloning two code to experiment, the fallback after remote branch, there was another man who will not see behind the remote branch submitted twice, so be sure to use the virtual machine to simulate the operation of A, B two people

6 Public remote branch version rollback method

Git reset reverses the version of the public remote branch by using git reset, which requires all other hands to override the local master branch by using the remote master branch.

Git revert HEAD~1 git revert HEAD~1 git revert HEAD~1 git revert HEADCopy the code

The git revert command means to undo a commit. It produces a new commit, and even though the code reverts, the version is still forward, so when you use revert and everyone pulls, their code automatically reverts. However, note the following:

  1. Revert undoes a commit, so the commit ID following it is the previous commit to the version you need to roll back to
  2. Using revert HEAD undoes a recent post. If your most recent post was created using the revert command, you can revert the post again. In other words, you can revert the post twice in a row as if it were nothing at all
  3. Use revert HEAD~1 to undo the most recent 2 commits. This number starts at 0, and if you have previously undone a commi ID, it will count.
  4. If you use revert to undo something other than the most recent commit, you can expect a code conflict that requires you to merge the code. Simply remove the current code and keep the previous version.

The nice thing about git Revert is that you don’t lose someone else’s commit. Even if you undo and overwrite someone else’s commit, you can use reset to go back to your own code, pull the branch, and merge it back.

7 Merge code to resolve conflicts

Using the revert command, if you do not undo the most recent commit, you will have a conflict like this:

< < < < < < < HEAD all empty Submitted for the first time = = = = = = = all empty > > > > > > > the parent of c24cde7... All emptyCopy the code

Resolving the conflict is simple because we only want to go back to a certain commit, so we need to remove the current most recent code, which is the code for the HEAD tag:

<<<<<<< HEAD all cleared first submission =======Copy the code

Remove the above part of the code and commit the code again to resolve the conflict.

8 continue to expand, simple and crude rollback methods

You may already think you’ve learned the remote repository version rollback method, but there are a lot of unorthodox problems in practice. Consider the following:

If you are developing, discovers suddenly far away there is a mistake in front of the combined code, combine was next to the function of the code at this time, this time all the members feel directly rolled back faster, because they all have a backup, covered it doesn’t matter, this time use the reset request is higher, in his teammates Do you have to revert a large conflict using revert?

This time, you can use the simple and crude, directly from the wrong submitted before a pull a code in other directories, and then delete all the master code, the party in a new code, and then submit, really simple and crude, although this method does not inflow, however, found that works well in practice, therefore, Practice is the sole criterion for testing truth. Be flexible when you encounter problems. If you have any problems, please leave a message to me, my CSDN blog – “Wutong then rain”.

9 summary

There are three methods for remote branch rollback:

  1. Use reset to roll back your branch directly
  2. Public branch rollback uses revert
  3. If it’s too far wrong, just delete all the code and replace it with the correct code

If you have any problems, please leave a message to me, my CSDN blog – “Wutong then rain”.

10 Reprint please indicate from “wutong then rain” blog:Blog.csdn.net/fuchaosz/ar…

Tips: If you find this blog helpful or like the blogger’s writing style, just follow the blogger or leave a message to encourage the blogger to create more good blogs. Thank you.