Git 命令详解(2026 最新常用版)

Git 是目前最流行的分布式版本控制系统,由 Linux 之父 Linus Torvalds 开发。掌握 Git 命令能让你高效管理代码版本、协作开发、回滚错误等。

下面按使用场景分类整理最常用、最实用的 Git 命令,每条都包含:

  • 作用
  • 常用用法
  • 示例

(推荐先安装最新版 Git:https://git-scm.com/downloads

1. 配置 Git(必须先做)

git config --global user.name "你的名字"
git config --global user.email "你的邮箱@example.com"
git config --global core.editor vim          # 设置默认编辑器
git config --list                            # 查看所有配置
git config --global alias.st status          # 设置别名(git st = git status)

2. 初始化与克隆仓库

命令作用示例
git init在当前目录初始化本地仓库git init
git init --bare创建裸仓库(适合做远程仓库)git init --bare /path/to/repo.git
git clone <url>克隆远程仓库到本地git clone https://github.com/user/repo.git
git clone --depth=1 <url>浅克隆(只下载最新一次提交,速度快)git clone --depth=1 <url>
git clone -b <branch> <url>克隆指定分支git clone -b dev <url>

3. 查看状态与差异

命令作用示例
git status查看工作区、暂存区状态(最常用!)git status
git status -s简洁模式显示git status -s
git diff比较工作区 vs 暂存区git diff
git diff --cached比较暂存区 vs 上次提交git diff --cached
git diff HEAD比较工作区 vs 最后一次提交git diff HEAD
git diff <commit1>..<commit2>比较两个提交git diff abc123..def456

4. 文件操作(添加、删除、重命名)

命令作用示例
git add <file>添加文件到暂存区git add README.md
git add . 或 git add -A添加所有修改(推荐)git add .
git add -p交互式分块添加(精细控制)git add -p
git rm <file>删除文件并放入暂存区git rm old.txt
git rm --cached <file>取消追踪,但保留本地文件git rm --cached .env
git mv <old> <new>重命名/移动文件git mv a.txt b.txt

5. 提交操作

命令作用示例
git commit -m "描述"提交暂存区内容git commit -m "feat: 添加登录页面"
git commit -am "描述"自动 add 修改的文件并提交(不含新文件)git commit -am "fix: 修复bug"
git commit --amend修改上一次提交(消息或内容)git commit --amend -m "新消息"
git commit --amend --no-edit只改内容,不改消息git commit --amend --no-edit

6. 查看历史

命令作用示例
git log查看提交历史git log
git log --oneline --graph简洁 + 分支图形git log --oneline --graph
git log -p显示每次提交的差异git log -p
git log --author="名字"查看某人提交git log --author="张三"
git show <commit>查看某次提交详情git show abc123
git reflog查看所有 HEAD 移动记录(找回丢失 commit 神器)git reflog

7. 分支管理(最核心功能)

命令作用示例
git branch列出本地分支git branch
git branch -a列出所有分支(含远程)git branch -a
git branch <name>创建分支git branch feature/login
git checkout -b <name>创建并切换分支(旧方式)git checkout -b dev
git switch -c <name>(Git 2.23+ 推荐)创建并切换分支git switch -c dev
git checkout <branch> / git switch <branch>切换分支git switch main
git merge <branch>合并分支(普通合并)git merge dev
git merge --no-ff <branch>非快进合并(保留分支记录)git merge --no-ff dev
git rebase <branch>变基合并(让历史更线性)git rebase main
git branch -d <branch>删除已合并分支git branch -d old-branch
git branch -D <branch>强制删除git branch -D old-branch

8. 远程仓库操作

命令作用示例
git remote -v查看远程仓库git remote -v
git remote add origin <url>添加远程仓库git remote add origin https://...
git fetch获取远程最新内容(不合并)git fetch origin
git pull拉取并合并(= fetch + merge)git pull origin main
git push推送本地分支git push origin main
git push -u origin <branch>第一次推送并建立追踪git push -u origin main
git push origin --delete <branch>删除远程分支git push origin --delete old

9. 撤销与恢复(救命命令)

命令作用示例
git reset --soft HEAD~1撤销上次 commit(保留修改在暂存区)git reset --soft HEAD~1
git reset --mixed HEAD~1(默认)撤销 commit(修改回到工作区)git reset HEAD~1
git reset --hard HEAD~1彻底撤销(危险,会丢弃修改)git reset --hard origin/main
git revert <commit>撤销某次提交(生成新 commit,安全)git revert abc123
git checkout -- <file> / git restore <file>丢弃工作区单个文件修改git restore README.md
git restore .丢弃所有工作区修改git restore .
git clean -fd删除未跟踪文件/目录git clean -fd

10. 临时保存(Stash)

git stash                # 保存当前修改
git stash save "描述"    # 带描述保存
git stash list           # 查看所有 stash
git stash pop            # 恢复并删除最新 stash
git stash apply          # 恢复但不删除
git stash drop stash@{0} # 删除指定 stash

11. 标签(Tag)与 cherry-pick

命令作用示例
git tag v1.0打轻量标签git tag v1.0
git tag -a v1.0 -m "描述"打带注解标签git tag -a v1.0 -m "发布1.0"
git push origin v1.0推送单个标签git push origin v1.0
git push origin --tags推送所有标签git push origin --tags
git cherry-pick <commit>把某个 commit 应用到当前分支git cherry-pick abc123

12. 其他实用命令

  • git bisect:二分查找引入 bug 的 commit(调试神器)
  • git submodule:管理子模块
  • git worktree:多工作目录(同时开多个分支)
  • git archive:打包仓库

日常最常用 6 个命令(记住这 6 个就够 80% 场景)

  1. git status
  2. git add .
  3. git commit -m "xxx"
  4. git pull
  5. git push
  6. git checkout -b new-branch 或 git switch -c new-branch
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值