Categories
Uncategorized

[Original] how to undo git commit (not push)

Revocation commit general use git reset, syntax is as follows:

git reset [ --mixed | --soft | --hard] []

1. Use parameter –mixed (default parameters), such as git reset –mixed or git reset

Undo git commit, undo git add, change the code editor reservations

2. parameters –soft, as git reset –soft

Undo git commit, not revoked git add, change the code editor reservations

3. –hard parameters, such as git reset –hard – This approach is very violent, all revoked, caution

Undo git commit, undo git add, delete change the code editor

The beginning of the revocation example commit:

Enter the git log, we can see that the last three commits, the most recent submission is test3, the first one was test1, where a bunch of yellow letters commit id (version number)

If too much output, can be added –pretty = oneline parameters, namely

git log --pretty=oneline

 

Next, press the letter q on the keyboard to exit the git log, ready to withdraw commit

Git must know which version is the current version, in Git, with HEAD represents the current version, which is the latest submitted commit id, the previous version is the HEAD ^ (or HEAD ~ 1), the previous version is the HEAD ^^ (or HEAD ~ 2), empathy N versions up to write the N ^ is not realistic, we write HEAD ~ 100.

Now, we want the current version of a version test3 test2, you can use git reset command: git reset –hard HEAD ^, and then view the log, it has been found to commit the remaining 2 version

git reset --hard HEAD^

The latest version of the test3 could not see, at this time you think there is a line of code is well written, I would like to look back version test3, how do?

Way but it is still there, as long as the above command line window has not been turned off, or in the recording, use the command git reset –hard commit_id can fall back to the specified version, such as the current example, we find that commit id is test3 e09af7ae711e2a79c15144c1e792fb2e27d201ff, and then enter the following instruction on the back

git reset --hard e09af7ae711e2a79c15144c1e792fb2e27d201ff

The version number can not write the whole, before entering four or more are possible, Git will automatically find, can not write only the first one or two, because Git may find more than one version number. For example, after reading test3 version of the code, and now want to roll back to the original version test1, test1 we find that the commit id is ab7b0c2b6e10a20d524156a81f6d4bc15a4ea7f3, then enter the following command to back

git reset --hard ab7b

Finally, in Git, there is always regret it can eat, Git provides a command git reflog used to record your every command, which can be found by each commit id, allowing you to move forward or fall back to the specified version

 

Leave a Reply