1 Perform a local rollback
You made a local error commit, first find the commit ID of the version you want to rollback:
git reflog
Copy the code
Then back to version:
git reset --hard cac0
Copy the code
Cac0 is the first few digits of the commit ID of the version you want to roll back
Fall back to a commit. Commits that fall back to after the specified commit disappear from the commit log.
The contents of both the workspace and the staging area are reset to the time specified for submission. If you do not add –hard, only the HEAD pointer is moved without affecting the contents of the workspace and staging area.
Git reflog rollback rollback rollback rollback rollback rollback rollback rollback rollback rollback rollback rollback
2 Perform remote rollback
2.1 Roll back your Remote Branch
Your error COMMIT has been pushed to the remote branch, so you need to roll back the remote branch.
- First we roll back the local branch:
git reflog
git reset --hard cac0
Copy the code
- After the local branch is rolled back, the version of the local branch will be later than that of the remote branch. You must use forced push to overwrite the remote branch; otherwise, the local branch cannot be pushed to the remote branch.
git push -f
Copy the code
- Note that the correction is
git push -f origin branch_name
2.2 Rolling back a Public Remote Branch
What if you fall back on the public remote branch and throw someone else’s commit away?
I did a lot of stupid things at my former employer HW when I graduated.
Analysis of the
Suppose your remote master branch looks like this:
A1, A2, B1Copy the code
A1, A2, and B1 represent their respective commit owners. The local branch has been updated to the latest version, consistent with the remote branch
If your colleagues pull git pull, their master branch will be rolled back. If your colleagues pull git pull, their master branch will be rolled back.
$ 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 clean
Copy the code
That is, your colleague’s branch did not actively fall back, but committed two times ahead of the remote branch because the remote branch fell back. Unfortunately, in reality, we often encounter 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 clean
Copy the code
Git push is a habit, or they directly use a graphical interface tool such as SourceTree, as soon as they see the interface display is push prompt, they directly click the push button, god, the painstaking rollback version is so easy to restore by your pig like teammates, so, as long as there is a teammate push, Remote master changed to:
A1 – A2 – B1
Copy the code
This is distributed, everyone has copies.
Roll back the version in another way.
3 Public remote rollback
Using Git reset to roll back the version of the public remote branch requires everyone else to override the local master branch with the remote master branch, which is obviously not an elegant way to roll back.
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:
- Revert undoes a commit, so the commit ID following it is the previous commit to the version you need to roll back to
- 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
- 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
- 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.
4 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 clear first submission ======= all clear >>>>>>> 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.