Lite Git (III) - First Commit

Keywords: git

Lite Git (III) - First Commit

preface

This column is called Lite Git. I mainly want to correspond to Pro Git, which is the official guide of Git. If you are interested or want to know more details, please move on Download PDF version from the official website.

The purpose of this column is to enable the novice students to master the basic application of Git faster and more reasonably;

At the same time, this column will also introduce what Android developers care about: the use of repo;

This is the third part of this column, which mainly introduces the structure division of Git warehouse. It belongs to a more important section. Students who have just begun to understand Git strongly recommend to understand it;

After understanding this chapter, the subsequent git command learning is easy.

After that, the first submission will be completed, and the understanding of Git warehouse structure will be further deepened in the process;

Structure division

Before introducing the use steps, I think the following concepts need to be introduced first, which is helpful for the memory and understanding of the following commands;

The concepts of working directory and git repository have been mentioned earlier. In fact, there is also a Staging Area under the conventional git warehouse (i.e. non bare warehouse). Then the introduction of these three concepts is as follows:

  • working directory workspace, that is, the working area after checking out the file content hosted by git warehouse, can be freely viewed, edited and changed by users. In most cases, it is equivalent to removing the contents other than the. Git directory. There is no workspace under the bare git warehouse;
  • In most cases, the git repository itself is equivalent to the. git directory (non bare warehouse, and the root directory of the latter is the warehouse itself). It has a set of management, compression and indexing mechanisms. For beginners, there is no need to pay too much attention (I will mention it later if there is time);
  • Staging area I'm used to calling it staging area (this will be maintained later). In git, it is managed through a file called index (so index mentioned in some places also refers to this). It is mainly used for the intermediate state records of some version control processes, usually the records of some contents to be submitted. This part is difficult to understand, but it is very helpful for playing with GIT. It will be explained in detail later;

To deepen your memory, use the demo in the previous section_ Client warehouse to explain:

# demo_client is a regular warehouse (non bare warehouse)
ryan ~/git_demo/demo_client (master) $ ll
total 12
drwxr-xr-x 3 ryan ryan 4096 Oct 27 13:36 ./
drwxr-xr-x 5 ryan ryan 4096 Oct 27 13:36 ../
drwxr-xr-x 7 ryan ryan 4096 Oct 27 13:36 .git/
ryan ~/git_demo/demo_client (master) $

At this time:

  • The workspace is empty (but it does not exist, and the workspace under the git bare warehouse does not exist);
  • The warehouse is not empty (the structure exists, but the content is empty);
  • The staging area is empty (no index);

Submit modification

Due to the current demo_client and remote demo_ None of the bare warehouses track any files, so the warehouse is empty.

Now, with the purpose of "completing the first submission", we will gradually introduce the situation of different regions in each stage, so as to deepen your understanding of these three regions:

  1. First, we are in the demo_ Create a file FileA (Linux Environment) in the client directory:

    ryan ~/git_demo/demo_client (master) $ touch FileA
    ryan ~/git_demo/demo_client (master) $ ll
    total 12
    drwxr-xr-x 3 ryan ryan 4096 Oct 27 13:44 ./
    drwxr-xr-x 5 ryan ryan 4096 Oct 27 13:36 ../
    drwxr-xr-x 7 ryan ryan 4096 Oct 27 13:36 .git/
    -rw-r--r-- 1 ryan ryan    0 Oct 27 13:44 FileA
    
  2. Then, we can view git status through git status:

    #Check the. git warehouse. There is still no index file
    ryan ~/git_demo/demo_client (master) $ ll .git/
    total 40
    drwxr-xr-x 7 ryan ryan 4096 Oct 27 13:45 ./
    drwxr-xr-x 3 ryan ryan 4096 Oct 27 13:44 ../
    -rw-r--r-- 1 ryan ryan   23 Oct 27 13:36 HEAD
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 branches/
    -rw-r--r-- 1 ryan ryan  254 Oct 27 13:36 config
    -rw-r--r-- 1 ryan ryan   73 Oct 27 13:36 description
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 hooks/
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 info/
    drwxr-xr-x 4 ryan ryan 4096 Oct 27 13:36 objects/
    drwxr-xr-x 4 ryan ryan 4096 Oct 27 13:36 refs/
    
    #git status can view the file status of the current warehouse
    ryan ~/git_demo/demo_client (master) $ git status
    On branch master
    
    No commits yet
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            FileA
    
    nothing added to commit but untracked files present (use "git add" to track)
    

    It can be seen that git found FileA, but said it did not track the file (Untracked)

    So at this time:

    • The workspace is not empty (with FileA);
    • The warehouse is not empty (the structure exists, but the content is empty);
    • The staging area is empty (no index);
  3. Next, if we want to submit the FileA to the git repository, we need to add it to the staging area using the git add command:

    #Add FileA to staging area
    ryan ~/git_demo/demo_client (master) $ git add FileA
    #Check the. git directory and find the index file
    ryan ~/git_demo/demo_client (master) $ ll .git/
    total 44
    drwxr-xr-x 7 ryan ryan 4096 Oct 27 13:52 ./
    drwxr-xr-x 3 ryan ryan 4096 Oct 27 13:44 ../
    -rw-r--r-- 1 ryan ryan   23 Oct 27 13:36 HEAD
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 branches/
    -rw-r--r-- 1 ryan ryan  254 Oct 27 13:36 config
    -rw-r--r-- 1 ryan ryan   73 Oct 27 13:36 description
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 hooks/
    -rw-r--r-- 1 ryan ryan  104 Oct 27 13:52 index
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 info/
    drwxr-xr-x 5 ryan ryan 4096 Oct 27 13:52 objects/
    drwxr-xr-x 4 ryan ryan 4096 Oct 27 13:36 refs/
    #Use git status again to view the situation
    ryan ~/git_demo/demo_client (master) $ git status
    On branch master
    
    No commits yet
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
            new file:   FileA
    
    

    You can see that FileA is no longer in the Untracked state, but in the to be committed state, indicating that "the behavior of creating a new FileA has been recorded in the temporary storage area";

    So at this time:

    • The workspace is not empty (with FileA);
    • The warehouse is not empty (the structure exists, but the content is empty);
    • The staging area is not empty (there is an index, recording the creation of FileA);
  4. Finally, we need to submit the modification of "FileA creation" to the git warehouse. Here, we need to use the git commit command:

    # git commit -m can directly submit the following string as the submission information. If it does not take the - m parameter, the default text editor (I configured nano before) will pop up for the user to enter the submission information;
    ryan ~/git_demo/demo_client (master) $ git commit -m "[demo_client]add FileA"
    [master (root-commit) 0dbaef0] [demo_client]add FileA
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 FileA
    # View the root directory of the warehouse
    ryan ~/git_demo/demo_client (master) $ ll
    total 12
    drwxr-xr-x 3 ryan ryan 4096 Oct 27 13:44 ./
    drwxr-xr-x 5 ryan ryan 4096 Oct 27 13:36 ../
    drwxr-xr-x 8 ryan ryan 4096 Oct 27 13:59 .git/
    -rw-r--r-- 1 ryan ryan    0 Oct 27 13:44 FileA
    # View. git warehouse
    ryan ~/git_demo/demo_client (master) $ ll .git/
    total 52
    drwxr-xr-x 8 ryan ryan 4096 Oct 27 13:58 ./
    drwxr-xr-x 3 ryan ryan 4096 Oct 27 13:44 ../
    -rw-r--r-- 1 ryan ryan   23 Oct 27 13:58 COMMIT_EDITMSG
    -rw-r--r-- 1 ryan ryan   23 Oct 27 13:36 HEAD
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 branches/
    -rw-r--r-- 1 ryan ryan  254 Oct 27 13:36 config
    -rw-r--r-- 1 ryan ryan   73 Oct 27 13:36 description
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 hooks/
    -rw-r--r-- 1 ryan ryan  137 Oct 27 13:58 index
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 info/
    drwxr-xr-x 3 ryan ryan 4096 Oct 27 13:58 logs/
    drwxr-xr-x 7 ryan ryan 4096 Oct 27 13:58 objects/
    drwxr-xr-x 4 ryan ryan 4096 Oct 27 13:36 refs/
    # Use git status to view git management
    ryan ~/git_demo/demo_client (master) $ git status
    On branch master
    Your branch is based on 'origin/master', but the upstream is gone.
      (use "git branch --unset-upstream" to fixup)
    
    nothing to commit, working tree clean
    # Use git log to view submission information
    ryan ~/git_demo/demo_client (master) $ git log
    commit 0dbaef0a8d45b36725fe6df20e1f1484709cea1f (HEAD -> master)
    Author: Ryan_ZHENG <***>
    Date:   Wed Oct 27 13:58:48 2021 +0800
    
        [demo_client]add FileA
    

    So at this time:

    • The workspace is not empty (with FileA);
    • The warehouse is not empty (there is a submission record of "[demo_client]add FileA");
    • The staging area is not empty (there is an index, recording the creation of FileA);
  5. To further understand the relationship between these three regions, let's delete FileA:

    # Directly operate the workspace and delete the file FileA
    ryan ~/git_demo/demo_client (master) $ rm FileA
    # Check the warehouse root directory. FileA no longer exists
    ryan ~/git_demo/demo_client (master) $ ll
    total 12
    drwxr-xr-x 3 ryan ryan 4096 Oct 27 14:07 ./
    drwxr-xr-x 5 ryan ryan 4096 Oct 27 13:36 ../
    drwxr-xr-x 8 ryan ryan 4096 Oct 27 13:59 .git/
    
    # View the. git directory
    drwxr-xr-x 8 ryan ryan 4096 Oct 27 14:07 ./
    drwxr-xr-x 3 ryan ryan 4096 Oct 27 14:07 ../
    -rw-r--r-- 1 ryan ryan   23 Oct 27 13:58 COMMIT_EDITMSG
    -rw-r--r-- 1 ryan ryan   23 Oct 27 13:36 HEAD
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 branches/
    -rw-r--r-- 1 ryan ryan  254 Oct 27 13:36 config
    -rw-r--r-- 1 ryan ryan   73 Oct 27 13:36 description
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 hooks/
    -rw-r--r-- 1 ryan ryan  137 Oct 27 13:58 index
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 info/
    drwxr-xr-x 3 ryan ryan 4096 Oct 27 13:58 logs/
    drwxr-xr-x 7 ryan ryan 4096 Oct 27 13:58 objects/
    drwxr-xr-x 4 ryan ryan 4096 Oct 27 13:36 refs/
    
    # Use git status to view git management
    ryan ~/git_demo/demo_client (master) $ git status
    On branch master
    Your branch is based on 'origin/master', but the upstream is gone.
      (use "git branch --unset-upstream" to fixup)
    
    Changes not staged for commit:
      (use "git add/rm <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
            deleted:    FileA
    
    no changes added to commit (use "git add" and/or "git commit -a")
    

    You can see that git finds that FileA has been deleted at this time, but because there are still FileA in the staging area, it determines that the operation of "deleting FileA" is not staged for commit;
    So at this time:

    • The workspace is empty (FileA has been deleted);
    • The warehouse is not empty (there is a submission record of "[demo_client]add FileA");
    • The staging area is not empty (there is an index, recording "creation of FileA", but not "deletion of FileA");
  6. Then, as usual, to submit this change to, we need to first add the "delete of FileA" behavior to the staging area. Note that since FileA is no longer in the workspace, the git add command needs to take the - A parameter:

    # Add delete FileA to staging area
    ryan ~/git_demo/demo_client (master) $ git add -A FileA
    # View the root directory of the warehouse
    ryan ~/git_demo/demo_client (master) $ ll
    total 12
    drwxr-xr-x 3 ryan ryan 4096 Oct 27 14:07 ./
    drwxr-xr-x 5 ryan ryan 4096 Oct 27 13:36 ../
    drwxr-xr-x 8 ryan ryan 4096 Oct 27 14:13 .git/
    # View the. git directory
    ryan ~/git_demo/demo_client (master) $ ll .git/
    total 52
    drwxr-xr-x 8 ryan ryan 4096 Oct 27 14:13 ./
    drwxr-xr-x 3 ryan ryan 4096 Oct 27 14:07 ../
    -rw-r--r-- 1 ryan ryan   23 Oct 27 13:58 COMMIT_EDITMSG
    -rw-r--r-- 1 ryan ryan   23 Oct 27 13:36 HEAD
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 branches/
    -rw-r--r-- 1 ryan ryan  254 Oct 27 13:36 config
    -rw-r--r-- 1 ryan ryan   73 Oct 27 13:36 description
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 hooks/
    -rw-r--r-- 1 ryan ryan   46 Oct 27 14:13 index
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 info/
    drwxr-xr-x 3 ryan ryan 4096 Oct 27 13:58 logs/
    drwxr-xr-x 7 ryan ryan 4096 Oct 27 13:58 objects/
    drwxr-xr-x 4 ryan ryan 4096 Oct 27 13:36 refs/
    # git status view git management
    ryan ~/git_demo/demo_client (master) $ git status
    On branch master
    Your branch is based on 'origin/master', but the upstream is gone.
      (use "git branch --unset-upstream" to fixup)
    
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            deleted:    FileA
    

    You can see that at this time, the. git/index file has been updated, and git status shows that "deletion of FileA" has changed from not stacked for commit to be committed;
    So at this time:

    • The workspace is empty (FileA has been deleted);
    • The warehouse is not empty (there is a submission record of "[demo_client]add FileA");
    • The staging area is not empty (there is an index, which records "creation of FileA" and "deletion of FileA");

    Note that although the staging area is not empty and the workspace is empty, the two records are the same - there is no FileA

  7. Finally, we use git commit to submit the "deletion of FileA":

    ryan ~/git_demo/demo_client (master) $ git commit -m "[demo_client]delete FileA"
    [master 0b64004] [demo_client]delete FileA
     1 file changed, 0 insertions(+), 0 deletions(-)
     delete mode 100644 FileA
     
    ryan ~/git_demo/demo_client (master) $ ll
    total 12
    drwxr-xr-x 3 ryan ryan 4096 Oct 27 14:07 ./
    drwxr-xr-x 5 ryan ryan 4096 Oct 27 13:36 ../
    drwxr-xr-x 8 ryan ryan 4096 Oct 27 14:19 .git/
    
    ryan ~/git_demo/demo_client (master) $ ll .git
    total 52
    drwxr-xr-x 8 ryan ryan 4096 Oct 27 14:19 ./
    drwxr-xr-x 3 ryan ryan 4096 Oct 27 14:07 ../
    -rw-r--r-- 1 ryan ryan   26 Oct 27 14:19 COMMIT_EDITMSG
    -rw-r--r-- 1 ryan ryan   23 Oct 27 13:36 HEAD
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 branches/
    -rw-r--r-- 1 ryan ryan  254 Oct 27 13:36 config
    -rw-r--r-- 1 ryan ryan   73 Oct 27 13:36 description
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 hooks/
    -rw-r--r-- 1 ryan ryan   65 Oct 27 14:19 index
    drwxr-xr-x 2 ryan ryan 4096 Oct 27 13:36 info/
    drwxr-xr-x 3 ryan ryan 4096 Oct 27 13:58 logs/
    drwxr-xr-x 9 ryan ryan 4096 Oct 27 14:19 objects/
    drwxr-xr-x 4 ryan ryan 4096 Oct 27 13:36 refs/
    
    ryan ~/git_demo/demo_client (master) $ git status
    On branch master
    Your branch is based on 'origin/master', but the upstream is gone.
      (use "git branch --unset-upstream" to fixup)
    
    nothing to commit, working tree clean
    
    ryan ~/git_demo/demo_client (master) $ git log
    commit 0b64004ae55297fee8ba6453f065a43a9135e5d7 (HEAD -> master)
    Author: Ryan_ZHENG <easter_walk@hotmail.com>
    Date:   Wed Oct 27 14:19:08 2021 +0800
    
        [demo_client]delete FileA
    
    commit 0dbaef0a8d45b36725fe6df20e1f1484709cea1f
    Author: Ryan_ZHENG <easter_walk@hotmail.com>
    Date:   Wed Oct 27 13:58:48 2021 +0800
    
        [demo_client]add FileA
    

    As you can see, git status shows that the work area returns to the clean state, that is, the records of the work area, warehouse and staging area are the same;
    At this time:

    • The workspace is empty (FileA has been deleted);
    • The warehouse is not empty (there is a submission record of "[demo_client]add FileA");
    • The staging area is not empty (there is an index, which records "creation of FileA" and "deletion of FileA");

    Like the above, it should be noted that although the states of the three are different, the phenomenon is the same - there is no FileA. The difference is that for the warehouse, the creation of FileA is recorded when two nodes are deleted; For the staging area, it records that the current directory exists, but no files are included; For the workspace, you only know that there are no files;

summary

The status of the current workspace file to the git warehouse can be judged according to the different status of the three areas, and displayed through different categories in git status:

Careful students should have found the following rules:

  • The work area exists and the staging area does not exist, which is Untracked;
  • The work area does not exist, and the staging area exists (or the contents of the work area and the staging area are inconsistent), which is manifested as Changes not staged for commit:
  • The staging area exists but the warehouse does not exist (or the contents of the staging area and the warehouse are inconsistent), which is shown as Changes to be committed:
  • If the three are completely consistent, it will prompt nothing to commit, working tree clean, and no Untracked

In fact, the first three cases above can be arranged and combined arbitrarily, and even the following two cases can be combined:

  • The three are completely inconsistent;
  • The staging area does not exist, and the warehouse does not exist;

These two types only appear in relatively advanced use scenarios. Here, we just "create" the environment. There will be more examples when explaining git reset later;

  • The three are completely inconsistent

    # Create a FileB file with bbb content
    ryan ~/git_demo/demo_client (master) $ echo "bbb" > FileB
    
    # Check the content
    ryan ~/git_demo/demo_client (master) $ cat FileB
    bbb
    
    # git status view FileB status as Untracked
    ryan ~/git_demo/demo_client (master) $ git status
    On branch master
    Your branch is based on 'origin/master', but the upstream is gone.
      (use "git branch --unset-upstream" to fixup)
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            FileB
    
    nothing added to commit but untracked files present (use "git add" to track)
    
    # Add FileB to staging area
    ryan ~/git_demo/demo_client (master) $ git add FileB
    
    # Use git status to view again. At this time, the progress of the workspace is consistent with that of the staging area, but inconsistent with that of the warehouse
    ryan ~/git_demo/demo_client (master) $ git status
    On branch master
    Your branch is based on 'origin/master', but the upstream is gone.
      (use "git branch --unset-upstream" to fixup)
    
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            new file:   FileB
    
    
    # Modify the content of FileB to bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
    ryan ~/git_demo/demo_client (master) $ echo "bbbbbb" > FileB
    
    # Check the content
    ryan ~/git_demo/demo_client (master) $ cat FileB
    bbbbbb
    
    # Use git status to view again. At this time, the progress of the workspace is inconsistent with that of the staging area and the warehouse
    ryan ~/git_demo/demo_client (master) $ git status
    On branch master
    Your branch is based on 'origin/master', but the upstream is gone.
      (use "git branch --unset-upstream" to fixup)
    
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            new file:   FileB
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
            modified:   FileB
    
    
  • The staging area does not exist, and the warehouse does not exist;

    #Then the environment above continues
    ryan ~/git_demo/demo_client (master) $ git status
    On branch master
    Your branch is based on 'origin/master', but the upstream is gone.
      (use "git branch --unset-upstream" to fixup)
    
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            new file:   FileB
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
            modified:   FileB
    # Delete FileB so that FileB does not exist in warehouse and workspace
    ryan ~/git_demo/demo_client (master) $ rm FileB
    # git status view
    ryan ~/git_demo/demo_client (master) $ git status
    On branch master
    Your branch is based on 'origin/master', but the upstream is gone.
      (use "git branch --unset-upstream" to fixup)
    
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            new file:   FileB
    
    Changes not staged for commit:
      (use "git add/rm <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
            deleted:    FileB
    

Last picture:

Posted by matstuff on Tue, 26 Oct 2021 23:01:00 -0700