Git常用命令速查手册

0 阅读7分钟

Git是程序员必备的版本控制工具,但命令太多记不住?本文整理了最常用的Git命令,按场景分类,建议收藏!


一、基础配置

1.1 用户配置

# 设置用户名
git config --global user.name "你的名字"

# 设置邮箱
git config --global user.email "your@email.com"

# 查看配置
git config --list

# 查看用户名
git config user.name

1.2 快捷命令(别名)

# 设置常用别名
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit

# 使用别名
git st   # 等同于 git status
git co   # 等同于 git checkout

二、仓库操作

2.1 创建仓库

# 初始化仓库
git init

# 克隆远程仓库
git clone <url>

# 克隆指定分支
git clone -b <branch> <url>

# 克隆到指定目录
git clone <url> <directory>

2.2 关联远程仓库

# 添加远程仓库
git remote add origin <url>

# 查看远程仓库
git remote -v

# 修改远程仓库地址
git remote set-url origin <new-url>

# 删除远程仓库关联
git remote remove origin

三、文件操作

3.1 添加到暂存区

# 添加单个文件
git add <file>

# 添加所有文件
git add .

# 添加所有修改的文件(不含新增)
git add -u

# 交互式添加
git add -p

3.2 提交更改

# 提交暂存区
git commit -m "提交信息"

# 提交所有更改(跳过add)
git commit -am "提交信息"

# 修改上次提交信息
git commit --amend -m "新的提交信息"

3.3 提交信息规范

<type>(<scope>): <subject>

type类型:
- feat: 新功能
- fix: 修复Bug
- docs: 文档更新
- style: 代码格式
- refactor: 重构
- test: 测试相关
- chore: 构建/工具相关

示例:
git commit -m "feat: 添加用户登录功能"
git commit -m "fix: 修复登录页面样式问题"

3.4 删除文件

# 删除文件(工作区+暂存区)
git rm <file>

# 删除文件(仅暂存区)
git rm --cached <file>

# 删除目录
git rm -r <directory>

四、分支管理

4.1 创建分支

# 创建新分支
git branch <branch-name>

# 创建并切换
git checkout -b <branch-name>

# 或使用switch命令(Git 2.23+)
git switch -c <branch-name>

# 从远程分支创建
git checkout -b <branch-name> origin/<branch-name>

4.2 查看分支

# 查看本地分支
git branch

# 查看所有分支(含远程)
git branch -a

# 查看远程分支
git branch -r

# 查看分支详细信息
git branch -vv

4.3 切换分支

# 切换分支
git checkout <branch-name>

# 或使用switch命令
git switch <branch-name>

# 切换到上一个分支
git checkout -

4.4 合并分支

# 合并指定分支到当前分支
git merge <branch-name>

# 不使用快进模式
git merge --no-ff <branch-name>

# 压缩合并
git merge --squash <branch-name>

4.5 删除分支

# 删除本地分支
git branch -d <branch-name>

# 强制删除未合并的分支
git branch -D <branch-name>

# 删除远程分支
git push origin --delete <branch-name>

五、远程操作

5.1 推送代码

# 推送到远程
git push origin <branch-name>

# 推送并设置上游分支
git push -u origin <branch-name>

# 推送所有分支
git push --all origin

# 强制推送
git push -f origin <branch-name>
git push --force-with-lease origin <branch-name>  # 更安全

5.2 拉取代码

# 拉取并合并
git pull origin <branch-name>

# 拉取并变基
git pull --rebase origin <branch-name>

# 仅拉取(不合并)
git fetch origin

六、撤销与回退

6.1 撤销工作区修改

# 撤销单个文件修改
git checkout -- <file>

# 或使用restore命令(Git 2.23+)
git restore <file>

# 撤销所有修改
git checkout -- .
git restore .

6.2 撤销暂存区

# 撤销暂存
git reset HEAD <file>

# 或使用restore命令
git restore --staged <file>

# 撤销所有暂存
git reset HEAD .

6.3 回退提交

# 回退到上一版本(保留修改)
git reset --soft HEAD^1

# 回退到上一版本(保留工作区修改)
git reset --mixed HEAD^1

# 回退到上一版本(丢弃所有修改)
git reset --hard HEAD^1

# 回退到指定版本
git reset --hard <commit-hash>

6.4 撤销已推送的提交

# 创建反向提交
git revert <commit-hash>

# 撤销多个提交
git revert <hash1> <hash2>

6.5 reset vs revert 对比

命令作用是否保留历史适用场景
reset --soft回退提交撤销commit
reset --mixed回退暂存撤销add
reset --hard完全回退放弃修改
revert创建反向提交已推送的提交

七、暂存工作

7.1 stash命令

# 暂存当前工作
git stash

# 暂存并添加说明
git stash save "工作说明"

# 包含未跟踪的文件
git stash -u

# 查看暂存列表
git stash list

# 查看暂存详情
git stash show
git stash show -p  # 显示差异

7.2 恢复暂存

# 恢复最近的暂存
git stash pop

# 恢复指定暂存
git stash apply stash@{0}

# 恢复但不删除
git stash apply

7.3 删除暂存

# 删除最近的暂存
git stash drop

# 删除指定暂存
git stash drop stash@{0}

# 删除所有暂存
git stash clear

八、标签管理

8.1 创建标签

# 创建轻量标签
git tag <tag-name>

# 创建附注标签(推荐)
git tag -a <tag-name> -m "标签说明"

# 给指定提交打标签
git tag -a <tag-name> <commit-hash> -m "标签说明"

8.2 查看标签

# 查看所有标签
git tag

# 查看标签信息
git show <tag-name>

# 搜索标签
git tag -l "v1.*"

8.3 推送标签

# 推送单个标签
git push origin <tag-name>

# 推送所有标签
git push origin --tags

8.4 删除标签

# 删除本地标签
git tag -d <tag-name>

# 删除远程标签
git push origin --delete <tag-name>

九、查看信息

9.1 查看状态

# 查看工作区状态
git status

# 简洁模式
git status -s
git status --short

# 查看被忽略的文件
git status --ignored

9.2 查看日志

# 查看提交日志
git log

# 单行显示
git log --oneline

# 图形化显示
git log --graph

# 显示最近N条
git log -n

# 显示文件变更
git log --stat

# 显示差异
git log -p

# 美化日志
git log --oneline --graph --all --decorate

9.3 查看差异

# 查看工作区差异
git diff

# 查看暂存区差异
git diff --staged
git diff --cached

# 查看两个提交差异
git diff <commit1> <commit2>

# 查看文件差异
git diff <file>

# 查看分支差异
git diff <branch1> <branch2>

9.4 查看提交历史

# 查看文件修改历史
git blame <file>

# 查看谁修改了某行
git blame -L 10,20 <file>

十、变基操作

10.1 基础变基

# 变基到main分支
git rebase main

# 交互式变基
git rebase -i HEAD~3

# 跳过当前冲突
git rebase --skip

# 放弃变基
git rebase --abort

10.2 变基 vs 合并

特点mergerebase
历史保留分支历史线性历史
提交创建新提交移动提交
冲突可能一次解决可能多次解决
安全更安全需谨慎

十一、解决冲突

11.1 冲突文件结构

<<<<<<< HEAD
当前分支的内容
=======
合并分支的内容
>>>>>>> branch-name

11.2 解决冲突步骤

# 1. 查看冲突文件
git status

# 2. 编辑冲突文件,手动解决冲突

# 3. 标记为已解决
git add <file>

# 4. 继续合并/变基
git merge --continue
git rebase --continue

十二、忽略文件

12.1 .gitignore语法

# .gitignore 文件语法

# 忽略特定文件
error.log

# 忽略目录
node_modules/
.DS_Store

# 忽略特定扩展名
*.log
*.tmp

# 忽略所有目录下的某文件
**/temp

# 例外(不忽略)
!important.log

# 忽略所有.c文件,除了test.c
*.c
!test.c

12.2 常见.gitignore模板

# Node.js
node_modules/
dist/
.env
*.log

# Python
__pycache__/
*.pyc
.venv/

# Java
target/
*.class
*.jar

# IDE
.idea/
.vscode/
*.iml

十三、常用场景

场景1:撤销最后一次提交

# 保留修改
git reset --soft HEAD~1

# 丢弃修改
git reset --hard HEAD~1

场景2:修改提交信息

# 修改最近一次提交信息
git commit --amend -m "新的提交信息"

场景3:合并多个提交

# 交互式变基
git rebase -i HEAD~3

# 将要合并的提交标记为 squash

场景4:恢复删除的分支

# 查看所有操作记录
git reflog

# 恢复分支
git checkout -b <branch-name> <commit-hash>

场景5:同步fork仓库

# 添加上游仓库
git remote add upstream <original-url>

# 拉取上游更新
git fetch upstream

# 合并到本地
git merge upstream/main

场景6:只提交部分修改

# 交互式添加
git add -p

# 选择要提交的修改块

场景7:查看某文件的修改历史

git log --follow -p <file>

十四、命令速查表

操作命令
初始化git init
克隆git clone
添加git add .
提交git commit -m "msg"
推送git push origin main
拉取git pull origin main
查看状态git status
查看日志git log --oneline
创建分支git checkout -b
切换分支git checkout
合并分支git merge
删除分支git branch -d
暂存git stash
恢复暂存git stash pop
回退git reset --hard

十五、常见问题

Q1:如何撤销git add?

git reset HEAD <file>
git restore --staged <file>  # Git 2.23+

Q2:如何撤销git commit?

git reset --soft HEAD~1  # 保留修改
git reset --hard HEAD~1  # 丢弃修改

Q3:如何修改最后一次提交?

git add <files>
git commit --amend --no-edit

Q4:如何解决冲突?

# 编辑冲突文件,保留需要的内容
# 标记为已解决
git add <file>
git commit

Q5:如何查看某个文件的修改历史?

git log -p <file>
git log --follow <file>  # 跟踪重命名

Q6:如何回退到指定版本?

git reset --hard <commit-hash>

Q7:如何恢复删除的提交?

git reflog
git reset --hard <commit-hash>

Q8:如何只拉取不合并?

git fetch origin

总结

本文整理了Git最常用的命令,涵盖日常开发的各个场景。建议:

  1. 收藏本文,随时查阅
  2. 理解原理,而不仅是记忆命令
  3. 多加练习,形成肌肉记忆

掌握这些命令,足以应对90%的Git使用场景!


你有其他常用的Git命令吗?欢迎在评论区分享!

本文首发于掘金,欢迎点赞收藏!


参考资料