Correct posture of GIT command

Time:2021-9-26

Common git operations

Add all change files to the staging area git add
Add all change files and submit to local warehouse git commit - am [commit message]
Create a new branch and switch to the new branch git checkout - B [branch name]
Delete local branch git branch - D [branch name]
Delete remote branch git push origin -- delete [branch name]
Reset the staging area and work area, consistent with the last commit git reset -- hard
Reset the pointer of the current branch to the specified commit, and reset the staging area, but the workspace remains unchanged git reset [commitid]

1. Git workspace

Correct posture of GIT command

  • Workspace: workspace
  • Index / stage: staging area
  • Repository: warehouse area (or local warehouse)
  • Remote: remote warehouse

2. New code warehouse

#Create a git code base in the current directory
$ git init
 
#Create a new directory and initialize it as a git code base
$ git init [project-name]
 
#Download a project and its entire code history
$ git clone [url]

3. Configure Git

#Displays the current git configuration
$ git config --list
 
#Edit git profile
$ git config -e [--global]
 
#Set user information when submitting code
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"

4. Add / delete files

#Adds the specified file to the staging area
$ git add [file1] [file2] ...
 
#Adds the specified directory to the staging area, including subdirectories
$ git add [dir]
 
#Add all files in the current directory to the staging area
$ git add .

5. Code submission

#Submit staging area to warehouse area
$ git commit -m [message]
 
#Submit the specified files in the staging area to the warehouse area
$ git commit [file1] [file2] ... -m [message]
 
#Submit the changes in the workspace since the last commit and go directly to the warehouse area
$ git commit -a
 
#Show all diff information when submitting
$ git commit -v
 
#Use a new commit instead of the last commit
#If there are no new changes in the code, it is used to rewrite the submission information of the last commit
$ git commit --amend -m [message]
 
#Redo the last commit and include new changes to the specified file
$ git commit --amend [file1] [file2] ...

6. Branching

#List all local branches
$ git branch
 
#List all remote branches
$ git branch -r
 
#List all local branches和远程分支
$ git branch -a
 
#Create a new branch, but still stay in the current branch
$ git branch [branch-name]
 
#Create a new branch and switch to it
$ git checkout -b [branch]
 
#Create a new branch and establish a tracking relationship with the specified remote branch
$ git branch --track [branch] [remote-branch]
 
#Switch to the specified branch and update the workspace
$ git checkout [branch-name]
 
#Switch to previous branch
$ git checkout -
 
#Establish a tracking relationship between an existing branch and a specified remote branch
$ git branch --set-upstream [branch] [remote-branch]
 
#Merge the specified branch to the current branch
$ git merge [branch]
 
#Delete branch
$ git branch -d [branch-name]
 
#Delete remote branch
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

7. Label

#List all tags
$ git tag
 
#Create a new tag in the current commit
$ git tag [tag]
 
#Create a new tag and specify the commit
$ git tag [tag] [commit]
 
#Delete local tag
$ git tag -d [tag]
 
#Delete remote Tag
$ git push origin :refs/tags/[tagName]
 
#View tag information
$ git show [tag]
 
#Submit specified tag
$ git push [remote] [tag]
 
#Submit all tags
$ git push [remote] --tags

8. View information

#Show changed files
$ git status
 
#Displays the version history of the current branch
$ git log
 
#Displays the commit history and the files that change each time a commit occurs
$ git log --stat
 
#Search submission history according to keywords
$ git log -S [keyword]
 
#Displays each diff related to the specified file
$ git log -p [file]
 
#Show last 5 submissions
$ git log -5 --pretty --oneline
 
#Show differences between staging and workspace
$ git diff
 
#Displays the difference between the staging area and the previous commit
$ git diff --cached [file]
 
#Displays the difference between the workspace and the latest commit of the current branch
$ git diff HEAD
 
#Displays the difference between two submissions
$ git diff [first-branch]...[second-branch]
 
#Displays the metadata and content changes of a submission
$ git show [commit]
 
#Displays the files that have changed in a submission
$ git show --name-only [commit]
 
#Displays the contents of a file at the time of a submission
$ git show [commit]:[filename]
 
#Displays the most recent commits of the current branch
$ git reflog

9. Remote synchronization

#Download all changes to the remote warehouse
$ git fetch [remote]

#Show all remote warehouses
$ git remote -v

#Displays information about a remote warehouse
$ git remote show [remote]

#Add a new remote warehouse and name it
$ git remote add [shortname] [url]

#Retrieve the changes from the remote warehouse and merge with the local branch
$ git pull [remote] [branch]

#Upload specified local branch to remote warehouse
$ git push [remote] [branch]

#Forcibly push the current branch to the remote warehouse, even if there is a conflict
$ git push [remote] --force

#Push all branches to remote warehouse
$ git push [remote] --all
#Update remote branch list
$ git remote update origin -p

10. Rescind

#Restore the specified files in the staging area to the workspace
$ git checkout [file]
 
#Restore the specified file of a commit to the staging area and workspace
$ git checkout [commit] [file]
 
#Restore all files in the staging area to the workspace
$ git checkout .
 
#Reset the specified file in the staging area to be consistent with the last commit, but the workspace remains unchanged
$ git reset [file]
 
#Reset the staging area and workspace to be consistent with the last commit
$ git reset --hard
 
#Reset the pointer of the current branch to the specified commit, and reset the staging area, but the workspace remains unchanged
$ git reset [commit]
 
#Reset the head of the current branch to the specified commit, and reset the staging area and workspace at the same time, which is consistent with the specified commit
$ git reset --hard [commit]
 
#Reset the current head to the specified commit, but leave the staging area and workspace unchanged
$ git reset --keep [commit]
 
#Uncommitted changes will be removed temporarily and moved in later
$ git stash
$ git stash pop

11. Git reference manual

http://gitref.justjavac.com/i…

Recommended Today

Supervisor

Supervisor [note] Supervisor – H view supervisor command help Supervisorctl – H view supervisorctl command help Supervisorctl help view the action command of supervisorctl Supervisorctl help any action to view the use of this action 1. Introduction Supervisor is a process control system. Generally speaking, it can monitor your process. If the process exits abnormally, […]