笔记

git

Git 命令速查

常用 Git 命令分类速查。

配置

命令 说明
git config --global user.name "Your Name" 设置全局用户名
git config --global user.email "you@example.com" 设置全局邮箱
git config --global core.editor vim 设置默认编辑器
git config --list 查看所有配置
git config --global core.autocrlf input 设置换行符处理(Linux/Mac)
git config --global init.defaultBranch main 设置默认分支名

初始化

命令 说明
git init 初始化本地仓库
git clone <url> 克隆远程仓库
git clone --depth 1 <url> 浅克隆(仅最新提交)
git clone -b <branch> <url> 克隆指定分支

分支

命令 说明
git branch 列出本地分支
git branch -a 列出所有分支(含远程)
git branch <name> 创建新分支
git checkout <branch> 切换分支
git checkout -b <branch> 创建并切换分支
git branch -d <branch> 删除已合并分支
git branch -D <branch> 强制删除分支
git merge <branch> 合并分支到当前分支
git switch <branch> 切换分支(Git 2.23+)
git switch -c <branch> 创建并切换(Git 2.23+)

提交

命令 说明
git status 查看工作区状态
git add <file> 添加文件到暂存区
git add . 添加所有变更到暂存区
git commit -m "msg" 提交变更
git commit -am "msg" 添加并提交已跟踪文件
git commit --amend 修改最近一次提交
git reset HEAD <file> 从暂存区撤销
git restore <file> 丢弃工作区修改

远程

命令 说明
git remote -v 查看远程仓库地址
git remote add origin <url> 添加远程仓库
git fetch origin 获取远程更新
git pull origin <branch> 拉取并合并
git push origin <branch> 推送到远程
git push -u origin <branch> 推送并设置上游分支
git push --force 强制推送(谨慎使用)
git remote set-url origin <new-url> 修改远程地址

日志

命令 说明
git log 查看提交历史
git log --oneline 简洁模式查看
git log --oneline --graph 图形化查看
git log -n 10 查看最近 10 条
git log --author="name" 按作者筛选
git log --since="2024-01-01" 按日期筛选
git log --stat 显示文件变更统计
git show <commit> 查看某次提交详情
git diff 查看未暂存的修改
git diff --staged 查看已暂存的修改

暂存

命令 说明
git stash 暂存当前修改
git stash list 查看暂存列表
git stash pop 恢复并删除最新暂存
git stash apply 恢复暂存(保留记录)
git stash drop 删除最新暂存
git stash clear 清空所有暂存
git stash save "msg" 暂存并添加备注

标签

命令 说明
git tag 列出所有标签
git tag v1.0.0 创建轻量标签
git tag -a v1.0.0 -m "release" 创建附注标签
git push origin v1.0.0 推送标签到远程
git push origin --tags 推送所有标签
git tag -d v1.0.0 删除本地标签
git checkout v1.0.0 切换到标签

变基

命令 说明
git rebase <branch> 将当前分支变基到目标分支
git rebase -i HEAD~3 交互式变基(最近 3 次)
git rebase --continue 继续变基(解决冲突后)
git rebase --abort 中止变基
git rebase --skip 跳过冲突提交

撤销

命令 说明
git reset --soft HEAD~1 撤销提交,保留变更到暂存区
git reset --mixed HEAD~1 撤销提交,保留变更到工作区(默认)
git reset --hard HEAD~1 撤销提交并丢弃变更(危险!)
git revert <commit> 创建反向提交(安全撤销)
git reflog 查看所有操作历史(救命用)
git reset --hard <commit-hash> 回退到指定提交

评论

暂无评论。

提交后需管理员审核通过才会显示