Git (two)-version rollback and partition concept

1. Version rollback:

Git has a "snapshot" function. Whenever you think the file has been modified to a certain extent, you can "save a snapshot". This snapshot is called commit in Git . Once you mess up the file, or delete the file by mistake, you can also recover from the most recent commit, and then continue to work instead of losing all the work results of several months. In Git, we use the git log command to view the history.
For example, we made three changes to readme. The records displayed are from the most recent to the oldest.

$ git log
commit 1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master)
Author: Michael Liao <[email protected]>
Date:   Fri May 18 21:06:15 2018 +0800

    append GPL

commit e475afc93c209a690c39c13a46716e8fa000c366
Author: Michael Liao <[email protected]>
Date:   Fri May 18 21:03:36 2018 +0800

    add distributed

commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
Author: Michael Liao <[email protected]>
Date:   Fri May 18 20:59:18 2018 +0800

    wrote a readme file

If the output information is too much to be dazzled, you can try adding the --pretty = oneline parameter:

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

You see a large string similar to 1094adb ... commit id (version number) , Git's commit id is not 1, 2, 3 ... incremented number, but a very large number calculated by SHA1, using sixteen Hex representation. Why does the commit id need to be represented by such a large string of numbers? Because Git is a distributed version control system, we need to study how many people work in the same version library. If everyone uses 1, 2, 3 ... as the version number, it will definitely conflict.

The preparation is complete, and now we will talk about how to roll back the version:

First, Git must know which version the current version is. In Git, HEAD is used to indicate the current version, which is the latest commit 1094adb ... (note that my commit ID is definitely not the same as yours). The last version is HEAD ^ The last version was HEAD ^^. Of course, writing 100 ^ in the previous 100 versions is easier to count, so it is written as HEAD ~ 100.

Now, if we want to rollback the current version of the append GPL to the previous version add distributed, we can use the git reset command:

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

Check out the content of readme: Sure enough it is restored!

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

Then go back:

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

tips: There is no need to write the full version number, the first few are enough, Git will automatically find it. Of course, you can't just write the first one or two, because Git may find multiple version numbers, and you can't determine which one.

Then the question comes again, let's take an extreme chestnut:
Now, you fall back to a certain version, turn off the computer, and regret the next morning, what if you want to revert to the new version? What if I cannot find the new version of the commit id? ? ? ! ! !

In Git, there are always regret medicines to eat. When you use $ git reset --hard HEAD ^ to fall back to the add distributed version, and then 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 every command you have:

$ 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

Comfortable ... From the output, the commit id of the append GPL is 1094adb. We can easily go back

Two: working area and temporary storage area:

  1. The Working Directory
    is the directory you can see on your computer. For example, my learngit folder is a working area:
  2. The Repository
    workspace has a hidden directory .git, which is not a workspace but a Git repository. There are many things stored in the Git repository, the most important of which is the temporary area called stage (or index), and the first branch master that Git automatically creates for us , and a pointer to the master called HEAD .

Insert picture description here

  1. The concepts of branches and HEAD will be discussed later.

Earlier, when we added the file to the Git repository, it was executed in two steps:

The first step is to use git add to add the file, in fact, it is to add the file modification to the staging area; the
second step is to use git commit to commit the changes, in fact, to commit all the contents of the staging area to the current branch.
Because when we created the Git repository, Git automatically created the only master branch for us, so now, git commit is to commit changes to the master branch.

You can simply understand that the file changes that need to be submitted are all placed in the temporary storage area, and then all the changes in the temporary storage area are submitted at once.

Now let's practice and familiarize with the process:
(1) First, make a modification to readme.txt, for example, add a line:

Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.

(2) Then, add a LICENSE text file in the workspace (write the content randomly). And use git status to check the status:

$ 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

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	LICENSE

no changes added to commit (use "git add" and/or "git commit -a")

Git told us very clearly that readme.txt was modified, and LICENSE has never been added, so its status is Untracked.

(3) Now, after using the git add command twice to add both readme.txt and LICENSE, check again with git status:

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   LICENSE
	modified:   readme.txt

Now the temporary storage area is sauce purple:

Therefore, the git add command is actually to put all the changes to be submitted to the temporary storage area (Stage), and then, execute git commit to submit all the changes of the temporary storage area to the branch at once.

After commit: check the status with git status:

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

Insert picture description here

That's all for today! In addition, the concept must be clearly understood, all operations are based on principles and concepts

Published 13 original articles · won 2 · views 231

Guess you like

Origin blog.csdn.net/qq_25871537/article/details/105501688