@TOC

background

General development of a system are more collaborative development in the company, use Git as code version management tool, that you may or will certainly encounter others wrong code to online, to pull back after others, the code will not change, this thought has been updated, subsequent resubmit, online code is “pollution”, Then roll back again, no one else pulls on the new code, and repeat until everyone gets tired.

The scene again

To prepare

Create a test repository in gitee, and create two directories to simulate the simultaneous development of two people, one is student1, the other is student2, and clone the repository in two folders respectively, as in the picture below

Then we commit and push the mock code four times each time we add a line to the update. TXT file in student1

Pull the code under student2 and keep it as remote as possible

repeat

You can use Reset to delete the third or fourth commit. Reset deletes the third or fourth commit. If you use Revert, you can delete the fifth commit. Resubmit the second submission to Reset the fifth submission

On the command line, yes

# git reset --hard [commit hash]
git reset --hard 7b8bcaa0b02959126b923fe554824fa9df1dfd87
Copy the code

As shown in figure

With git show log, right-click the commit you need to rollback, select “Hard”, and click “OK”

With idea you can do this

After rollback, remember to push to remote

A direct push will cause an error, prompting you to put the local HEAD in front of the remote code, as shown

This time need to git push – f force pushed to the remote, after the success, to go under the student2 pull the latest code, this time many people may think the pull down the code is the remote code after the rollback, in fact is not, after the pull tips below

Strangely, there is no prompt for the code to pull down. If you pull under idea, there is only a prompt for the Already up to date, if you think it has been synchronized with the online, and then continue to write the code to submit, then the submitted code will still be the code before rollback, that is, you changed the remote code back. So what’s the problem?

Identify and solve problems

Student2 use git log –pretty=oneline graph or gitk to check whether the git tree is abnormal

Student1: the git HEAD is local and online, and student2: the remote HEAD is rolling back to the commit on the second commit.

Git reset reset git HEAD

2. Others need to unify local and online heads

So how to solve it? It’s very simple. Use this command

git reset --hard origin/master
Copy the code

Reset the HEAD to be the same as the HEAD of a branch. Origin /master is the remote master

If you use this command, it will delete the code after the second commit. If you use this command, it will be killed. So make sure no changes are being made to hard or backup a copy

With git, right-click the commit of the HEAD and reset it

It’s even easier with IDEA

The last

Code doesn’t lie. When it’s wrong, it’s wrong. There must be a reason for it