Git 命令速查

Git 常用命令速查表,按场景分类,常见问题速查。

初始化配置 6

git init

在当前目录初始化新仓库

$ git init

git clone <url>

克隆远程仓库到本地

$ git clone https://github.com/user/repo.git

git config --global user.name "<name>"

设置全局用户名

$ git config --global user.name "张三"

git config --global user.email "<email>"

设置全局邮箱

$ git config --global user.email "zhangsan@example.com"

git config --list

查看所有配置

$ git config --list

git config --global alias.<alias> <command>

设置命令别名

$ git config --global alias.st status

基础操作 12

git status

查看工作区和暂存区状态

$ git status

git add <file>

将文件添加到暂存区

$ git add README.md

git add .

将所有修改添加到暂存区

$ git add .

git commit -m "<msg>"

提交暂存区的修改

$ git commit -m "feat: 添加用户登录功能"

git commit -am "<msg>"

添加并提交已跟踪文件的修改

$ git commit -am "fix: 修复登录按钮样式"

git diff

查看未暂存的修改

$ git diff

git diff --staged

查看已暂存的修改

$ git diff --staged

git log

查看提交历史

$ git log

git log --oneline --graph

简洁图形化查看提交历史

$ git log --oneline --graph

git stash

暂存未提交的修改

$ git stash

git stash pop

恢复最近暂存的修改

$ git stash pop

git stash list

查看所有暂存记录

$ git stash list

分支管理 11

git branch

列出本地分支

$ git branch

git branch <name>

创建新分支

$ git branch feature/login

git checkout <branch>

切换分支

$ git checkout feature/login

git checkout -b <branch>

创建并切换到新分支

$ git checkout -b feature/login

git switch <branch>

切换分支(新命令)

$ git switch feature/login

git switch -c <branch>

创建并切换到新分支(新命令)

$ git switch -c feature/login

git branch -d <branch>

删除已合并的分支

$ git branch -d feature/login

git branch -D <branch>

强制删除分支

$ git branch -D feature/login

git merge <branch>

合并指定分支到当前分支

$ git merge feature/login

git rebase <branch>

变基合并(保持线性历史)

$ git rebase main

git cherry-pick <commit>

将指定提交应用到当前分支

$ git cherry-pick a1b2c3d

远程操作 9

git remote -v

查看远程仓库地址

$ git remote -v

git remote add origin <url>

添加远程仓库

$ git remote add origin https://github.com/user/repo.git

git fetch

拉取远程更新(不合并)

$ git fetch origin

git pull

拉取并合并远程更新

$ git pull origin main

git pull --rebase

拉取并变基合并

$ git pull --rebase origin main

git push

推送本地提交到远程

$ git push origin main

git push -u origin <branch>

推送并设置上游分支

$ git push -u origin feature/login

git push --force

强制推送(覆盖远程历史)

$ git push --force origin main

git remote remove <name>

移除远程仓库

$ git remote remove origin

撤销回退 9

git checkout -- <file>

撤销工作区修改

$ git checkout -- README.md

git restore <file>

撤销工作区修改(新命令)

$ git restore README.md

git restore --staged <file>

取消暂存(保留修改)

$ git restore --staged README.md

git reset HEAD <file>

取消暂存(保留修改)

$ git reset HEAD README.md

git reset --soft HEAD~1

回退提交(保留修改在暂存区)

$ git reset --soft HEAD~1

git reset --mixed HEAD~1

回退提交(保留修改在工作区)

$ git reset --mixed HEAD~1

git reset --hard HEAD~1

回退提交(丢弃所有修改)

$ git reset --hard HEAD~1

git revert <commit>

创建新提交来撤销指定提交

$ git revert a1b2c3d

git clean -fd

删除未跟踪的文件和目录

$ git clean -fd

标签管理 7

git tag

列出所有标签

$ git tag

git tag <name>

创建轻量标签

$ git tag v1.0.0

git tag -a <name> -m "<msg>"

创建附注标签

$ git tag -a v1.0.0 -m "正式版 1.0.0"

git push origin <tag>

推送标签到远程

$ git push origin v1.0.0

git push origin --tags

推送所有标签到远程

$ git push origin --tags

git tag -d <name>

删除本地标签

$ git tag -d v1.0.0

git push origin --delete <tag>

删除远程标签

$ git push origin --delete v1.0.0

信息查看 6

git show <commit>

查看提交详情

$ git show a1b2c3d

git blame <file>

查看文件每行的修改者

$ git blame README.md

git reflog

查看所有操作记录(找回丢失提交)

$ git reflog

git shortlog

查看提交者统计

$ git shortlog -sn

git ls-files

列出仓库中所有已跟踪文件

$ git ls-files

git describe

生成可读的版本描述

$ git describe --tags