git tutorial (version fallback)

Now that you have learned to modify the file, submit the modification to Git version library. Now, practice again. Modify the readme.txt file as follows:

Git is a distributed version control system.
Git is free software distributed under the GPL.

Then try to commit:

$ git add readme.txt
$ git commit -m "append GPL"
[master 1094adb] append GPL
 1 file changed, 1 insertion(+), 1 deletion(-)

Like this, you constantly modify the file, and then submit the modification to the version library. Whenever you think the file has been modified to a certain extent, you can "save a snapshot", which is called commit in Git. Once you change the file disorderly or delete the file by mistake, you can also recover from the latest commit, and then continue to work, rather than losing all the work results of several months.

Now, let's review how many versions of the readme.txt file have been submitted to the Git Repository:

Version 1: wrote a readme file
Git is a version control system. Git is free software.

Version 2: add distributed
Git is a distributed version control system. Git
is free software.

Version 3: append GPL

Git is a distributed version control system. Git is free software
distributed under the GPL.

Of course, in practical work, how can we remember what a file with thousands of lines has changed every time in our mind, otherwise we need version control system to do. There must be a command in the version control system that can tell us the history. In Git, we use the git log command to view:

$ git log
commit 1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master)
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Fri May 18 21:06:15 2018 +0800

    append GPL

commit e475afc93c209a690c39c13a46716e8fa000c366
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Fri May 18 21:03:36 2018 +0800

    add distributed

commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Fri May 18 20:59:18 2018 +0800

    wrote a readme file

The git log command displays the latest and farthest commit logs. We can see three commits. The latest one is append GPL, the last one is add distributed, and the first one is write a readme file.

If you are confused by too much output information, you can try to add the – pretty = online parameter:

$ git log --pretty=oneline
1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master) append GPL
e475afc93c209a690c39c13a46716e8fa000c366 add distributed
eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 wrote a readme file

What we need to remind you is that you see a large string similar to 1094adb The commit id (version number) is different from SVN. Git's commit id is not 1, 2, 3 The incremental number is a very large number calculated by SHA1, which is expressed in hexadecimal, and the commit id you see is definitely different from mine, which is subject to your own. Why does the commit id need to be represented by such a large number? Because Git is a distributed version control system, later we will study how many people work in the same version library. If everyone uses 1, 2, 3 As the version number, it must be in conflict.

Every time a new version is submitted, Git will actually string them into a timeline automatically. If you use the visualizer to view the Git history, you can see the timeline of the commit history more clearly:

OK, now we start the time shuttle and are ready to return readme.txt to the previous version, that is, the version of add distributed. What should we do?

First of all, Git must know which version the current version is. In Git, HEAD is used to represent the current version, that is, the latest submission 1094adb (note that my submission ID is definitely different from yours). The last version is HEAD ^ the last version is HEAD ^ of course, it's easier to write 100 versions up, so it's HEAD~100.

Now, to return the current version of append GPL to the previous version of add distributed, you can use the git reset command:

$ git reset --hard HEAD^
HEAD is now at e475afc add distributed

What is the meaning of the – hard parameter? Let's talk later. Now you can use it at ease.

See if the content of readme.txt is version add distributed:

$ cat readme.txt
Git is a distributed version control system.
Git is free software.

It was restored.

You can still go back to the previous version of write a readme file, but wait a moment. Let's use git log to see the current version library status:

$ git log
commit e475afc93c209a690c39c13a46716e8fa000c366 (HEAD -> master)
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Fri May 18 21:03:36 2018 +0800

    add distributed

commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
Author: Michael Liao <askxuefeng@gmail.com>
Date:   Fri May 18 20:59:18 2018 +0800

    wrote a readme file

The latest version of append GPL is no longer visible! For example, when you came to the 19th century by time shuttle from the 21st century, you can't go back. What about swelling?

In fact, there is still a way. As long as the command line window above is not closed, you can go up and find it. The commit id of the append GPL is 1094adb , so you can specify to go back to a future version:

$ git reset --hard 1094a
HEAD is now at 83b0afe append GPL

There is no need to write all version numbers. The first few digits are OK. Git will find them automatically. Of course, you can't just write the first one or two, because git may find multiple version numbers, so it's impossible to determine which one.

Take a closer look at the contents of readme.txt:

$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.

Sure enough, Hu Hansan is back.

Git's version fallback speed is very fast, because git has a HEAD pointer to the current version internally. When you fallback the version, GIT just points the HEAD from to the append GPL:

┌────┐
│HEAD│
└────┘

└──> ○ append GPL

○ add distributed

○ wrote a readme file
Point to add distributed instead:

┌────┐
│HEAD│
└────┘

│ ○ append GPL
│ │
└──> ○ add distributed

○ wrote a readme file
And then update the workspace file by the way. So if you let the HEAD point to the version number, you will locate the current version.

Now, you go back to a certain version, turn off the computer, and regret the next morning. What do you do if you want to restore to the new version? What if I can't find a new version of commit id?

In git, there are always regrets to take. When you use $git reset --hard HEAD ^ to go back to the add distributed version, and then you want to restore to the append GPL, you must find the commit id of the append GPL. Git provides a command git reflog to record your every command:

$ git reflog
e475afc HEAD@{1}: reset: moving to HEAD^
1094adb (HEAD -> master) HEAD@{2}: commit: append GPL
e475afc HEAD@{3}: commit: add distributed
eaadf4e HEAD@{4}: commit (initial): wrote a readme file

To summarize:
The version that the HEAD points to is the current version. Therefore, Git allows us to shuttle through the history of the version, using the command Git reset -- hard commit "ID.
Before shuttling, you can use git log to view the submission history, so as to determine which version you want to backoff to.
To go back to the future, check the command history with git reflog to determine which version you want to go back to

Published 20 original articles, won praise 2, visited 692
Private letter follow

Tags: git snapshot svn SHA1

Posted on Sun, 09 Feb 2020 20:36:26 -0800 by Ice