Distributed version management system---->Git(Linux---centos (nanny style) explanation 1)

Article directory:

        1: What is Git and its functions

        ​ ​ 2. The basic operation process of Git (create git warehouse, configure the configuration of the warehouse)

        3.The relationship between git workspace, staging area and version library

        4. Add files to the repository: git add and git commit -m commands

        5.Introduction of git log to view logs

        ​ ​ 6. View the contents of the .git file

        7. Modify the file content and view git diff and git diff HEAD

        8. Introduction of the concept and commands of version rollback

        9. Introduction of undo modifications and related command operations

        10.git rm deletes the repository file


Foreword:

        This article explains the related operations of Git. It provides a deep understanding of the operation process and operations of Git, masters the enterprise-level application of Git, and explains Git from scratch.

The article officially begins:

1: What is Git and its role

        First, before explaining what Git is, let’s talk about a scene in our work:

        When we work every day, our boss may ask us to write a requirements document (Document 1), and after we finish writing Document 1, we may hand this Document 1 to the boss, but our boss is not interested in our Document 1. Why are we very satisfied and feel that our document is missing something, so we take this document to modify it, so we take the file to modify it. At this time, we modify document 2 and submit this document. Give it to the boss. The boss may feel that he is still not very satisfied with this document, so he asks us to modify the document... Suppose we finally modify the 10th version of the document, but the boss feels that our fifth document is the best It met his needs, so we went to find document 5. If we had not saved document 5 before, we would be very upset because we had modified the document many times and did not know what document 5 was, but we If what is modified is not the same file but a new copy of the document is created every time it is modified, then we will happily hand the fifth document to the boss.

                From the above work scenario, it is not difficult to find that there are two ways when we modify files. One is to modify the same file multiple times, and the other is to modify a copy of the file to be modified. Obviously, the second situation is A better modification method, because this modification method allows us to find the version of the file we modify each time.

In order to manage our files, we have git, one of the mainstream version management controllers.

So we can simply understand Git as: a version control system that manages files, which can help us know the various versions of this file and the contents inside. The files here can be in any format, and for developers, the biggest role of git is to manage our source code.

2: Basic operations of Git (operations under centos)

        1: First we need to install Git under our centos system

        Order:

root用户:yum install -y git
普通用户:sudo  yum install -y git

     Create a git repository

      The first thing to note is that our created warehouse must be created in a directory

    

        Create a git repository:git init

        


        Configure the warehouse

   

git config user.name "xxxxx" 

git config user.email "xxxxx" 

         

First of all, before configuring the name and email address, we can check whether git is configured. If it is configured, we do not need to configure it. Generally speaking, if we have just started to build a git warehouse, we need to configure it.

View git configuration itemsInstruction: git config -l

 

So let’s configure name and email

        

  We can also add a --global option to configure these two, such as:

        git config --global user.name "    "

        git config --global user.email "    "

Whether to add --global depends on your own wishes.


Deletion of configuration items

        Suppose our configuration items are accidentally misconfigured or we need to modify the configuration items of a git warehouse, then we can use the following instructions:

        git config  --unset user.name(user.email) For local configuration

        git config --global --unset   user.name(user.email) For global configuration

        


3: Introduction to the three major areas of git

        In fact, what our git actually manages is the files located in the repository, but the files in the git warehouse directory cannot be managed by git, and they need to be added to the git repository before they can be managed.

We introduce several concepts through the above graphics

        Workspace: The area under the same level path of the .git file. A simple understanding is the area where we write code.

        Staging area: It is a very important content in the .git repository. After we pass the content of the workspace through the add command, the files will be placed in this area.

       Git repository: It is our .git directory.

        HEAD: Essentially a pointer pointing to the branch we are currently on.

        Master: The essence is the commit id.

        Object object library: When we modify the workspace, the modified content will be stored in the object object library.

        

The .git file contains some of the above areas.


4.git add git commit -m 

        ​ ​ ​ We already have a certain understanding of the git area above, so we can introduce two instructions.

        git add  .  or git add filename

        First of all, this command is for the workspace and temporary storage area.

        The meaning is: Add the modified content of our workspace to the temporary storage area.

        “Modification”: is not limited to modification of files but also includes deletion and creation of files

        The difference between git add . and git add filename

        add.: Means All modifications of the working area are added to the temporary storage area.

        Add Filename: Add The repair of the file to the temporary storage area.

        ​ ​ ​ For example, we first use the above commands to perform related operations:

        git status

        This command can be used to view the status of our three current workspaces

         


        git commit -m "xxxx" ""In the semicolon, it is recommended to carefully write down what operations you have done this time

        git status View the status of the three areas 


5:git log Instructions for viewing logs

        This command can be used to view the logs we submitted to the repository

You can also use simple and beautiful printing instructions

git log --pretty=oneline


6. Introduction to the contents of the .git file

        .git is actually a tree directory with many files or directories in it. We use the tree command to view it under Linux.

        Index: This is what we call the temporary storage area, which contains the content modified by add.

        HEAD: This is what we call a pointer, which contains the branch we point to.

        Object: It is called the object library, which contains the index of the content that we modify every time we modify the workspace.

        Master: In fact, it is our latest commit id. We can take a look.


7. View modified content

        We know that git is a version control management system, so the most important thing git manages is modifications, not files.

        We first introduce a few scenarios

        ​ ​ 1. Only the content of the file is modified, no add.

        In this case, if we git wants to know the contents of this file and the last time The difference in the content of the fileHow can one of us view it? ? 

       Of course, we may be able to know the difference between the two files through our memory, but will we still remember it after a long time? ?

So our git provides a command to display the difference between the work area and the staging area.

        git diff filename

We demonstrate through pictures.

        Scenario 2: The modified file has been added, and you want to see the difference between the file in the staging area and the version library

        git diff HEAD -- filename

        At this point, the difference between the staging area and the version library comes out.


8. Version rollback

        We know that an important function of our git is to perform version management on files, so version rollback is a very important knowledge of git.

        Version rollback is often used. If we have been developing for a period of time, but we no longer want our development results very much, and want to re-develop under the previous version, then our version rollback will be The operation is very necessary.

    Version rollback syntax:git reset [--soft] [--mixed] [--hard] commitid [HEAD]

        commitid: It is the numbers after commit when we use git log to print. Of course, commitid can also beHEAD, which means a>. Current version

        The essence of rollback: Roll back the contents of the repository. Whether to roll back to other areas depends on the rollback option. 

        ​ ​ ​ We use pictures to explain the meaning of specific options.

--soft: Only roll back the content in the repository.

--mixed: Both the repository and the staging area will be rolled back, which is also the default option of git reset.

--hard: Roll back the content of all areas, use with caution.


        At this point, assume that we have already developed it once and noted "coding x function"

        And the content of the file is:

        We don’t want the results this time, we want to go back to the previous version.

        At this point you can use the version rollback function provided by git.

The content of the test at this time

In this way, all the contents in the workspace, staging area, and version library will be returned to the previous version.

        From here we can also find that git version rollback is very fast. This is because rollback actually just changes the master of the HEAD pointer, so the modification speed is very fast.


     git reflog can view all our commit ids, which allows us to return to that version at will.

        But it is not recommended to roll back casually, as the commit id may be washed away.

        We use diagrams to explain the essential content of git version rollback

        

        

9. Undo modified files

         The undo modification here refers to the corresponding operation when we operate on a file. That is to say, we feel that the code we developed this time is very bad and want to return to the state at the beginning.

        ​ ​ 1: Undo the code in the workspace, no add

                Method 1: Modify manually. This method is too frustrating and will not be demonstrated here.

                Method 2: git checkout -- [filename], go back to the most recent timeThe add status and commit status of .

            

                


        ​ ​ 2. The add operation has been performed, but there is no commit operation

        

        Method:git reset HEAD filename +git checkout -- [filename] Two steps

 


        3. Already added, already committed

        We can only rely on our git reset --hard/mixed/soft commitid for undoing at this time, and the version will be rolled back.

        I won’t give you a demonstration here.


        10. Delete files in the repository

        命令:git rm filename

                We explain git rm through specific examples

        We first create file1, file2, and file3, and then delete them.

        

 

        At this time, use git status to view the content

        We can completely delete the file with just a git commit operation.

        Summarize the two steps of using git rm to delete files in the repository: git rm filename +git commit -m " xxx"


       How to recover files that we accidentally deleted in the workspace?

        You can use the undo operation here, because deletion is essentially a modification.

        


        This article is finished! ! ! ! Thank you everyone for your patience.

Other content will be introduced in subsequent articles. If you think it is useful to you, you can give it a like.

Guess you like

Origin blog.csdn.net/2201_75964502/article/details/134799045
Recommended