Basic commands of Git (learning notes)

I'm a sophomore. The purpose of writing this article is to make a certain record of my knowledge and technology, and I'm willing to share it with you. Because I'm still young, there are inevitably some mistakes and omissions in the published article. Please forgive me and correct me if you read this article. If you have any questions during reading, you can also ask them through comments. I will answer the questions according to my ability.


Because bloggers are learning git command-line operation recently, they want to record all kinds of GIT basic operation commands learned through this article for later review. Notes mainly include git upload, update, delete, branch, merge, conflict and other simple basic commands.

For command learning, bloggers learn through the free course "comprehensive introduction git" of teacher's "summer in May". If you want to systematically learn git commands, you can also try to watch the teacher's teaching video for learning. (the following is the link address corresponding to the course)

<Comprehensive introduction git>:



01 - set global user name and mailbox

git config --global "Bosen"
git config --global ""


02 - initialize warehouse

# Initialize local warehouse
git init
# Create a file
# Add the new file to the local staging area
git add
# Submit local staging area
git commit -m "first commit" 
# Configure remote warehouse
git remote add origin
# Upload local warehouse code to remote warehouse
git push -u origin master


03 - find submission record

#View current project changes
git status
# View submission records
git log
# Find submission records for the specified author
git log --author='Bosen'

① When no file changes: git status

② When a file changes: git status (those not added to the buffer are displayed in red, and those added to the buffer are displayed in green)

③ View submission record: git log

④ Find the submission record of the specified author Bosen: git log --author='Bosen '


04 - delete file

#Delete file
git rm demo.txt

① Delete manually:

② Command delete:


05 - rename file

git mv demo.txt demo111.txt

Manual renaming: (you also need to manually join the staging area)

Command rename: (no need to manually join the staging area)


06 - move files

git demo.txt home


07 - check the changes before and after the file

#Get commitID
git log --pretty=oneline demo.html
#Obtain file change information through commitID
git show [commitID
#View change information for all versions
git log -p demo.html

Get the change information of the specified version:

View change information for all versions:


08 - one click Restore

# View the difference between the current file and the last submitted one
git diff
#Restore the status of the last submission
git checkout -- demo.html

Viewing the file is different from the last submission

Restore the status of the last submission:


09 - undo tracking

# Undo tracking
git reset HEAD demo.demo.html


10 - fallback version

#How many [^], represents how many versions are backed back
git reset --hard HEAD^
# Fallback by version number
git reset --hard [commitID]
#A file returns to the specified version
git checkout [commitID] -- demo.html

Back to previous version:

Fallback by version number:

Specified file fallback to specified version:


11 - upload to remote warehouse

git push origin master


12 - create label

#Create a label for the current version
git tag v1.0
# View current version label
git tag
# Label the specified version
git tag v2.0 [commitID] 
#Delete label
git tag -d v1.0


13 - switch and delete branches

#Create branch
git branch dev
# View branch
git branch
# Switch branch
git checkout dev
#Delete branch
git branch -d dev
#Force delete branch
git branch -D dev

Create branch:

Switch branches:

Delete branch: (Note: the current branch and the branch commit ted but not push ed cannot be deleted. In the second case, forced deletion can be used!)


14 - Branch consolidation

# Merge branch
git merge dev

Merge branches:


15 - resolve conflicts during consolidation

#Ignore the code of other branches and keep the code of the current branch
git merge --abort

When the master and dev branches make different modifications to one line of code at the same time, a prompt that cannot be merged will appear when merging dev into the master (as follows)

At this point, you can see the line of code that caused the conflict as follows:

The first line HEAD represents the modified content of the current branch (master), and the second line represents the modified content of the dev branch. At this time, we programmers need to solve the conflict manually.

① Ignore the codes of other branches and keep the current branch code:

② Resolve manually (remove special symbols and choose to retain the code that needs to be retained after resolving the conflict)

Modified code:

At this time, use the command git status to find that the current file is added to the temporary storage area, and use the command git add Add the file to the staging area, and use the command git commit to submit and add conflict resolution instructions.

Use the command git commit to enter the editing page, add instructions, save and exit!

Use the command git log to view the submission information:


Route - view version 16

git log --oneline --graph


17 - delete branch of remote warehouse

git push origin --delete dev


18 - pull remote warehouse

#Pull default branch
git fetch
# Pull specified branch
git fetch origin dev

pull can be equal to fetch + merge or fetch + rebase according to different configurations (we'll know more later).


  👇 Scan QR code attention

Keywords: Python Java Linux git github

Added by greenie__ on Wed, 02 Feb 2022 08:26:57 +0200