Skip to main content

GitReset

AndyBinOriginalAbout 2 min

Conclusion

Git workspace status:

git status

To view all modifications:

git diff

To view the modified contents of the specified file:

git diff <file>

Fallback to the specified version

git reset --hard commit_id

Go back to the previous version

git reset --hard HEAD^

Go back to the version before last

git reset --hard HEAD^^

Go back to the last n versions

git reset --hard HEAD~n

View detailed submission history

git log

View simplified submission history

git log --pretty=oneline

View Branch Merge Graph

git log --graph

View Command History

git reflog

Caption

We modify files for many times. If we accidentally delete something, we can use version rollback to restore it.

Step

Continue the content of the previous section, modify readme.txt, add a line, and execute:

git status

The results are as follows:

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
        modified:   readme.txt
no changes added to commit (use "git add" and/or "git commit -a")

git status command allows us to keep track of the current status of the warehouse. The above command output tells us that readme.txt has been modified but has not yet been submitted.
At this point, if you want to see which part of readme.txt we have modified, execute:

git diff

The results are as follows:

$ git diff
diff --git a/readme.txt b/readme.txt
index cc3b095..6b77a0b 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,3 @@
 git is very famous!
-Content added for the first time
\ No newline at end of file
+Content added for the first time^M
+Content added for the second time
\ No newline at end of file

Knowing what has been modified, you can submit it with confidence,Execute git add git commit -m "" in sequence,submission completed,execute git status to view the status, as shown below:

$ git status
On branch master
nothing to commit, working tree clean

OK, after multiple submissions, if you want to return to a version, first execute:

git log

The results are as follows:

$ git log
commit f09d57ce853e850551e8802b9a4be3643ba894c0 (HEAD -> master)
Author: rumosky <rumosky@163.com>
Date:   Sun Nov 3 16:02:23 2019 +0800
    Content added for the third time
commit c3b8908ddddd8364ac8b2681b56e948885e49b1d
Author: rumosky <rumosky@163.com>
Date:   Sun Nov 3 16:00:36 2019 +0800
    Content added for the second time
commit a82d91a6bb97b1acc158d98bc1f82697df938e3b
Author: rumosky <rumosky@163.com>
Date:   Sun Nov 3 15:49:55 2019 +0800
    Content added for the first time
commit 69997611303057230d8fa50c81681bd823644553
Author: rumosky <rumosky@163.com>
Date:   Sun Nov 3 15:28:27 2019 +0800
    First submission

You can see that there are four submissions, in which the string of characters after commit is commit_id. If you think the log content is too long, you can add the parameter --pretty=online. The result is as follows:

$ git log --pretty=oneline
f09d57ce853e850551e8802b9a4be3643ba894c0 (HEAD -> master) Content added for the third time
c3b8908ddddd8364ac8b2681b56e948885e49b1d Content added for the second time
a82d91a6bb97b1acc158d98bc1f82697df938e3b Content added for the first time
69997611303057230d8fa50c81681bd823644553 First submission

Back to the previous version, the results are as follows:

$ git reset --hard HEAD^
HEAD is now at c3b8908 Content added for the second time

Now, we have returned to the previous version, but what if we regret and want to restore to the new version?
It's OK. At this point, execute git reflog to find the latest commit_id, the result is as follows:

$ git reflog
c3b8908 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
f09d57c HEAD@{1}: commit: Content added for the third time
c3b8908 (HEAD -> master) HEAD@{2}: commit: Content added for the second time
a82d91a HEAD@{3}: commit: Content added for the first time
6999761 HEAD@{4}: commit (initial): First submission

Third append commit_id is f09d57c. Execute the rollback command. The result is as follows:

$ git reset --hard f09d57c
HEAD is now at f09d57c Content added for the third time

At this time, check the readme file and find that it has been restored:

$ cat readme.txt
git is very famous!
Content added for the first time
Content added for the second time
Content added for the third time

Tips

commit_id is not necessary to enter all the IDs. You can find the commit by entering at least the first four digits

Last update:
Contributors: rumosky
Comments
  • Latest
  • Oldest
  • Hottest