整理日常使用 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 会丢弃未提交的修改,使用前务必确认。