git指令大全

1.按功能

1.1 基础操作

# 初始化仓库
git init

# 克隆远程仓库
git clone <repo_url>

# 查看当前状态
git status

# 添加全部修改到暂存区
git add .

# 提交到本地仓库
git commit -m "提交说明"

# 将本地分支推送到远程
git push 

# 查看提交历史(精简版~)
git log --oneline

# 查看文件修改差异
git diff

1.2 分支管理

# 创建新分支
git branch <branch_name>

# 切换分支
git checkout <branch_name>

# 创建并切换分支
git checkout -b <new_branch>

# 合并分支到当前分支
git merge <branch_name>

# 删除本地分支
git branch -d <branch_name>

# 强制删除未合并分支
git branch -D <branch_name>

# 查看所有分支(含远程)
git branch -a

1.3 远程仓库操作

# 添加远程仓库
git remote add <remote_name> <repo_url>

# 推送本地分支到远程
git push -u <remote_name> <branch_name>

# 强制推送(慎用!)
git push -f

# 拉取远程更新
git pull <remote_name> <branch_name>

# 获取远程分支但不合并
git fetch

# 删除远程分支
git push <remote_name> --delete <branch_name>

1.4 撤销操作

# 撤销工作区修改
git checkout -- <file>

# 撤销暂存区文件
git reset HEAD <file>

# 修改最后一次提交
git commit --amend

# 回退到指定提交(保留修改)
git reset --soft <commit_id>

# 彻底回退到指定提交(慎用!)
git reset --hard <commit_id>

# 恢复误删的文件
git checkout <commit_id> -- <file_path>

1.5 日志与查询

#显示提交历史,支持多种格式和过滤选项(如--oneline, --graph, --author, --since, --grep等)
git log​ 

# 图形化提交历史
git log --graph --all

# 按作者搜索提交
git log --author="name"

# 搜索提交内容
git log -S "keyword"


#查找包含 "bugfix" 的所有提交
git log -S "bugfix" --oneline

#查找包含 "TODO" 或 "FIXME" 的提交
git log -G "TODO|FIXME" --oneline   
 
# 查看某个提交(或标签、分支)的详细信息及内容变更,默认显示最新提交。
git show​ 

#比较工作区、暂存区、任意两个提交之间的差异。常用变体:
git diff​
git diff — 工作区 vs 暂存区
git diff --cached — 暂存区 vs 最近提交
git diff <commit1> <commit2> — 两个提交之间的差异

#逐行显示文件的每一行最后是谁在哪个提交中修改的,常用于追溯代码来源。
git blame​ <file>

#在工作树或特定提交中搜索文本模式,类似命令行 grep 但针对 Git 仓库。
git grep​ 

#按作者分组汇总提交记录,常用于生成发布日志。
git shortlog​ 

#显示本地的引用日志(HEAD 移动的历史),可找回丢失的提交或恢复误操作。
git reflog​ 

#根据最近的标签描述当前提交的位置,常用于生成版本号。
git describe​ 

#二分法查找引入bug的提交,属于交互式调试查询工具。
git bisect​ 

#旧版命令,功能类似 git log --raw,现在推荐直接用 git log。
git whatchanged​ 

#虽主要用于打包,但结合 --output 也可用于查询特定版本的快照。
git archive​ 

1.6 标签管理

# 创建标签
git tag <tag_name>

# 创建带注释标签
git tag -a v1.0 -m "版本说明"

# 推送标签到远程
git push --tags

# 删除本地标签
git tag -d <tag_name>

# 删除远程标签
git push origin :refs/tags/<tag_name>

1.7 高级操作

# 贮藏当前修改
git stash

# 应用最近贮藏
git stash pop

# 交互式变基(修改最近3次提交)
git rebase -i HEAD~3

# 二分法查找问题提交
git bisect start
git bisect bad         # 标记当前为错误提交
git bisect good <id>   # 标记已知正常提交

# 清理未跟踪文件
git clean -fd

1.8 配置相关

# 全局用户名配置
git config --global user.name "Your Name"

# 全局邮箱配置
git config --global user.email "email@example.com"

# 查看所有配置
git config --list

# 设置别名(例如简化log)
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"

1.9 子模块管理

# 添加子模块
git submodule add <repo_url> <path>

# 初始化子模块
git submodule init

# 更新子模块
git submodule update

1.10 其他

# 忽略文件权限变更
git config core.fileMode false

# 生成.gitignore模板
curl https://gitignore.io/api/<语言/工具>

# 查看仓库大小
git count-objects -vH

# 克隆指定分支(浅克隆)
git clone --branch <branch_name> --depth 1 <repo_url>

2.按指令

2.1 查看所有指令

git help -a

2.2 查看某个指令的用法

git help cherry-pick

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值