Git 常用命令速查

2026-11-15 · 工具

整理日常使用 Git 时最常用的一些命令,方便自己随时查阅。

初始化与配置

git init                                  # 在当前目录创建仓库
git config --global user.name "你的名字"
git config --global user.email "you@example.com"

日常提交流程

git status                # 查看当前状态
git add 文件名            # 加入暂存区(git add . 表示全部)
git commit -m "说明"      # 提交
git log --oneline         # 简洁查看提交历史

分支

git branch                # 查看分支
git switch -c feature-x   # 新建并切换分支
git switch main           # 切回主分支
git merge feature-x       # 合并分支

远程仓库

git clone 仓库地址
git pull                  # 拉取并合并远程更新
git push                  # 推送本地提交

撤销操作

git restore 文件名        # 丢弃工作区的修改
git restore --staged 文件名   # 取消暂存
git revert 提交ID         # 生成一个反向提交来撤销某次提交

提示:git reset --hard 会丢弃未提交的修改,使用前务必确认。

← 返回笔记列表