Quick check of Git common commands
This article organizes common Git commands according to the actual development process. in the example <branch>、<commit>、<file> Content such as content needs to be replaced with the real branch name, commit hash, or file path.
It is recommended to run the command to modify history or delete files before executing it git status, pass if necessary git stash -u Temporarily save current modifications.
Git Workflow
flowchart LR
A[工作区] -->|git add| B[暂存区]
B -->|git commit| C[本地仓库]
C -->|git push| D[远程仓库]
D -->|git fetch / git pull| C
B -->|git restore --staged| A
C -->|git restore / git reset| A
| regional | description | frequently used commands |
|---|---|---|
| workspace | Files currently being edited | git diff、git restore |
| staging area | What to include in the next submission | git add、git diff --staged |
| local repository | Submitted version history | git commit、git log |
| remote store | Remote copies of GitHub, GitLab, etc. | git fetch、git pull、git push |
Initialize and clone a warehouse
Initialize local warehouse
Initialize the Git repository in the current directory
git initInitialize and specify the default branch as main
git init -b mainClone remote warehouse
git clone https://github.com/<owner>/<repo>.gitgit clone git@github.com:<owner>/<repo>.gitClone to specified directory
git clone <repository-url> <directory>Clone only the most recent commit, suitable for scenarios where only the latest code is needed
git clone --depth 1 <repository-url>Configure Git
Set global user name
git config --global user.name "Your Name"Set up global mailbox
git config --global user.email "you@example.com"Set the default branch of the new warehouse to main
git config --global init.defaultBranch mainView all configurations and their sources
git config --list --show-originView the current valid username and email address for the warehouse
git config user.name
git config user.emailremove --global After, the configuration takes effect only for the current warehouse. Warehouse level configuration overrides global configuration.
Check warehouse status
View workspace and staging area status
git statusDisplay status and current branches in reduced format
git status -sbstaging file
temporary storage of specified file
git add <file>temporary storage specified directory
git add <directory>Temporarily store all additions, modifications and deletions in the current directory
git add .Interactive selection of code blocks to temporarily stage
git add -pRemove files from scratch, but keep workspace modifications
git restore --staged <file>Delete the file and add the delete operation to the scratch area
git rm <file>Stop tracking files, but keep local files
git rm --cached <file>Move or rename files
git mv <old-path> <new-path>Create Submit
Open the editor and fill in the submission instructions
git commitFill in the submission instructions directly
git commit -m "docs: update Git command guide"Temporary storage and submission of modifications to all tracked files
# 不包含尚未被 Git 跟踪的新文件
git commit -am "fix: update tracked files"Modify the most recent submission and re-edit the submission instructions
git commit --amendAdd the newly temporarily stored content to the latest submission and retain the original submission instructions
git commit --amend --no-editgit commit --amend A new commit hash is created. Submissions that have been pushed and used by others are not recommended to modify them at will.
operative branches
View branches
View local branches
git branchView local and remote branches
git branch -aView branches and their upstream tracking relationships
git branch -vvDisplay the current branch name
git branch --show-currentCreate and switch branches
Create a new branch from the current commit, but do not switch
git branch <branch>Create a new branch from a specified commit, branch, or label
git branch <branch> <start-point>Switch to existing branch
git switch <branch>Create a new branch and switch immediately
git switch -c <new-branch>Create a new branch from the specified starting point and switch immediately
git switch -c <new-branch> <start-point>Switch back to previous branch
git switch -Rename and delete branches
Rename the current branch
git branch -m <new-name>Rename the specified branch
git branch -m <old-name> <new-name>Delete merged local branches
git branch -d <branch>Force deleting local branches without checking whether they have been merged
git branch -D <branch>Delete remote branch
git push <remote> --delete <branch>Manage remote warehouses
View remote warehouse name and address
git remote -vAdd a remote warehouse
git remote add origin <repository-url>Add upstream warehouses to Fork projects
git remote add upstream <upstream-url>Modify remote warehouse address
git remote set-url origin <new-url>Rename remote warehouse
git remote rename <old-name> <new-name>Delete remote warehouse configuration
git remote remove <remote>View remote warehouse details
git remote show originGet, pull and push
Get new commits and references to the remote repository, but do not modify the current branch
git fetch originGet all remote warehouses and clean up deleted remote branch references
git fetch --all --prunePull the remote branch corresponding to the current branch and merge it
git pullOnly fast forward mergers are allowed to avoid automatic merge submissions
git pull --ff-onlyUse rebase to organize local submissions after pulling
git pull --rebasePush the branch for the first time and establish an upstream tracking relationship
git push -u origin <branch>Push current branch
git pushPush all local tags
git push origin --tagsDo not use it directly in ordinary collaboration git push --force。When you really need to rewrite the remote history, use it first git push --force-with-lease, it will refuse to overwrite if unexpected updates occur on a remote branch.
merge branches
Merge the specified branch into the current branch
git merge <branch>Create a merge commit even if you can fast forward
git merge --no-ff <branch>After a conflict occurs, view the conflict files
git statusAfter resolving conflicts and temporarily storing files, continue to complete the merge
git add <resolved-file>
git commitAbandon the current merge and return to the pre-merge state
git merge --abortvariable base branch
After reapplying the commit from the current branch to the target branch
git rebase <base-branch>Interactive sorting of the last 3 submissions
git rebase -i HEAD~3Resolve conflicts and temporarily store them and continue to base them
git add <resolved-file>
git rebase --continueSkip currently unapplicable commits
git rebase --skipAbandon rebasing and return to the original state
git rebase --abortRebase rewrites the commit history. Don't rebase a shared branch that has been made public and developed based on it.
Select and submit
Apply the specified commit from another branch to the current branch
git cherry-pick <commit>Continue cherry-pick after resolving the conflict
git add <resolved-file>
git cherry-pick --continueGive up this cherry-pick
git cherry-pick --abortUndo workspace and temporary area modifications
Discards modifications that have not yet been staged in the specified file
git restore <file>Restores files to the version in the specified commit
git restore --source=<commit> <file>Cancel temporary storage, but keep workspace modifications
git restore --staged <file>Restore the staging area and workspace to HEAD simultaneously
# 会丢弃所有未提交修改
git restore --staged --worktree :/Withdraw submission
Create a reverse commit
Create a new commit and undo the changes you made last commit
git revert HEADCreate a new commit and undo the changes specified for the commit
git revert <commit>git revert The original commit will not be deleted, which is suitable for history that has been pushed to the shared warehouse.
Move the current branch pointer
Undo the most recent commit, keep the changes and keep them in temporary status
git reset --soft HEAD~1Undo the most recent submission, keep the workspace modification but cancel temporary storage
git reset HEAD~1Restore all current branches, staging areas, and workspace to the specified commit
git reset --hard <commit>+++Danger:reset --hard with clean
git reset --hard Uncommitted modifications in the workspace and staging areas are discarded.
git clean -fd Untracked files and directories are permanently deleted. Use preview commands before executing:
Preview only what will be deleted
git clean -ndExecute after confirmation
git clean -fd+++
Use reflog to recover misoperations
View local movement records of HEAD and branch pointers
git reflogCreate a recovery branch from a commit before a mishandling for higher security
git switch -c recovery/<name> <commit>After confirming that the submission was correct, you can also directly restore the current branch
git reset --hard <commit>reflog It is a local record and will not follow push Uploading cannot directly restore operation records on other computers.
Temporary storage modification
Store changes to tracked documents
git stash push -m "WIP: description"While storing untracked documents
git stash push -u -m "WIP: description"View storage list
git stash listView detailed differences in recent storage
git stash show -pApply the most recent storage and remove it from the list
git stash popApplication specifies storage, but remains in list
git stash apply stash@{0}Delete specified storage
git stash drop stash@{0}Empty all storage
git stash clearView submission history
View the full submission history
git logDisplay full history with single lines, branch charts, and reference decorations
git log --oneline --graph --decorate --allView the latest 10 submissions
git log -10 --onelineView submissions from specified authors
git log --author="<name>"View submissions after the specified time
git log --since="2026-01-01"View file history and track renames
git log --follow -- <file>View specified submissions and differences
git show <commit>See which line of code was last modified by which submission
git blame -L <start>,<end> <file>Use binary to find location problems
git bisect Help locate submissions that introduce issues for the first time by constantly testing historical midpoints.
Start binary search
git bisect startMark the current submission as problematic
git bisect badMark a known normal historical commit as normal
git bisect good <good-commit>Git will automatically switch to intermediate commit. Continue marking after testing:
Current submission is normal
git bisect goodCurrent submission has a problem
git bisect badCurrent submission cannot be tested, skipped
git bisect skipView the current binary search process
git bisect logUsing gitk to visualize the current scope requires native installation of gitk
git bisect visualizeEnds binary search and returns to the branch before starting
git bisect resetAutomate binary lookup using test scripts
# 脚本退出码为 0 表示正常,1~127(除 125)表示有问题,125 表示跳过
git bisect run <test-command>management tag
View all tags
git tagCreate a note label
git tag -a v1.0.0 -m "Release v1.0.0"Create tags for specified submissions
git tag -a v1.0.0 <commit> -m "Release v1.0.0"View label information
git show v1.0.0Push designated label
git push origin v1.0.0Remove local tags
git tag -d v1.0.0Remove remote tags
git push origin --delete v1.0.0Sync Fork's upstream warehouses
first configured
# origin 指向自己的 Fork,upstream 指向原仓库
git remote add upstream <upstream-url>
git remote -vDaily synchronization
git fetch upstream --prune
git switch main
git merge --ff-only upstream/main
git push origin mainif the local main There is no own commit, and you cannot fast forward and merge. You can use it after confirming that the commit has not been shared:
git fetch upstream --prune
git switch main
git rebase upstream/main
git push --force-with-lease origin mainThe second way will rewrite yourself in Fork main History of the branch. When multiple people use the branch together, merge should be preferred over rebase.
Remove files ignored by.gitignore
Remove the Git tracking status of all files in the current directory (do not delete local files)
git rm -r --cached .Re-add all files to the staging area (Git will filter strictly according to the current.gitignore rules)
git add .Submit a new clean status
git commit -m "chore: 重新应用 .gitignore 规则并清理缓存"Cleanup historical submissions
installationgit-filter-repotool
pip install git-filter-repoRewrite the entire history of Git
git filter-repo --path 文件名或文件目录 --invert-paths --forceClean up residual objects
git reflog expire --expire=now --all
git gc --prune=now
If you enjoyed this, leave a comment~