Git Series II Data Management

Keywords: git Linux PHP vim

1.Git Basic Management

Basic operations commonly used in git

1.1 Data submission

We can simply think of working directory as a directory managed by Git service program. Git will keep track of file changes in the directory at all times. In addition, after installing Git service program, by default, a branch called master will be created. We can submit data directly to the directory.

1. Create the local working directory oldboy and initialize it to gi


  1. [root@linux-node1 ~]# mkdir oldboy ##This directory is a warehouse.
  2. [root@linux-node1 ~]# cd oldboy/
    [root@linux-node1 oldboy]# git init
    Initialized empty Git repository in /root/oldboy/.git/
    [root@linux-node1 oldboy]# ls -la
    Total dosage 12
    drwxr-xr-x 3 root root 4096 1 Month 2011:49 .
    dr-xr-x---. 10 root root 4096 1 Month 2011:48 ..
    drwxr-xr-x 7 root root 4096 1 Month 2011:49 .git


2. Create readme.txt file

  1. [root@linux-node1 oldboy]# vim readme.txt
    hehe

3. View git status

    [root@linux-node1 oldboy]# git status
    # On branch master
    #
    # Initial commit
    #
    # Untracked files:
    # (use "git add <file>..." to include in what will be committed)
    #
    # readme.txt
    nothing added to commit but untracked files present (use "git add" to track)

4. Hint to add files to the temporary area using git add

    [root@linux-node1 oldboy]# git add readme.txt
    [root@linux-node1 oldboy]# git status
    # On branch master
    #
    # Initial commit
    #
    # Changes to be committed:
    # (use "git rm --cached <file>..." to unstage)
    #
    # new file: readme.txt

5. Use git cmmit to submit temporary area files to git version warehouse-m: submit description information (submit to remote warehouse needs to add remote warehouse)

    [root@linux-node1 oldboy]# git commit -m "the first commit"
    [master (root-commit) 92e0f4f] the first commit
    1 files changed, 1 insertions(+), 0 deletions(-)
    create mode 100644 readme.txt
    [root@linux-node1 oldboy]# git status
    # On branch master
    nothing to commit (working directory clean)
    [root@linux-node1 oldboy]# vim deploy.sh
    #!/bin/bash
    echo hehe
    [root@linux-node1 oldboy]# cat deploy.sh
    #!/bin/bash
    echo hehe
    [root@linux-node1 oldboy]# git status
    # On branch master
    # Untracked files:
    # (use "git add <file>..." to include in what will be committed)
    #
    # deploy.sh
    nothing added to commit but untracked files present (use "git add" to track)
    [root@linux-node1 oldboy]# git add deploy.sh
    [root@linux-node1 oldboy]# git commit -m "2th commit"
    [master e5689ee] 2th commit
    1 files changed, 2 insertions(+), 0 deletions(-)
    create mode 100644 deploy.sh
    [root@linux-node1 oldboy]# ll
    Total dosage 8
    -rw-r--r-- 1 root root 22 1 Month 2013:51 deploy.sh
    -rw-r--r-- 1 root root 7 1 Month 2013:47 readme.txt
    [root@linux-node1 oldboy]# git log
    commit e5689eeca892c838f95b6c75591a3c55a4adfe88
    Author: sundandan <sundandan@gmail.com>
    Date: Fri Jan 20 13:52:13 2017 +0800
    2th commit
    commit 92e0f4f60bded4b7b19eb663bf57e0bbc121c199
    Author: sundandan <sundandan@gmail.com>
    Date: Fri Jan 20 13:50:05 2017 +0800
    the first commit
    [root@linux-node1 oldboy]# vim readme.txt
    1 hehe
    2 hehe
    [root@linux-node1 oldboy]# cat readme.txt
    1 hehe
    2 hehe
    [root@linux-node1 oldboy]# git status
    # On branch master
    # Changed but not updated:
    # (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")
    [root@linux-node1 oldboy]# git diff readme.txt
    diff --git a/readme.txt b/readme.txt
    index 408e625..833bc94 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1 +1,2 @@
    1 hehe
    +2 hehe
    [root@linux-node1 oldboy]# git add readme.txt
    [root@linux-node1 oldboy]# git commit -m "add 2hehe"
    [master 7bec572] add 2hehe
    1 files changed, 1 insertions(+), 0 deletions(-)
    [root@linux-node1 oldboy]# git log
    commit 7bec57206401f756f43cdc5ddcceaaff2cdbe3bb
    Author: sundandan <sundandan@gmail.com>
    Date: Fri Jan 20 13:54:21 2017 +0800
    add 2hehe
    commit e5689eeca892c838f95b6c75591a3c55a4adfe88
    Author: sundandan <sundandan@gmail.com>
    Date: Fri Jan 20 13:52:13 2017 +0800
    2th commit
    commit 92e0f4f60bded4b7b19eb663bf57e0bbc121c199
    Author: sundandan <sundandan@gmail.com>
    Date: Fri Jan 20 13:50:05 2017 +0800
    the first commit

1.2 Remove data

Sometimes the files that have been added to the temporary area will be removed, but you still want the files not to be lost in the working directory, in other words, the files will be deleted from the tracking list.

1. Establishment of new documents

  1. [root@git-node1 demo]# touch index.php

2. Add new files to the temporary area

  1. [root@git-node1 demo]# git add index.php
  2. [root@git-node1 demo]# git status
  3. # Located in branch master
  4. # Changes to be submitted:
  5. # (Use "git reset HEAD < file >..." to evacuate the temporary storage area)
  6. #
  7. # New file: index.php
  8. #

3. Remove new files from the temporary area (data in the working directory will not be deleted)

  1. [root@git-node1 demo]# git reset HEAD index.php
  2. [root@git-node1 demo]# git status //index.php file status is not tracked
  3. # Located in branch master
  4. # Untracked files:
  5. # (Use "git add < file >..." to include the content to be submitted)
  6. #
  7. # index.php
  8. The submission is empty, but there are files that have not yet been tracked (using "git add" Establish tracking)
  9. If you want to transfer file data fromgitThe temporary area and working directory can be deleted together to do the following.
  10. [root@git-node1 demo]# git add index.php
  11. [root@git-node1 demo]# git rm index.php //File storage has been put into temporary storage area to prevent deletion by mistake
  12. error: 'index.php' Changes have been temporarily saved to the index
  13. (Use --cached Save files, or use -f Forced deletion)
  14. [root@git-node1 demo]# git rm -f index.php //Delete the - f parameter using the tracing reinforcement. Or - cache save
  15. rm 'index.php'
  16. [root@git-node1 demo]# git status //View the current status
  17. # Located in branch master
  18. No documents to submit, clean workspace
  19. [root@git-node1 demo]# ls //The index.php file has been deleted
  20. index.html

1.3 Mobile Data

Sometimes the name of the file that has been added to the temporary area will be changed again, which can be accomplished by following operations.
1.git renames the file name and uses the git mv operation.

  1. [root@git-node1 demo]# git mv index.html index.php

2. Use git status to view change status

  1. [root@git-node1 demo]# git status
  2. # Located in branch master
  3. # Changes to be submitted:
  4. # (Use "git reset HEAD < file >..." to evacuate the temporary storage area)
  5. #
  6. # Rename: index. HTML - > index. PHP
  7. #

3. Use git commit to submit git repository and describe submission information

  1. [root@git-node1 demo]# git commit -m "chnged name index.html->index.php"
  2. [master 9573413] chnged name index.html->index.php
  3. 1 file changed, 0 insertions(+), 0 deletions(-)
  4. rename index.html => index.php (100%)

1.4 Historical Data

After submitting several updates, to review the submission history, you can use the git log command to view the submission history.
1. View the submission history

  1. [root@git-node1 demo]# git log
  2. commit 95734131860ef7c9078b8a208ff6437d0952380b
  3. Author: xuliangwei <xuliangwei@foxmail.com>
  4. Date: Sun Nov 6 02:15:35 2016 +0800
  5. chnged name index.html->index.php
  6. commit 85bd2680bd4b70aeded9dbd230c07ab086712ff9
  7. Author: xuliangwei <xuliangwei@foxmail.com>
  8. Date: Sun Nov 6 01:51:22 2016 +0800
  9. the first commit index.html

2. View the history of two submissions

  1. [root@git-node1 demo]# git log -10
  2. commit 95734131860ef7c9078b8a208ff6437d0952380b
  3. Author: xuliangwei <xuliangwei@foxmail.com>
  4. Date: Sun Nov 6 02:15:35 2016 +0800
  5. chnged name index.html->index.php
  6. commit 85bd2680bd4b70aeded9dbd230c07ab086712ff9
  7. Author: xuliangwei <xuliangwei@foxmail.com>
  8. Date: Sun Nov 6 01:51:22 2016 +0800
  9. the first commit index.html

3. Display submission content differences, such as only looking at the latest differences

  1. [root@git-node1 demo]# git log -p -2
  2. commit 95734131860ef7c9078b8a208ff6437d0952380b
  3. Author: xuliangwei <xuliangwei@foxmail.com>
  4. Date: Sun Nov 6 02:15:35 2016 +0800
  5. chnged name index.html->index.php
  6. diff --git a/index.html b/index.html
  7. deleted file mode 100644
  8. index e69de29..0000000
  9. diff --git a/index.php b/index.php
  10. new file mode 100644
  11. index 0000000..e69de29
  12. commit 85bd2680bd4b70aeded9dbd230c07ab086712ff9
  13. Author: xuliangwei <xuliangwei@foxmail.com>
  14. Date: Sun Nov 6 01:51:22 2016 +0800
  15. the first commit index.html
  16. diff --git a/index.html b/index.html
  17. new file mode 100644
  18. index 0000000..e69de29
  1. Brief display of the number of rows added and changed by data
  1. [root@git-node1 demo]# git log --stat
  2. commit 95734131860ef7c9078b8a208ff6437d0952380b
  3. Author: xuliangwei <xuliangwei@foxmail.com>
  4. Date: Sun Nov 6 02:15:35 2016 +0800
  5. chnged name index.html->index.php
  6. index.html | 0
  7. 2 files changed, 0 insertions(+), 0 deletions(-)
  8. commit 85bd2680bd4b70aeded9dbd230c07ab086712ff9
  9. Author: xuliangwei <xuliangwei@foxmail.com>
  10. Date: Sun Nov 6 01:51:22 2016 +0800
  11. the first commit index.html
  12. index.html | 0
  13. 1 file changed, 0 insertions(+), 0 deletions(-)

5. Display submitted historical information in different formats

  1. [root@git-node1 demo]# git log --pretty=oneline
  2. 95734131860ef7c9078b8a208ff6437d0952380b chnged name index.html->index.php
  3. 85bd2680bd4b70aeded9dbd230c07ab086712ff9 the first commit index.htm
  4. Form parameters can be used to specify the specific output format, which is very convenient for extraction and analysis of later programming, oh, commonly used formats are:
  5. % s Submit notes.
  6. % cd submission date.
  7. % The name of an author.
  8. % The name of the cn submitter.
  9. % e-mail from ce submitter.
  10. % H submits the complete SHA-1 hash string of the object.
  11. % The short SHA-1 hash string for the h submission object.
  12. % A complete SHA-1 hash string for a T-tree object.
  13. % Short SHA-1 hash string for t-tree objects.
  14. % The complete SHA-1 hash string of the P parent object.
  15. % Short SHA-1 hash string for p parent object.
  16. % The revision time of the author of ad.
  17. Customize detailed log output
  18. [root@git-node1 demo]# git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset %cn"' --abbrev-commit --date=relative
  19. * 5f3f588 - (HEAD, origin/master, - r, tt, master) merge branch linux readme.txt (68 minutes ago) xulia
  20. |\
  21. |* 4580c3b - (linux) touch readme.txt (80 minutes ago) xuliangwei“
  22. *| 4c7a145 - touch readme.txt (72 minutes ago) xuliangwei“
  23. |/
  24. * 9573413 - (tag: v1.0.0) chnged name index. HTML - > index. PHP (5 hours ago) xuliangwei“
  25. * 85bd268 - the first commit index.html (5 hours ago) xuliangwei“
  26. Configure the. git/config file, add the following two lines, and then use git hlog instead of the complex command as above
  27. [root@git-node1 demo]# tail -2 .git/config
  28. [alias]
  29. hlog = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset %cn' --abbrev-commit --date=relative
  30. // Complex commands can be implemented later with git hlog

1.5 Restore Data

The file boss good is written as boss dou, but it has been submitted to the remote warehouse and now wants to withdraw and resubmit.
(This summary covers remote warehouses. You can learn the following chapters first and this chapter later.)
1. View the index.html file

  1. [root@git-node1 demo]# git checkout -b dev //Content has dev branches
  2. [root@git-node1 demo]# cat index.html
  3. hello boss
  4. [root@git-node1 demo]# echo "boos doubi" >> index.html

2. Additional content to index.html and submitted to remote warehouse

  1. [root@git-node1 demo]# git add index.html
  2. [root@git-node1 demo]# git commit -m "boss dou"
  3. [master 1941990] boss dou
  4. 1 file changed, 1 insertion(+)
  5. [root@git-node1 demo]# git push origin dev
  6. Counting objects: 5, done.
  7. Compressing objects: 100% (2/2), done.
  8. Writing objects: 100% (3/3), 298 bytes | 0 bytes/s, done.
  9. Total 3 (delta 0), reused 0 (delta 0)
  10. To git@git-node1:root/git_demo.git
  11. 507bf99..1941990 dev -> dev

3. At this time, I feel that I did not write properly. I want to restore the snapshot of the last submitted document and find out the historical information.

  1. [root@git-node1 demo]# git hlog -2
  2. * 1941990 - (HEAD, origin/dev, dev) boss dou (2 Minutes ago) xuliangwei
  3. * 507bf99 - hello (30 Minutes ago) xuliangwei

4. After finding the value of the historical restoring point, it can be restored (if the value is not complete, the system will automatically match)

  1. [root@git-node1 demo]# git reset --hard 507bf99
  2. HEAD Now located 507bf99 hello
  3. [root@git-node1 xuliangwei]# git push origin dev -f //Force push to dev branch
  4. [root@git-node1 demo]# cat index.html
  5. hello boss

1.6 Summary of Basic Operation

Believe that you have a certain understanding of the basic operation of Git, the following is a simple summary of combing:

  1. command git config --global user.name "name" #Configure git to use users
  2. # git config --global user.email "mail" #Configure git to use mailbox
  3. # git config --global color.ui true #Collocation color
  4. # git config --list #View the current configuration
  5. # git init #Initially git working directory
  6. # git status #View git status
  7. # git reflog #View Future History Update Points
  8. # git reset --hard 4bf5b29 #If the SHA-1 value of the historical restore point is found, it can be restored.
  9. # git checkout -- file #Restore the Temporary Zone to the previous version
  10. # git add [file1] [file2] ... #Add the specified file to the temporary area
  11. # git add [dir] #Add the specified directory to the temporary area, including subdirectories (recursive addition)
  12. # git add . #Add all files in the current directory to the temporary area
  13. # git rm [file1] [file2] ... #Delete the workspace file and put this deletion in the temporary storage area
  14. # git rm –cached [file] #Stop tracking the specified file, but it will remain in the workspace
  15. # git mv [file-old] [file-new] #Rename the file and put it in the temporary storage area after modification




Posted by johnsworld on Fri, 19 Apr 2019 15:33:35 -0700