🌿 Git 命令速查
基础配置
配置用户信息bash
git config --global user.name "Your Name"
git config --global user.email "you@example.com"配置编辑器bash
git config --global core.editor vim查看配置bash
git config --list别名配置bash
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit
git config --global alias.lg "log --oneline --graph --all"创建与克隆
初始化仓库bash
git init克隆仓库bash
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git my-dir
git clone -b dev https://github.com/user/repo.git # 指定分支暂存与提交
查看状态bash
git status添加到暂存区bash
git add file.txt # 单个文件
git add . # 所有文件
git add -u # 仅更新已跟踪的文件
git add *.md # 通配符提交bash
git commit -m "message" # 带消息提交
git commit -am "message" # 跳过 add,直接提交已跟踪文件
git commit --amend # 修改上一次提交从暂存区撤回bash
git restore --staged file.txt
git reset HEAD file.txt # 旧版本命令查看历史
查看提交历史bash
git log
git log --oneline # 一行显示
git log -5 # 最近5条
git log --graph # 图形化分支
git log --all # 所有分支
git log --author="name" # 按作者筛选
git log --since="2024-01-01" # 按时间筛选
git log -p file.txt # 查看文件的修改历史查看某次提交bash
git show commit_id
git show commit_id:file.txt比较差异bash
git diff # 工作区 vs 暂存区
git diff --staged # 暂存区 vs 最后提交
git diff HEAD # 工作区 vs 最后提交
git diff branch1 branch2 # 两个分支比较分支操作
查看分支bash
git branch # 本地分支
git branch -a # 所有分支(含远程)
git branch -r # 远程分支
git branch -v # 显示最后提交创建分支bash
git branch dev # 创建 dev 分支
git checkout -b dev # 创建并切换到 dev
git switch -c dev # 同上(新命令)切换分支bash
git checkout dev
git switch dev # 新命令
git checkout - # 切换回上一个分支
git switch - # 同上删除分支bash
git branch -d dev # 删除已合并的分支
git branch -D dev # 强制删除重命名分支bash
git branch -m old new合并与变基
合并bash
git merge dev # 将 dev 合并到当前分支
git merge --no-ff dev # 不使用快进合并
git merge --squash dev # 压缩成一个提交变基bash
git rebase main # 将当前分支变基到 main
git rebase -i HEAD~3 # 交互式变基(最近3个提交)变基中的操作bash
git rebase --continue # 继续变基
git rebase --abort # 中止变基
git rebase --skip # 跳过当前提交远程操作
查看远程仓库bash
git remote -v添加远程仓库bash
git remote add origin https://github.com/user/repo.git拉取bash
git fetch origin # 拉取远程更新,不合并
git pull origin main # 拉取并合并
git pull --rebase origin main # 拉取并变基推送bash
git push origin main # 推送到远程
git push -u origin main # 推送并设置上游
git push origin dev -f # 强制推送(谨慎!)
git push origin --delete dev # 删除远程分支撤销与回退
撤销工作区修改bash
git restore file.txt
git checkout -- file.txt # 旧命令撤回暂存bash
git restore --staged file.txt
git reset HEAD file.txt回退提交bash
git reset --soft HEAD~1 # 回退到上一次,改动保留在暂存区
git reset --mixed HEAD~1 # 回退到上一次,改动保留在工作区(默认)
git reset --hard HEAD~1 # 回退到上一次,所有改动丢弃!撤销某次提交(生成新提交)bash
git revert commit_id回到某个提交bash
git checkout commit_id # 临时查看(分离头指针)
git switch - # 返回贮藏(Stash)
保存工作进度bash
git stash
git stash save "message"
git stash -u # 包含未跟踪文件查看贮藏列表bash
git stash list应用贮藏bash
git stash apply # 应用最新的,保留在列表中
git stash pop # 应用最新的,并从列表移除
git stash apply stash@{1} # 应用指定的删除贮藏bash
git stash drop stash@{0}
git stash clear # 清空所有标签
创建标签bash
git tag v1.0.0 # 轻量标签
git tag -a v1.0.0 -m "Release 1.0" # 附注标签查看标签bash
git tag
git tag -l "v1.*" # 按模式过滤
git show v1.0.0推送标签bash
git push origin v1.0.0 # 推送单个标签
git push origin --tags # 推送所有标签删除标签bash
git tag -d v1.0.0
git push origin --delete v1.0.0 # 删除远程标签常见场景
放弃所有本地修改bash
git reset --hard HEAD
git clean -fd # 删除未跟踪文件临时切换分支,当前工作未完成bash
git stash
git switch other-branch
# ... 做完事情 ...
git switch original-branch
git stash pop将提交移动到另一个分支bash
# 假设在错误的分支上做了提交
git log # 记下 commit_id
git reset --hard HEAD~1 # 回退(如果还没 push)
git switch correct-branch
git cherry-pick commit_id # 把提交移过来修改最近一次提交的 messagebash
git commit --amend -m "new message"
# 如果已经 push 了,需要强制推送
git push -f撤销已 push 的提交bash
git revert commit_id # 生成一个反向提交
git push origin main # 推送上去
# (不建议 reset + force push 到公共分支)💡 更多开发工具速查 → Linux 常用命令 · V4L2 命令
