Recently, there is a news that the domestic Gitee has been nominated by the Ministry of Industry and Information Technology. It is possible that the domestic programmers will turn to Gitee hosting code in the future.

However, whether you use Gitee or Github, you will always use Git, and I personally think it is hard for you to replace Git with another tool.

Git’s use is fairly common in the programmer community these days, but some git operations may not be easy to understand, especially since the documentation begins with a bunch of commands that are explained, whether or not they will be used in most cases.

This can make git students confused. I want to see how a command works and what all the parameters are for.

In this article I’m going to talk about how to revert in Git. I’m going to talk about how to revert in Git. I’m going to talk about how to revert in Git.

Since I’m focusing on Revert, you probably know a little bit about Git, and if you don’t, this article isn’t for you.

First let’s assume we have a commit history that looks like this:

A1 ---> A2 ---> A3 ---> A4 ---> A5 ---> A6
Copy the code

The current latest commit is A6, and git’s HEAD points to A6.

If you are not sure, you can run a Git log in your repo to see if your HEAD is pointing to your latest commit.

The scene of a

What if one day you find that neither A4 nor the subsequent commit is available?

Case one

If your commit has not yet been pushed to a remote repository, and you don’t want any of the content you didn’t commit locally, you can do this:

git reset --hard HEAD~3 
Copy the code

What is this HEAD~3?

A1 ---> A2 ---> A3 ---> A4 ---> A5 ---> A6

5 --> 4 --> 3 --> 2 --> 1 --> 0Copy the code

Okay? Starting with HEAD, sequence number.

Note: the hard parameter clears all uncommitted content and changes the commit history, i.e., A4, A5, and A6 histories. (Probably not a good idea in most cases, but as a personal project or experimenting with things on a branch yourself, this is appropriate because the commit history of the experiment isn’t worth much, it’s probably just a bunch of WiP)

The second case

If your commit is already pushed to a remote repository, or you don’t want to change your commit history, git revert is the best option.

For our example you can do this:

git revert --no-commit HEAD~2^.. HEADCopy the code

or

git revert --no-commit HEAD~3.. HEADCopy the code

We want to revert A4 to A6, HEAD~2^.. HEAD or HEAD ~ 3.. The HEAD.

m.. HEAD~2^ is the parent of HEAD~2, so it is equivalent to HEAD~3.

“No-commit” means that you don’t automatically commit after revert, which means that after you run this command, your repO will revert that code, and then it will be uncommitted. I like this because before you commit, Clearly see if it’s the code you want to revert.

If everything is ready to commit your revert, the current commit history looks like this:

A1 ---> A2 ---> A3 ---> A4 ---> A5 ---> A6 ---> B
Copy the code

Yes, your A4, A5 and A6 are still in the history. The new B commit is the result after you revert A4, A5 and A6, which is actually the status of A3, but the history is kept.

An equivalent form of revert is:

git revert --no-commit HEAD HEAD~1 HEAD~2
Copy the code

Specify commit directly.

Scenario 2

The first scenario is a context for revert, but there is another scenario where you can just use git to revert. If you want to just revert A5, you can do this:

git revert --no-commit HEAD~1
Copy the code

This will result in a new COMMIT that you can revert after A5.

A1 ---> A2 ---> A3 ---> A4 ---> A5 ---> A6 ---> C
Copy the code

C is what happens after A5 is removed.

conclusion

Two scenarios cover most work scenarios. If you have more complex requirements, you may not need to read my article. For simple requirements, rather than being confused by various parameters, it is better to remember this command, and remember the effect of these commands after execution, so as to achieve safety and control.

I know you may think that I didn’t explain the details of the command, but according to my experience, for many beginners, when you begin to explain the details, all in order to simplify and novice understanding do before summary, will slowly be so-called in detail to confused, so, when you don’t understand a thing, just remember I command, remember it, use it. Don’t worry about other first, use it, time is long, go to see details again, the effect will be much, much better.

– EOF –

When you are lost

A cup of coffee, a thousand lines of code

Welcome to pay attention to the public number: a cup of code