Git 是现代开发必备的版本控制工具,本文整理日常高频命令。

基础操作

1
2
3
4
5
6
7
git init              # 初始化仓库
git clone <url> # 克隆远程仓库
git status # 查看状态
git add . # 暂存所有更改
git commit -m "msg" # 提交
git push # 推送到远程
git pull # 拉取远程更新

分支管理

1
2
3
4
5
git branch            # 查看分支
git branch <name> # 创建分支
git checkout <name> # 切换分支
git merge <name> # 合并分支
git branch -d <name> # 删除分支

撤销操作

1
2
3
git restore <file>    # 撤销工作区修改
git reset HEAD <file> # 取消暂存
git reset --soft HEAD~1 # 撤销最近一次 commit(保留更改)

查看历史

1
2
3
git log --oneline     # 简洁提交历史
git diff # 查看未暂存的更改
git diff --staged # 查看已暂存的更改

掌握以上命令可以应付 90% 的日常开发场景!