Git常用命令大全
60+命令
一键复制
8大分类
收录60+条Git高频命令,按仓库管理、提交暂存、分支、远程同步、查看对比、回退撤销、标签、高级技巧分类,每条命令附带中文说明和实用示例,点击即可复制到剪贴板。
仓库管理
| 命令 | 说明 | 示例 | |
|---|---|---|---|
| git init | 初始化新仓库 | git init myproject | |
| 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 "z@test.com" | |
| git remote add origin <url> | 添加远程仓库 | git remote add origin https://github.com/user/repo.git | |
| git remote -v | 查看远程地址 | 查看当前配置的远程仓库URL |
提交与暂存
| 命令 | 说明 | 示例 | |
|---|---|---|---|
| git add <file> | 添加文件到暂存区 | git add index.html | |
| git add . | 添加所有改动到暂存区 | git add . | |
| git commit -m "msg" | 提交暂存区 | git commit -m "修复登录bug" | |
| git commit --amend | 修改上次提交 | git commit --amend -m "新消息" | |
| git stash | 暂存当前修改 | git stash save "临时保存" | |
| git stash pop | 恢复暂存的修改 | git stash pop | |
| git stash list | 查看暂存列表 | git stash list |
分支操作
| 命令 | 说明 | 示例 | |
|---|---|---|---|
| git branch | 查看本地分支 | * main 表示当前分支 | |
| git branch <name> | 创建新分支 | git branch feature/login | |
| git checkout <branch> | 切换分支 | git checkout develop | |
| git checkout -b <name> | 创建并切换分支 | git checkout -b feature/pay | |
| git switch <branch> | 切换分支(新语法) | git switch main | |
| git switch -c <name> | 创建并切换(新语法) | git switch -c hotfix/bug | |
| git merge <branch> | 合并分支到当前 | git merge feature/login | |
| git branch -d <name> | 删除分支 | git branch -d feature/done | |
| git rebase <branch> | 变基到目标分支 | git rebase main |
远程同步
| 命令 | 说明 | 示例 | |
|---|---|---|---|
| git fetch | 获取远程更新(不合并) | git fetch origin | |
| git pull | 拉取并合并远程 | git pull origin main | |
| git push | 推送到远程 | git push origin main | |
| git push -u origin <branch> | 首次推送并设置跟踪 | git push -u origin feature/x | |
| git push --force | 强制推送(危险) | 仅在确认时使用 | |
| git push --tags | 推送所有标签 | git push --tags |
查看与对比
| 命令 | 说明 | 示例 | |
|---|---|---|---|
| git status | 查看工作区状态 | git status | |
| git log | 查看提交历史 | git log --oneline -10 | |
| git log --graph | 图形化提交历史 | git log --graph --oneline | |
| git diff | 查看未暂存的修改 | git diff | |
| git diff --staged | 查看已暂存的修改 | git diff --staged | |
| git show <commit> | 查看某次提交详情 | git show a1b2c3d | |
| git blame <file> | 查看文件每行作者 | git blame main.py |
回退与撤销
| 命令 | 说明 | 示例 | |
|---|---|---|---|
| git reset --soft <commit> | 回退到指定提交(保留改动) | git reset --soft HEAD~1 | |
| git reset --mixed <commit> | 回退(取消暂存) | git reset --mixed HEAD~1 | |
| git reset --hard <commit> | 回退并丢弃所有改动 | git reset --hard HEAD~1 | |
| git revert <commit> | 生成反向提交(安全) | git revert a1b2c3d | |
| git checkout -- <file> | 丢弃文件修改 | git checkout -- index.html | |
| git clean -fd | 删除未跟踪的文件 | git clean -fd |
标签管理
| 命令 | 说明 | 示例 | |
|---|---|---|---|
| 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 "正式版" | |
| git push origin <tag> | 推送指定标签 | git push origin v1.0.0 | |
| git tag -d <name> | 删除本地标签 | git tag -d v0.9 |
高级技巧
| 命令 | 说明 | 示例 | |
|---|---|---|---|
| git cherry-pick <commit> | 挑选某次提交到当前分支 | git cherry-pick a1b2c3d | |
| git reflog | 查看所有HEAD变动记录 | 可找回"丢失"的提交 | |
| git bisect start | 二分查找引入bug的提交 | git bisect start HEAD v1.0 | |
| git submodule add <url> | 添加子模块 | git submodule add https://repo.git lib/ | |
| git worktree add <path> <branch> | 多工作树并行开发 | git worktree add ../hotfix hotfix/bug | |
| git alias | 设置命令别名 | git config --global alias.co checkout |
使用说明
- • 点击每行右侧 图标即可复制命令到剪贴板
- • 搜索框支持按命令名或中文说明模糊搜索
- •
<file>表示需要替换为实际文件名 - •
HEAD~1表示上一次提交,HEAD~2 表示上两次 - • 强制推送(--force)和硬重置(--hard)不可逆,请谨慎使用
