Creating version library and common command operation of git through time and space

Posted by m3rajk on Tue, 08 Mar 2022 08:44:28 +0100

git is an open source distributed version control system, which can effectively and quickly handle the version management of projects from very small to very large. git's powerful branch management far exceeds that of SVN. That git
How to create a version library? How to perform some common operations? For the answer, see the breakdown below.

**Create version Library**

When I was a child, I saw a very impressive play called love through time and space. Today, I have the cheek to direct a git through time and space. It's not released. It's pure self hi!

what is
Version library? No matter how often we usually use git, small partners in the IT industry must often hear people say that which cow X project is in which warehouse, Which project's warehouse was maliciously added with issue (I heard that the issue in the Apollo program's code warehouse was occupied by many irrelevant Chinese comments ~ hey, our test engineers must not do this and do their part for this industry);

After talking so much nonsense, in fact, the warehouse we usually talk about is the version library. The version library is also known as the warehouse, and its English name is repository. I believe many children who use GitHub often see this word;

I simply understand it as a directory folder, which can put all kinds of files. Like normal use management, it can be deleted and modified; But Git's "folder" is a little bigger. It has a good memory-
Git can "remember" the whole management history and "archive".

Note: in fact, all versions of Git files can only be tracked, such as the version of Git files and the code of Git.

How to create a version library is simple:

  • 1) Select a path (you like just ok!), Create an empty directory
$ mkdir mygit$ cd mygit$ pwd/Users/qinzhen/mygit  #Here is the warehouse address created on my Mac
  • 2) After the warehouse address is created, it is now an ordinary folder. Then use the git init command to turn it into a "Git folder", that is, turn this directory into a warehouse that Git can manage

$ git initInitialized empty Git repository in /Users/qinzhen/mygit/.git/

It will prompt that an empty git repository has been created in your directory and there is one more in your directory Git, even if the creation is completed at this time.

**Common command operation**

****2.1 submittals

Before reading the command, let's use a picture to understand several concepts, which can be easily understood and remembered:

  • Working Directory

  • Temporary warehouse (Staging Area)

  • Git Repository

We just edit files in the workspace first; Then add to the temporary warehouse, you can add multiple; Finally, submit it to the warehouse together with the commit.
Then officially enter the practice:

1) Under the mygit path, create the file readme Txt, enter the following:

I am a test engineer.I want to study Git

2) Use git add command to submit the file to the temporary warehouse (if it runs successfully, no message will be output. Don't panic if you can't see the output ~)

$ git add readme.txt

3) Then use git commit command to submit the file from the temporary storage area to the warehouse

localhost:mygit qinzhen$ git commit -m "study git"[master 64f5ced] study git1 file changed, 2 insertions(+), 2 deletions(-)

Command interpretation:

-m : Add notes and write a description of this submission 1 file changed : 1 A file has been changed, that is readme.txt;2 insertions(+): 2 Line inserts the content, that is, the two lines we entered 2 deletions(-): 2 The content of the line is missing because I put readme.txt The original content in the file is added after being deleted, so there will be a change prompt. If it is completely new, there will only be added records

2.2 diff

  • 1) Let's use git status to check the status of the current warehouse

localhost:mygit qinzhen$ git statusOn branch masternothing to commit, working tree clean

Now it means that there is nothing to submit on a master branch, and the working directory is currently clean

  • 2) Modify readme Txt file:

I am a test engineer.I want to study Git very much.
  • 3) Then use git status to view

localhost:mygit qinzhen$ git statusOn branch masterChanges 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")

At this time, you can see that the file has been modified, but it has not been add ed or commit ted;

  • 4) Then use the git diff (different) command to see the difference between the current operation and the previous operation and where it has been modified:

localhost:mygit qinzhen$ git diffdiff --git a/readme.txt b/readme.txtindex 225e15a..8080bd8 100644--- a/readme.txt+++ b/readme.txt@@ -1,2 +1,2 @@ I am a test engineer.-I want to study Git.+I want to study Git very much.

You can see that I added very much in the second line

2.3 back to the past (withdrawal and fallback)

  • 1) First, we will submit the modified document to the warehouse:

$ git add readme.txt$ git commit -m "very much"[master 7582a45] very much 1 file changed, 1 insertion(+), 1 deletion(-)
  • 2) Let's use the git log command to see what we have done before:

$ git logcommit 7582a45a1acd9f5540f381d6e9bb7c9d38e74348 (HEAD -> master)Author: qinzhen <376057520@qq.com>Date:   Thu Sep 19 16:48:31 2019 +0800  
    very much  
commit 64f5cedd48745267e4e161c57f126a9230344339Author: qinzhen <376057520@qq.com>Date:   Thu Sep 19 16:05:40 2019 +0800  
    study git

After the git log command, we can see the previous commit history. Each commit is assigned a unique commit
id, this id is the key for us to go back to the past, which is equivalent to going back to a certain period in science fiction movies

3.1) HEAD -- now, if I want to go back to the "period" of study git, I can use git reset --hard HEAD^

$ git reset --hard HEAD^HEAD is now at 64f5ced study git  
$ cat readme.txtI am a test engineer.I want to study Git.

At this time, we can see that we have successfully returned to the "period" of study git, and very much has disappeared

Command interpretation:

If it's hard to understand the past, it can also be understood as archiving. When I was a child, I liked to play a computer game called red alert. When playing task war, I would archive it once before passing each level, so that I can return to the designated level and start again after "death"^Represents the last archive,^^Represents the last archive. If you want to reverse the last 100 versions, you can use it directly HEAD~100 Go back to the past

3.2) commit id -- now modify the file, add I can fly in the first line, and then add and commit:

I am a test engineer,I can fly.I want to study Git.  
$ git add readme.txt$ git commit -m "I can fly"[master 9c32701] I can fly 1 file changed, 1 insertion(+), 1 deletion(-)

git log view the modification submission log

$ git logcommit 9c327016eec10a6db7f9b75ecb705df417b6508c (HEAD -> master)Author: qinzhen <376057520@qq.com>Date:   Thu Sep 19 17:06:10 2019 +0800  
    I can fly  
commit 64f5cedd48745267e4e161c57f126a9230344339Author: qinzhen <376057520@qq.com>Date:   Thu Sep 19 16:05:40 2019 +0800  
    study git  

It is said that you can use the commit id to go back to the past. Now let's try it; Use git reset --hard 64f5ce Command:

$ git reset --hard 64f5cedHEAD is now at 64f5ced study git  
$ cat readme.txtI am a test engineer.I want to study Git.

From the above results, we can see that we have successfully returned to the past through the commit id, back to the era when there was no "I can fly"; it can be noted that we are writing commit
The id is not written completely, but only the front part is written, and git can be found;

Um... There must be someone who wants to ask. To tell you the truth, I don't know 0.0, and I haven't specially studied it. The top four or five? Six or seven? Seven or eight? It's good to meet the use requirements. If you can ensure that the id is unique, just copy a few more...

3.3) go back to the era before add - only the add file reaches the temporary storage area, and there is no commit; It seems that you have entered another space of add and can't find him in the future. If you want to return to the original "period", you can use git
reset HEAD throw away the file

Now my readme Txt file is as follows, and has been add ed to the temporary storage area:

I am a test engineer.I can fly!I want to study Git.  
$ git statusOn branch masterChanges to be committed:  (use "git reset HEAD <file>..." to unstage)  
  modified:   readme.txt  

Use git reset head readme The txt command undoes the changes from add

$ git reset HEAD readme.txtUnstaged changes after reset:M  readme.txt$ git statusOn branch masterChanges 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")  

3.4) there is another question at this time. I just put readme Txt is revoked from the temporary storage area after add, and the file has not been changed; If you want to withdraw the modification of the file, you need to use git checkout -- < File > command:

$ git checkout -- readme.txt  
$ cat readme.txtI am a test engineer.I want to study Git.  
$ git statusOn branch masternothing to commit, working tree clean  

Add: a file has been submitted to the version library. Sometimes we delete a file by mistake in the workspace (rm or manual). At this time, there is still this file in the version library. You can "retrieve" it through git checkout -- < File > command;
If you really want to delete files from the version library, you need to use git rm and git commit commands

2.4 return to the future

1) That's right. In general, you will always find a way to return to the future at the end of the movie. git is no exception. In addition to going back to the past (back to the version), you can also go back to the future (back to the new version);

Here we still need to do it with the help of commit id, but after fallback, we can't see the positioning coordinates of the future period with git log (commit id):

$ git logcommit 64f5cedd48745267e4e161c57f126a9230344339 (HEAD -> master)Author: qinzhen <376057520@qq.com>Date:   Thu Sep 19 16:05:40 2019 +0800  
    study git

2) At this time, you need to use the git reflog command to record your previous journey:

$ git reflog64f5ced (HEAD -> master) HEAD@{0}: reset: moving to 64f5ced9c32701 HEAD@{1}: commit: I can fly64f5ced (HEAD -> master) HEAD@{2}: reset: moving to HEAD^7582a45 HEAD@{3}: commit: very much64f5ced (HEAD -> master) HEAD@{4}: commit: study git

Here you can see the commit id of the previous I can fly; By the way, it is said that the commit id does not need to write all but the first part, but I'm not sure how many bits to write. Here, I see that the git log only records the first seven bits, so let's count them as seven...

With the commit id, you can get the coordinate information and methods to return to the future. The movie is almost at the end of the finale. This article is about to end. Let's finish:

$ git reset --hard 9c32701HEAD is now at 9c32701 I can fly  
$ cat readme.txtI am a test engineer,I can fly.I want to study Git.  
$ git statusOn branch masternothing to commit, working tree clean  

The sci-fi film "Git through time and space" is still being updated. Please look forward to it~

Previous articles

Welfare|
Learn Jenkins to deploy the test environment by himself, so that your salary is high to fly

How to build the unit test framework? What is the magic of the new version of JUnit 5?

Based on Junit4, use xUnit framework to greatly improve the maintainability of your test cases

Video at the end of the article|
From PO design pattern to Appium source code analysis

**
Come to Hogwarts test and development society to learn more advanced technologies of software testing and test development. The knowledge points include web automated testing, app automated testing, interface automated testing, test framework, performance testing, security testing, continuous integration / continuous delivery / DevOps, test left, test right, precision testing, test platform development, test management, etc, The course technology covers bash, pytest, junit, selenium, appium, postman, requests, httprunner, jmeter, jenkins, docker, k8s, elk, sonarqube, Jacobo, JVM sandbox and other related technologies, so as to comprehensively improve the technical strength of test and development engineers

Click for more information