TL
Tool Lab
💰捐赠
💰捐赠

Git Commands Cheat Sheet

Comprehensive Git command reference organized by workflow.

Setup & Config

git config --global user.name "Name"Set global username
git config --global user.email "email"Set global email
git config --listList all config settings
git initInitialize a new local repository
git clone <url>Clone a remote repository

Staging & Committing

git statusShow working tree status
git add <file>Stage a specific file
git add .Stage all changes
git commit -m "message"Commit staged changes with a message
git commit --amendModify the last commit
git diffShow unstaged changes
git diff --stagedShow staged changes

Branching

git branchList all local branches
git branch <name>Create a new branch
git checkout <branch>Switch to a branch
git checkout -b <branch>Create and switch to a new branch
git switch <branch>Switch to a branch (modern)
git switch -c <branch>Create and switch to a new branch (modern)
git branch -d <branch>Delete a merged branch
git branch -D <branch>Force-delete a branch
git merge <branch>Merge a branch into current branch
git rebase <branch>Rebase current branch onto another

Remote Repositories

git remote -vList remote connections
git remote add origin <url>Add a remote named origin
git fetchDownload remote changes without merging
git pullFetch and merge remote changes
git pull --rebaseFetch and rebase instead of merge
git push origin <branch>Push branch to remote
git push -u origin <branch>Push and set upstream tracking
git push --force-with-leaseSafe force push (checks remote)

History & Log

git logShow commit history
git log --oneline --graphCompact branch graph
git log -n 10Show last 10 commits
git log --author="name"Filter commits by author
git blame <file>Show who changed each line
git show <commit>Show details of a specific commit

Undoing Changes

git restore <file>Discard unstaged changes in a file
git restore --staged <file>Unstage a file
git reset HEAD~1Undo last commit, keep changes staged
git reset --soft HEAD~1Undo last commit, keep changes
git reset --hard HEAD~1Undo last commit, discard changes
git revert <commit>Create a new commit that undoes a commit
git stashStash uncommitted changes
git stash popApply and remove latest stash
git stash listList all stashes

Tags

git tagList all tags
git tag v1.0.0Create a lightweight tag
git tag -a v1.0.0 -m "Release"Create an annotated tag
git push origin --tagsPush all tags to remote
git tag -d v1.0.0Delete a local tag

关于本工具

Git命令参考提供了七个类别的基本Git命令快速查找索引:设置、暂存与提交、分支、远程仓库、历史、撤销更改和标签。

使用搜索框按命令名称或描述实时过滤。收藏此页面,需要快速查看语法时随时使用。

使用方法

  1. 在搜索框中输入命令名称或关键词(例如:commit、branch、remote)。
  2. 匹配的命令按类别实时过滤显示。
  3. 查看命令及其说明。
  4. 清空搜索框可恢复完整列表。

使用场景

开发者使用它在不离开浏览器的情况下快速回忆Git命令语法。初学者从分类结构中学习Git。在代码审查时快速查找特定git操作非常有用。

常见问题

  • 为什么命令描述是英文的?Git命令和标志是国际技术标准。保持英文可以防止技术术语被误译。
  • 包含所有Git命令吗?涵盖了最常用的命令。完整参考请查阅官方git文档。