Give yourself some time to remember these 200 git commands

Time:2021-12-8

When I usually use git, I don’t use many git commands very often. In work, we usually cooperate with some visualization tools or some plug-ins brought by the editor to maintain the GIT warehouse, but we should also remember some common git commands to adapt to some special situations. Here are some common and uncommon git commands I collected and sorted out, I hope it can help you better master the use of GIT. If the articles and notes can give you a little help or inspiration, please don’t be stingy with your praise and collection. Your will certainly be the biggest driving force for me to move forward

  • Note links are attached to read more high-quality articles in previous periods. You can check them step by step. You can praise and encourage me if you like:https://github.com/Wscats/CV/issues/31

newly build

Create a new git version library. The configuration, storage and other information of this version library will be saved to the. Git folder

#Initialize current project
$ git init

#Create a new directory and initialize it as a git code base
$ git init [project-name]

#Create an empty git repository in the specified directory. Running this command creates an empty directory called directory that contains only. Git subdirectories.

$ git init --bare <directory>

#Download a project and its entire code history
#This command is to copy a version library to another directory, and also copy all branches to the new version library. This allows you to commit to the remote branch in the new version library
$ git clone [url]

to configure

Change settings. It can be the setting of version library, system or global

#Displays the current git configuration
$ git config --list

#Edit git profile
$ git config -e [--global]

#Output and set basic global variables
$ git config --global user.email
$ git config --global user.name

$ git config --global user.email "[email protected]"
$ git config --global user.name "My Name"

#Define the author mailbox used by all submissions of the current user.
$ git config --global alias.<alias-name> <git-command>

#Create a shortcut (alias) for the GIT command.
$ git config --system core.editor <editor>

help

Git has built-in a very detailed explanation of the command, which can be consulted quickly

#Find available commands
$ git help

#Find all available commands
$ git help -a

#Find specific commands in the document
#Git help < command >
$ git help add
$ git help commit
$ git help init

state

Displays the difference between the index file (that is, the current workspace) and the submission pointed to by the current header pointer

#Show branches, untracked files, changes and other differences
$ git status

#Check the usage of other git status
$ git help status

information

Get git information such as some files, some branches, a submission, etc

#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]

#All changes after a commit are displayed, and each commit occupies one line
$ git log [tag] HEAD --pretty=format:%s

#Display all changes after a commit, and its "submission description" must meet the search criteria
$ git log [tag] HEAD --grep feature

#Displays the version history of a file, including file renaming
$ git log --follow [file]
$ git whatchanged [file]

#Displays each diff related to the specified file
$ git log -p [file]

#Show last 5 submissions
$ git log -5 --pretty --oneline

#Displays all submitted users, sorted by submission times
$ git shortlog -sn

#Displays who modified the specified file and when
$ git blame [file]

#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]

#Shows how many lines of code you wrote today
$ git diff --shortstat "@{0 day ago}"

#Compare staging and version library differences
$ git diff --staged

#Compare staging and version library differences
$ git diff --cached

#Compare statistics only
$ git diff --stat

#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

#View remote branches
$ git br -r

#Create a new branch
$ git br <new_branch>

#View the last submission information of each branch
$ git br -v

#View branches that have been merged into the current branch
$ git br --merged

#View branches that have not been merged into the current branch
$ git br --no-merged

add to

Add files to the current workspace. If you don’t usegit addIf you add files, they will not be added to subsequent submissions

#Add a file
$ git add test.js

#Add files in a subdirectory
$ git add /path/to/file/test.js

#Support regular expressions
$ git add ./*.js

#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 .

#Confirmation is required before adding each change
#For multiple changes in the same file, you can submit it in batches
$ git add -p

delete

RM, in contrast to the add command above, removes a file from the workspace

#Remove helloworld.js
$ git rm HelloWorld.js

#Remove files from subdirectories
$ git rm /pather/to/the/file/HelloWorld.js

#Delete the workspace file and put the deletion into the staging area
$ git rm [file1] [file2] ...

#Stops tracking the specified file, but the file remains in the workspace
$ git rm --cached [file]

branch

To manage branches, you can add, delete, modify, query and switch branches through the following commands

#View all branches and remote branches
$ git branch -a

#Create a new branch
$ git branch [branch-name]

#Rename branch
#Git branch - M < old name > < new name >
$ git branch -m [branch-name] [new-branch-name]

#Introduction to editing branches
$ git branch [branch-name] --edit-description

#List all local branches
$ git branch

#List all remote branches
$ git branch -r

#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 point to the specified commit
$ git branch [branch] [commit]

#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]

#Select a commit to merge into the current branch
$ git cherry-pick [commit]

#Delete branch
$ git branch -d [branch-name]

#Delete remote branch
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

#Switch to a branch
$ git co <branch>

#Create a new branch and switch to the past
$ git co -b <new_branch>

#Create new based on branch_ branch
$ git co -b <new_branch> <branch>

#A history submission record is checked out, but there is no branch information. Switching to other branches will automatically delete it
$ git co $id

#Check out a history submission record and create a branch
$ git co $id -b <new_branch>

#Delete a branch
$ git br -d <branch>

#Forcibly delete a branch (it is necessary to forcibly delete an unconsolidated branch)
$ git br -D <branch>

detection

Updates the current workspace to the workspace identified by the index or a specific workspace

#Check out a version library, which will be updated to the master branch by default
$ git checkout
#Check out a specific branch
$ git checkout branchName
#Create a new branch and switch to the past, which is equivalent to "git branch < name >; git checkout < name >"
$ git checkout -b newBranch

Remote synchronization

Remote branch of 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]

#View remote server address and warehouse name
$ git remote -v

#Add remote warehouse address
$ git remote add origin [email protected] github:xxx/xxx.git

#Set remote warehouse address (used to modify remote warehouse address)
$ git remote set-url origin [email protected] github.com:xxx/xxx.git

#Delete remote warehouse
$ git remote rm <repository>

#Upload specified local branch to remote warehouse
#Update the local branch to the master branch of the remote origin
#Git push < remote > < Branch >
#Git push is equivalent to git push origin master
$ 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

revoke

#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]

#Create a new commit to revoke the specified commit
#All changes in the latter will be offset by the former and applied to the current branch
$ git revert [commit]

#Restore the last submitted state
$ git revert HEAD

#Uncommitted changes will be removed temporarily and moved in later
$ git stash
$ git stash pop

#Column all stash
$ git stash list

#Recover staged content
$ git stash apply

#Delete staging area
$ git stash drop

commit

Save the changes to the current index as a new submission, which includes the changes and information made by the user

#Submit staging area to warehouse area with submission information
$ 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] ...

diff

Displays the difference between the current workspace and the submitted

#Displays the differences between the working directory and the index
$ git diff

#Displays the difference between the index and the last submitted
$ git diff --cached

#Displays the difference between the working directory and the last submitted
$ git diff HEAD

grep

You can quickly find it in the version library

Optional configuration:

#Thank Travis Jeffery for the following usage:
#Show line numbers in search results
$ git config --global grep.lineNumber true

#Yes, the search results are more readable
$ git config --global alias.g "grep --break --heading --line-number"
#Find variablename in all Java
$ git grep 'variableName' -- '*.java'

#Search all rows containing "arraylistname" and "add" or "remove"
$ git grep -e 'arrayListName' --and \( -e add -e remove \)

log

Show all submissions for this repository

#Show all submissions
$ git log

#Show some submission information
$ git log -n 10

#Show merge submissions only
$ git log --merges

#View each submission record of the file
$ git log <file>

#View diff of each detailed modification
$ git log -p <file>

#View the diff of the last two detailed modifications
$ git log -p -2

#View submission statistics
$ git log --stat

merge

Merging is to merge external submissions into their own branches

#Merge other branches into the current branch
$ git merge branchName

#Create a new merged commit when merging
#Do not fast forward merge, which can generate merge submissions
$ git merge --no-ff branchName

mv

Rename or move a file

#Rename
$ git mv test.js test2.js

#Move
$ git mv test.js ./new/path/test.js

#Rename the file and put the rename in the staging area
$ git mv [file-original] [file-renamed]

#Force rename or move
#This file already exists and will be overwritten
$ git mv -f myFile existingFile

tag

#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

#Create a new branch to point to a tag
$ git checkout -b [branch] [tag]

pull

Merge from remote repository to current branch

#Update the version library from the master branch of the remote origin
#Git pull < remote > < Branch >
$ git pull origin master

#Grab all branch updates from the remote warehouse and merge them locally. Do not fast forward merge
$ git pull --no-ff

ci

$ git ci <file>
$ git ci .
#Combine git add, GIT RM and git CI
$ git ci -a
$ git ci -am "some comments"
#Modify the last submission record
$ git ci --amend

Rebase (use with caution)

Apply all commit history on one branch to another
_ Do not use rebase_. On a remote branch that is already exposed

#Apply experimentbranch to master
# git rebase <basebranch> <topicbranch>
$ git rebase master experimentBranch

Reset (use with caution)

Resets the current header pointer to a specific state. This allows you to undo merge, pull, commit, add, etc
This is a very powerful command, but you must be aware of its consequences when using it

#Restore the staging area to the state of the last submission without changing the current working directory
$ git reset

#Restore the staging area to the state of the last submission and overwrite the current working directory
$ git reset --hard

#Restore the current branch to a submission without changing the current working directory
#All changes still exist in the working directory
$ git reset dha78as

#Restore the current branch to a submission and overwrite the current working directory
#And delete all uncommitted changes and all submissions after the specified submission
$ git reset --hard dha78as

other

#Generate a compressed package for publishing
$ git archive

#Patch
$ git apply ../sync.patch

#Test whether the patch is successful
$ git apply --check ../sync.patch

#View git version
$ git --version

Reference documents

Recommended Today

Heavyweight Tencent cloud open source industry’s first etcd one-stop governance platform kstone

​ Kstone open source At the kubecon China Conference held by CNCF cloud native foundation on December 9, 2021,Tencent cloud container tke team released the open source project of kstone etcd governance platform. KstoneIt was initiated by the TKE team of Tencent cloud containerCloud native one-stop etcd governance project based on kubernetes。 The project originates […]