Git - basic operation commands

Xiao Yongyong 2021-09-15 10:45:14

View warehouse status

First, look at the whole state of the warehouse without operation
[root@supman git]# git status
# In a branch master
#
# Initial submission
#
No documents to submit ( establish / Copy files and use "git add" Set up tracking )
Then create a new file in the directory to check the status
[root@supman git]# touch first.txt
[root@supman git]# git status
# In a branch master
#
# Initial submission
#
# Untracked files :
# ( Use "git add <file>..." To include content to submit )
#
# first.txt
Submission is empty , But there are files that have not been tracked ( Use "git add" Set up tracking )

Track a file

From the above, we can see what the status of a file is now

Now let's follow the prompts to use git add Command to trace this file

[root@supman git]# git add first.txt
[root@supman git]# git status
# In a branch master
#
# Initial submission
#
# Changes to be submitted :
# ( Use "git rm --cached <file>..." Get out of the staging area )
#
# A new file : first.txt
#
git Again ! git rm --cached <file> You can withdraw from the staging area, that is, cancel tracking , So let's operate it for you and see what the function of this command is .( The staging area is git One of the three areas of , There will be a unified explanation later )
[root@supman git]# git rm --cached first.txt
rm 'first.txt'
[root@supman git]# git status
# In a branch master
#
# Initial submission
#
# Untracked files :
# ( Use "git add <file>..." To include content to submit )
#
# first.txt
Submission is empty , But there are files that have not been tracked ( Use "git add" Set up tracking )

From the above execution results, we can clearly see git rm --cached xxx Is to use git add Command to restore tracked files to untracked state

In addition to operating files one by one, you can also operate them in batches .

[root@supman git]# touch second.txt
[root@supman git]# git status
# In a branch master
#
# Initial submission
#
# Untracked files :
# ( Use "git add <file>..." To include content to submit )
#
# first.txt
# second.txt
Submission is empty , But there are files that have not been tracked ( Use "git add" Set up tracking )
[root@supman git]# git add .
[root@supman git]# git status
# In a branch master
#
# Initial submission
#
# Changes to be submitted :
# ( Use "git rm --cached <file>..." Get out of the staging area )
#
# A new file : first.txt
# A new file : second.txt
#
[root@supman git]# git rm -r --cached *.txt
rm 'first.txt'
rm 'second.txt'
[root@supman git]# git status
# In a branch master
#
# Initial submission
#
# Untracked files :
# ( Use "git add <file>..." To include content to submit )
#
# first.txt
# second.txt
Submission is empty , But there are files that have not been tracked ( Use "git add" Set up tracking )
When a file is changed, what is the status of the tracked file ?
[root@supman git]# git add .
[root@supman git]# echo " Hello " > first.txt
[root@supman git]# git status
# In a branch master
#
# Initial submission
#
# Changes to be submitted :
# ( Use "git rm --cached <file>..." Get out of the staging area )
#
# A new file : first.txt
# A new file : second.txt
#
# Changes that have not been staged for submission :
# ( Use "git add <file>..." Update content to submit )
# ( Use "git checkout -- <file>..." Discard workspace changes )
#
# modify : first.txt
#

git Gave us three options

  1. Get out of the staging area - In this way, the documents will not be submitted when they are submitted first.txt This file .
  2. Update content to submit - That is, the temporary storage area first.txt Update to the latest status .
  3. Discard workspace changes - It's the local first.txt Update to the state before no change ( The workspace is also one of the three areas ).

    [root@supman git]# git checkout -- first.txt
    [root@supman git]# ls
    first.txt second.txt
    [root@supman git]# cat first.txt 

    Operate this command , Find out first.txt There's nothing in it , It is restored to the content before modification .

Except for the top git rm --cached There is a similar command , You can also undo tracking git reset --

[root@supman git]# git add .
[root@supman git]# git status
# In a branch master
#
# Initial submission
#
# Changes to be submitted :
# ( Use "git rm --cached <file>..." Get out of the staging area )
#
# A new file : first.txt
# A new file : second.txt
#
[root@supman git]# git reset --
[root@supman git]# git status
# In a branch master
#
# Initial submission
#
# Untracked files :
# ( Use "git add <file>..." To include content to submit )
#
# first.txt
# second.txt
Submission is empty , But there are files that have not been tracked ( Use "git add" Set up tracking )
Use git diff Command compares files in staging and workspace
[root@supman git]# git diff
diff --git a/first.txt b/first.txt
index e69de29..3b18e51 100644
--- a/first.txt
+++ b/first.txt
@@ -0,0 +1 @@
+hello world

Make a new version

Before learning to add a new version, let's learn how to view the version information

Use git log Command to view the submitted history of the version area ( The version area is also git One of the three regions )

[root@supman git]# git log
fatal: bad default revision 'HEAD'

It shows that we have not submitted the version for you ....

So let's submit it a few times ... The command to submit is git commit

[root@supman git]# git commit -m " First submission "
[master( Root submit ) e58e53e] First submission 2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 first.txt
create mode 100644 second.txt
[root@supman git]# git logcommit e58e53ea4a19021ac7640aac6994831b7fcf1b85
Author: yong.yuan <1218639030@qq.com>
Date: Sat Sep 4 17:14:15 2021 +0800
First submission 

Then we'll submit it again for you

here git As like as two peas, the information we sent to our version is exactly the same as the information in the temporary storage area

[root@supman git]# git commit -m " Second submission "
# In a branch master# Changes that have not been staged for submission :
# ( Use "git add <file>..." Update content to submit )
# ( Use "git checkout -- <file>..." Discard workspace changes )
# modify : first.txt
#
The modification has not been added to the submission ( Use "git add" and / or "git commit -a")
Let's modify a file later , Submit again to see
[root@supman git]# git add second.txt
[root@supman git]# git commit -m " Second submission "
[master c55da94] Second submission 1 file changed, 1 insertion(+)
[root@supman git]# git log
commit c55da94d57a4e7f1a076483d09afd182ad10bd5d
Author: yong.yuan <1218639030@qq.com>
Date: Sat Sep 4 17:19:31 2021 +0800
Second submission
commit e58e53ea4a19021ac7640aac6994831b7fcf1b85
Author: yong.yuan <1218639030@qq.com>
Date: Sat Sep 4 17:14:15 2021 +0800
First submission 
After all submitted , Let's look at the local state again
[root@supman git]# git status
# In a branch master
# Your branch leads 'origin/master' common 1 Submission .
# ( Use "git push" To publish your local submission )
#
No documents to submit , Clean work area 

Commands for viewing submission history , Some common options are introduced :

  • git log [ Branch name ] View the submission history of a branch , View the current branch without writing the branch name
  • git log --oneline One line shows the submission history
  • git log -n among n It's the number. , Look at the recent n Submission
  • git log --author [ Contributor name ] View submission records of specified contributors
  • git log --graph Graphically display submission history
  • git log --reverse Show positive order submission history

Version recall

First, we will modify the two files for you and submit them
[root@supman git]# echo "hello ha-ha " > first.txt
[root@supman git]# git add first.txt
[root@supman git]# git commit -m " modify first.txt"
[master fda1d98] modify first.txt
1 file changed, 1 insertion(+), 1 deletion(-)
[root@supman git]# echo " Hello ha-ha " > second.txt
[root@supman git]# git add second.txt
[root@supman git]# git commit -m " modify sencond.txt"
[master d967d87] modify sencond.txt
1 file changed, 1 insertion(+), 1 deletion(-)
[root@supman git]# git log -2
commit d967d8730ca0e5a9d97a04a2ce448fbf9e280d4c
Author: yong.yuan <1218639030@qq.com>
Date: Mon Sep 6 19:47:05 2021 +0800
modify sencond.txt
commit fda1d98c0b5fdbcaf06f0edffac9a63004a6365d
Author: yong.yuan <1218639030@qq.com>
Date: Mon Sep 6 19:44:31 2021 +0800
modify first.txt
We can use git reset --soft HEAD^ Back to the previous version , It can also be used. git reset --soft HEAD^2 Go back to two versions
[root@supman git]# git reset --soft HEAD^
[root@supman git]# git log -2
commit fda1d98c0b5fdbcaf06f0edffac9a63004a6365d
Author: yong.yuan <1218639030@qq.com>
Date: Mon Sep 6 19:44:31 2021 +0800
modify first.txt
commit 1c0724081fa7505b34e9d269dae43d06babc9851
Author: yong.yuan <1218639030@qq.com>
Date: Mon Sep 6 19:27:15 2021 +0800
Second submission 
Let's now look at the withdrawn second.txt file , The discovery is the file information before we submit , in other words git reset --soft Just take the file from Version area There we are Temporary storage area But the contents of the document have not changed .
[root@supman git]# cat second.txt
Hello ha-ha 
Similar to this, there is a more hard The choice of git reset --hard When you look at the file, you find that this is not the content of the file just submitted , according to --soft The logic should be hello ha-ha That's right , But now it's time to move the workspace ( Local ) Of first.txt It becomes the content of the latest version in the version area . The other one is git reset --mixed Be situated between hard and soft Between , Using this command will make the tracked files and version area files the previous version , However, the contents of the local file, that is, the file in the workspace, remain unchanged .
[root@supman git]# git reset --hard HEAD^
HEAD It's now in 1c07240 Second submission
[root@supman git]# cat first.txt
hello world

Push to remote server

Use git push -u origin master After the command, enter the account password to commit Files in the version area are pushed to the remote warehouse .
[root@supman git]# git push -u origin master
Username for 'https://gitee.com': codeyuany
Password for 'https://codeyuany@gitee.com':
Counting objects: 9, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (9/9), 769 bytes | 0 bytes/s, done.
Total 9 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.1]
To https://gitee.com/codeyuany/git.git
* [new branch] master -> master
Branch master Set to trace from origin Remote branch of master
Use git branch You can view the current branch
[root@supman git]# git branch
* master
Use git branch -avv Command can view basic information of some branches , The corresponding messages below are Local branch Version number Remote time sharing Submission log
[root@supman git]# git branch -avv
* master 1c07240 [origin/master] Second submission
remotes/origin/master 1c07240 Second submission 
When we use git --reset Keep falling back , What if the version in the remote warehouse is new to the version to be submitted locally ?
[root@supman git]# git push
Username for 'https://gitee.com': codeyuany
Password for 'https://codeyuany@gitee.com':
To https://gitee.com/codeyuany/git.git
! [rejected] master -> master (non-fast-forward)
error: Can't push some references to 'https://gitee.com/codeyuany/git.git'
Tips : Update denied , Because your current branch's latest commit is behind its corresponding remote branch .
Tips : Before pushing again , Merge with remote changes first ( Such as 'git pull'). See
Tips :'git push --help' Medium 'Note about fast-forwards' Section .
  1. Now there are two ways. One is git pull Pull down the new version from the warehouse to make the local version become a new version .
  2. It's mandatory submission git push --force Make the local old version directly overwrite the new version in the remote warehouse .

Generally, use the second method only when you are very sure , For example, in a piece of code, you will find that the previous version is correct , There is a problem with the current version .

The first method is the common method , It is often necessary to use... When submitting documents git pull Command to update the local version , For example, when your colleagues are right before you submit A The file has made some changes and has been submitted to the remote warehouse , You changed B file , Then, when submitting, you must first modify others A File first pull Come down and submit , Otherwise, you go straight to --force Have you directly covered other people's code before long ? Pay attention to this , Be careful to let your colleagues cut you with a knife .

Use git reflog Command can view all local warehouses ( Version area ) The changes to the , Include pullresetcommit

[root@supman git]# git reflog
f058de8 HEAD@{0}: pull: Fast-forward
1c07240 HEAD@{1}: pull: Fast-forward
a36b4ed HEAD@{2}: reset: moving to HEAD^
1c07240 HEAD@{3}: reset: moving to HEAD^
fda1d98 HEAD@{4}: reset: moving to HEAD^
d967d87 HEAD@{5}: commit: modify sencond.txt
fda1d98 HEAD@{6}: commit: modify first.txt
a36b4ed HEAD@{7}: commit: First submission
c55da94 HEAD@{8}: commit: Second submission
e58e53e HEAD@{9}: commit (initial): First submission 
According to reflog This information will switch the local warehouse to any version , The switching method can be version number switching, such as : git reset --hard d967d87 You can also use HEAD To switch, such as : git reset --hard HEAD@{1}
[root@supman git]# git reset --hard d967d87
HEAD It's now in d967d87 modify sencond.txt
[root@supman git]# git reflog
d967d87 HEAD@{0}: reset: moving to d967d87
f058de8 HEAD@{1}: pull: Fast-forward
1c07240 HEAD@{2}: pull: Fast-forward
a36b4ed HEAD@{3}: reset: moving to HEAD^
1c07240 HEAD@{4}: reset: moving to HEAD^
fda1d98 HEAD@{5}: reset: moving to HEAD^
d967d87 HEAD@{6}: commit: modify sencond.txt
fda1d98 HEAD@{7}: commit: modify first.txt
a36b4ed HEAD@{8}: commit: First submission
c55da94 HEAD@{9}: commit: Second submission
e58e53e HEAD@{10}: commit (initial): First submission
[root@supman git]# git reset --hard HEAD@{1}
HEAD It's now in f058de8 update second.txt.
[root@supman git]# git reflog
f058de8 HEAD@{0}: reset: moving to HEAD@{1}
d967d87 HEAD@{1}: reset: moving to d967d87
f058de8 HEAD@{2}: pull: Fast-forward
1c07240 HEAD@{3}: pull: Fast-forward
a36b4ed HEAD@{4}: reset: moving to HEAD^
1c07240 HEAD@{5}: reset: moving to HEAD^
fda1d98 HEAD@{6}: reset: moving to HEAD^
d967d87 HEAD@{7}: commit: modify sencond.txt
fda1d98 HEAD@{8}: commit: modify first.txt
a36b4ed HEAD@{9}: commit: First submission
c55da94 HEAD@{10}: commit: Second submission
e58e53e HEAD@{11}: commit (initial): First submission 
Please bring the original link to reprint ,thank
Similar articles

2021-09-15

2021-09-15