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
[root@linux-node1 ~]# mkdir oldboy ##This directory is a warehouse. [root@linux-node1 ~]# cd oldboy/[root@linux-node1 oldboy]# git initInitialized empty Git repository in /root/oldboy/.git/[root@linux-node1 oldboy]# ls -laTotal dosage 12drwxr-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
[root@linux-node1 oldboy]# vim readme.txthehe
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.txtnothing 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 commit1 files changed, 1 insertions(+), 0 deletions(-)create mode 100644 readme.txt[root@linux-node1 oldboy]# git status# On branch masternothing to commit (working directory clean)[root@linux-node1 oldboy]# vim deploy.sh#!/bin/bashecho hehe[root@linux-node1 oldboy]# cat deploy.sh#!/bin/bashecho hehe[root@linux-node1 oldboy]# git status# On branch master# Untracked files:# (use "git add <file>..." to include in what will be committed)## deploy.shnothing 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 commit1 files changed, 2 insertions(+), 0 deletions(-)create mode 100644 deploy.sh[root@linux-node1 oldboy]# llTotal 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 logcommit e5689eeca892c838f95b6c75591a3c55a4adfe88Author: sundandan <sundandan@gmail.com>Date: Fri Jan 20 13:52:13 2017 +08002th commitcommit 92e0f4f60bded4b7b19eb663bf57e0bbc121c199Author: sundandan <sundandan@gmail.com>Date: Fri Jan 20 13:50:05 2017 +0800the first commit[root@linux-node1 oldboy]# vim readme.txt1 hehe2 hehe[root@linux-node1 oldboy]# cat readme.txt1 hehe2 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.txtdiff --git a/readme.txt b/readme.txtindex 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 2hehe1 files changed, 1 insertions(+), 0 deletions(-)[root@linux-node1 oldboy]# git logcommit 7bec57206401f756f43cdc5ddcceaaff2cdbe3bbAuthor: sundandan <sundandan@gmail.com>Date: Fri Jan 20 13:54:21 2017 +0800add 2hehecommit e5689eeca892c838f95b6c75591a3c55a4adfe88Author: sundandan <sundandan@gmail.com>Date: Fri Jan 20 13:52:13 2017 +08002th commitcommit 92e0f4f60bded4b7b19eb663bf57e0bbc121c199Author: sundandan <sundandan@gmail.com>Date: Fri Jan 20 13:50:05 2017 +0800the 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
[root@git-node1 demo]# touch index.php
2. Add new files to the temporary area
[root@git-node1 demo]# git add index.php
[root@git-node1 demo]# git status
# Located in branch master
# Changes to be submitted:
# (Use "git reset HEAD < file >..." to evacuate the temporary storage area)
#
# New file: index.php
#
3. Remove new files from the temporary area (data in the working directory will not be deleted)
[root@git-node1 demo]# git reset HEAD index.php
[root@git-node1 demo]# git status //index.php file status is not tracked
# Located in branch master
# Untracked files:
# (Use "git add < file >..." to include the content to be submitted)
#
# index.php
The submission is empty, but there are files that have not yet been tracked (using "git add" Establish tracking)
If you want to transfer file data fromgitThe temporary area and working directory can be deleted together to do the following.
[root@git-node1 demo]# git add index.php
[root@git-node1 demo]# git rm index.php //File storage has been put into temporary storage area to prevent deletion by mistake
error: 'index.php' Changes have been temporarily saved to the index
(Use --cached Save files, or use -f Forced deletion)
[root@git-node1 demo]# git rm -f index.php //Delete the - f parameter using the tracing reinforcement. Or - cache save
rm 'index.php'
[root@git-node1 demo]# git status //View the current status
# Located in branch master
No documents to submit, clean workspace
[root@git-node1 demo]# ls //The index.php file has been deleted
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.
[root@git-node1 demo]# git mv index.html index.php
2. Use git status to view change status
[root@git-node1 demo]# git status
# Located in branch master
# Changes to be submitted:
# (Use "git reset HEAD < file >..." to evacuate the temporary storage area)
#
# Rename: index. HTML - > index. PHP
#
3. Use git commit to submit git repository and describe submission information
[root@git-node1 demo]# git commit -m "chnged name index.html->index.php"
[master 9573413] chnged name index.html->index.php
1 file changed, 0 insertions(+), 0 deletions(-)
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
[root@git-node1 demo]# git log
commit 95734131860ef7c9078b8a208ff6437d0952380b
Author: xuliangwei <xuliangwei@foxmail.com>
Date: Sun Nov 6 02:15:35 2016 +0800
chnged name index.html->index.php
commit 85bd2680bd4b70aeded9dbd230c07ab086712ff9
Author: xuliangwei <xuliangwei@foxmail.com>
Date: Sun Nov 6 01:51:22 2016 +0800
the first commit index.html
2. View the history of two submissions
[root@git-node1 demo]# git log -10
commit 95734131860ef7c9078b8a208ff6437d0952380b
Author: xuliangwei <xuliangwei@foxmail.com>
Date: Sun Nov 6 02:15:35 2016 +0800
chnged name index.html->index.php
commit 85bd2680bd4b70aeded9dbd230c07ab086712ff9
Author: xuliangwei <xuliangwei@foxmail.com>
Date: Sun Nov 6 01:51:22 2016 +0800
the first commit index.html
3. Display submission content differences, such as only looking at the latest differences
[root@git-node1 demo]# git log -p -2
commit 95734131860ef7c9078b8a208ff6437d0952380b
Author: xuliangwei <xuliangwei@foxmail.com>
Date: Sun Nov 6 02:15:35 2016 +0800
chnged name index.html->index.php
diff --git a/index.html b/index.html
deleted file mode 100644
index e69de29..0000000
diff --git a/index.php b/index.php
new file mode 100644
index 0000000..e69de29
commit 85bd2680bd4b70aeded9dbd230c07ab086712ff9
Author: xuliangwei <xuliangwei@foxmail.com>
Date: Sun Nov 6 01:51:22 2016 +0800
the first commit index.html
diff --git a/index.html b/index.html
new file mode 100644
index 0000000..e69de29
- Brief display of the number of rows added and changed by data
[root@git-node1 demo]# git log --stat
commit 95734131860ef7c9078b8a208ff6437d0952380b
Author: xuliangwei <xuliangwei@foxmail.com>
Date: Sun Nov 6 02:15:35 2016 +0800
chnged name index.html->index.php
index.html | 0
2 files changed, 0 insertions(+), 0 deletions(-)
commit 85bd2680bd4b70aeded9dbd230c07ab086712ff9
Author: xuliangwei <xuliangwei@foxmail.com>
Date: Sun Nov 6 01:51:22 2016 +0800
the first commit index.html
index.html | 0
1 file changed, 0 insertions(+), 0 deletions(-)
5. Display submitted historical information in different formats
- [root@git-node1 demo]# git log --pretty=oneline
- 95734131860ef7c9078b8a208ff6437d0952380b chnged name index.html->index.php
- 85bd2680bd4b70aeded9dbd230c07ab086712ff9 the first commit index.htm
- 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:
- % s Submit notes.
- % cd submission date.
- % The name of an author.
- % The name of the cn submitter.
- % e-mail from ce submitter.
- % H submits the complete SHA-1 hash string of the object.
- % The short SHA-1 hash string for the h submission object.
- % A complete SHA-1 hash string for a T-tree object.
- % Short SHA-1 hash string for t-tree objects.
- % The complete SHA-1 hash string of the P parent object.
- % Short SHA-1 hash string for p parent object.
- % The revision time of the author of ad.
- Customize detailed log output
- [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
- * 5f3f588 - (HEAD, origin/master, - r, tt, master) merge branch linux readme.txt (68 minutes ago) xulia
- |\
- |* 4580c3b - (linux) touch readme.txt (80 minutes ago) xuliangwei“
- *| 4c7a145 - touch readme.txt (72 minutes ago) xuliangwei“
- |/
- * 9573413 - (tag: v1.0.0) chnged name index. HTML - > index. PHP (5 hours ago) xuliangwei“
- * 85bd268 - the first commit index.html (5 hours ago) xuliangwei“
- Configure the. git/config file, add the following two lines, and then use git hlog instead of the complex command as above
- [root@git-node1 demo]# tail -2 .git/config
- [alias]
- hlog = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset %cn' --abbrev-commit --date=relative
- // 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
[root@git-node1 demo]# git checkout -b dev //Content has dev branches
[root@git-node1 demo]# cat index.html
hello boss
[root@git-node1 demo]# echo "boos doubi" >> index.html
2. Additional content to index.html and submitted to remote warehouse
[root@git-node1 demo]# git add index.html
[root@git-node1 demo]# git commit -m "boss dou"
[master 1941990] boss dou
1 file changed, 1 insertion(+)
[root@git-node1 demo]# git push origin dev
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 298 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@git-node1:root/git_demo.git
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.
[root@git-node1 demo]# git hlog -2
* 1941990 - (HEAD, origin/dev, dev) boss dou (2 Minutes ago) xuliangwei
* 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)
[root@git-node1 demo]# git reset --hard 507bf99
HEAD Now located 507bf99 hello
[root@git-node1 xuliangwei]# git push origin dev -f //Force push to dev branch
[root@git-node1 demo]# cat index.html
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:
command git config --global user.name "name" #Configure git to use users
# git config --global user.email "mail" #Configure git to use mailbox
# git config --global color.ui true #Collocation color
# git config --list #View the current configuration
# git init #Initially git working directory
# git status #View git status
# git reflog #View Future History Update Points
# git reset --hard 4bf5b29 #If the SHA-1 value of the historical restore point is found, it can be restored.
# git checkout -- file #Restore the Temporary Zone to the previous version
# git add [file1] [file2] ... #Add the specified file to the temporary area
# git add [dir] #Add the specified directory to the temporary area, including subdirectories (recursive addition)
# git add . #Add all files in the current directory to the temporary area
# git rm [file1] [file2] ... #Delete the workspace file and put this deletion in the temporary storage area
# git rm –cached [file] #Stop tracking the specified file, but it will remain in the workspace
# git mv [file-old] [file-new] #Rename the file and put it in the temporary storage area after modification