Quick check of Git common commands

Published 2026-07-30 11:19 Updated 2026-08-02 00:00 2416 words 13 min read ... Page views

This article collates commonly used commands in Git development, classifies them according to the actual workflow, covering core functions such as warehouse initialization, configuration, status view, file operations, branch management, merge and rebase, commit operations, historical traceability, label management, and emergency recovery. The content emphasizes operational safety, reminds you to avoid dangerous modifications to shared history (such as rebase, reset --hard), and recommends using tools such as reflog, cherry-pick, and git bisect to efficiently locate problems and manage changes.

Quick check of Git common commands

This article organizes common Git commands according to the actual development process. <branch>, <commit>, <file>, etc. in the example need to be replaced with the real branch name, commit hash, or file path.

Before executing the command to modify history or delete files, it is recommended to run git status first, and temporarily save the current modifications through git stash -u if necessary.

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
AreaDescriptionCommon Commands
WorkspaceFiles currently being editedgit diff, git restore
Temporary storage areaContents to be included in the next submissiongit add, git diff --staged
Local warehouseSubmitted version historygit commit, git log
remote warehouseGitHub, GitLab and other remote copiesgit fetch, git pull, git push

Initialize and clone a warehouse

Initialize local warehouse

Initialize the Git repository in the current directory

git init

Initialize and specify the default branch as main

git init -b main

Clone remote warehouse

git clone https://github.com/<owner>/<repo>.git
git clone git@github.com:<owner>/<repo>.git

Clone 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 main

View all configurations and their sources

git config --list --show-origin

View the current valid username and email address for the warehouse

git config user.name
git config user.email

After removing --global, the configuration will only take effect for the current warehouse. Warehouse level configuration overrides global configuration.

Check warehouse status

View workspace and staging area status

git status

Display status and current branches in reduced format

git status -sb

staging 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 -p

Remove 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 commit

Fill 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 --amend

Add the newly temporarily stored content to the latest submission and retain the original submission instructions

git commit --amend --no-edit

git commit --amend creates a new commit hash. 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 branch

View local and remote branches

git branch -a

View branches and their upstream tracking relationships

git branch -vv

Display the current branch name

git branch --show-current

Create 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 -v

Add 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 origin

Get, pull and push

Get new commits and references to the remote repository, but do not modify the current branch

git fetch origin

Get all remote warehouses and clean up deleted remote branch references

git fetch --all --prune

Pull the remote branch corresponding to the current branch and merge it

git pull

Only fast forward mergers are allowed to avoid automatic merge submissions

git pull --ff-only

Use rebase to organize local submissions after pulling

git pull --rebase

Push the branch for the first time and establish an upstream tracking relationship

git push -u origin <branch>

Push current branch

git push

Push all local tags

git push origin --tags

Do not use git push --force directly in ordinary collaboration. When you really need to overwrite the remote history, use git push --force-with-lease first, which will refuse to overwrite if unexpected updates occur on the 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 status

After resolving conflicts and temporarily storing files, continue to complete the merge

git add <resolved-file>
git commit

Abandon the current merge and return to the pre-merge state

git merge --abort

variable 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~3

Resolve conflicts and temporarily store them and continue to base them

git add <resolved-file>
git rebase --continue

Skip currently unapplicable commits

git rebase --skip

Abandon rebasing and return to the original state

git rebase --abort

Rebase 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 --continue

Give up this cherry-pick

git cherry-pick --abort

Undo 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 HEAD

Create a new commit and undo the changes specified for the commit

git revert <commit>

git revert will not delete the original commit, 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~1

Undo the most recent submission, keep the workspace modification but cancel temporary storage

git reset HEAD~1

Restore all current branches, staging areas, and workspace to the specified commit

git reset --hard <commit>
Hazardous operation: `reset --hard` and `clean`

git reset --hard discards uncommitted modifications in the workspace and scratch areas.

git clean -fd permanently deletes untracked files and directories. Use preview commands before executing:

Preview only what will be deleted

git clean -nd

Execute after confirmation

git clean -fd

Use reflog to recover misoperations

View local movement records of HEAD and branch pointers

git reflog

Create 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 is a local record and will not be uploaded with push, nor can operation records on other computers be directly restored.

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 list

View detailed differences in recent storage

git stash show -p

Apply the most recent storage and remove it from the list

git stash pop

Application specifies storage, but remains in list

git stash apply stash@{0}

Delete specified storage

git stash drop stash@{0}

Empty all storage

git stash clear

View submission history

View the full submission history

git log

Display full history with single lines, branch charts, and reference decorations

git log --oneline --graph --decorate --all

View the latest 10 submissions

git log -10 --oneline

View 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 helps locate submissions that introduce issues for the first time by constantly testing historical midpoints.

git bisect start

Mark the current submission as problematic

git bisect bad

Mark 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 good

Current submission has a problem

git bisect bad

Current submission cannot be tested, skipped

git bisect skip

View the current binary search process

git bisect log

Using gitk to visualize the current scope requires native installation of gitk

git bisect visualize

Ends binary search and returns to the branch before starting

git bisect reset

Automate binary lookup using test scripts

# 脚本退出码为 0 表示正常,1~127(除 125)表示有问题,125 表示跳过
git bisect run <test-command>

management tag

View all tags

git tag

Create 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.0

Push designated label

git push origin v1.0.0

Remove local tags

git tag -d v1.0.0

Remove remote tags

git push origin --delete v1.0.0

Sync Fork's upstream warehouses

first configured

# origin 指向自己的 Fork,upstream 指向原仓库
git remote add upstream <upstream-url>
git remote -v

Daily synchronization

git fetch upstream --prune
git switch main
git merge --ff-only upstream/main
git push origin main

If your own commit exists on the local main and cannot be fast-forwarded and merged, 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 main

The second method will rewrite the history of the main branch in your Fork. When multiple people use the branch together, merge should be preferred over rebase.

Remove files ignored by.gitignore

1.Remove the Git tracking status of all files in the current directory (do not delete local files)

git rm -r --cached .

2.Re-add all files to the staging area (Git will filter strictly according to the current.gitignore rules)

git add .

3.Submit a new clean status

git commit -m "chore: 重新应用 .gitignore 规则并清理缓存"

If you enjoyed this, leave a comment~

... Page views
© 2026 跨越星轨的客 @Hoshiumi
Powered by theme astro-koharu · Inspired by Shoka