git 常用命令

配置相关

复制代码
# 设置用户名
git config --global user.name "Your Name"
git config --local user.name "Your Name"  # 仅当前仓库

# 设置邮箱
git config --global user.email "email@example.com"

# 查看配置
git config --list                      # 所有配置
git config user.name                   # 查看用户名
git config user.email                  # 查看邮箱
git config --global --list             # 全局配置

仓库创建与克隆

复制代码
# 初始化新仓库
git init

# 克隆远程仓库
git clone <repository-url>            # 克隆到当前目录
git clone <url> <directory>           # 克隆到指定目录
git clone --branch <branch-name> <url> # 克隆特定分支

基础工作流

复制代码
# 检查状态
git status
git status -s                         # 简洁模式

# 添加到暂存区
git add <file>                        # 添加单个文件
git add .                             # 添加所有修改
git add -A                            # 添加所有变更(包括删除)
git add --patch                       # 交互式添加

# 提交
git commit -m "commit message"        # 提交暂存区
git commit -a -m "message"            # 添加并提交所有跟踪文件
git commit --amend                    # 修改最近一次提交

# 查看提交历史
git reflog														# 历史
git log                               # 完整历史
git log --oneline                     # 单行显示
git log --graph                       # 图形化显示分支
git log -p                            # 显示具体修改内容
git log --stat                        # 显示统计信息

分支管理

复制代码
# 查看分支
git branch                            # 本地分支
git branch -a                         # 所有分支(包括远程)
git branch -v                         # 查看分支最后提交

# 创建分支
git branch <branch-name>              # 创建分支
git checkout -b <branch-name>         # 创建并切换到分支

# 切换分支
git checkout <branch-name>
git switch <branch-name>              # Git 2.23+ 新方式

# 合并分支
git merge <branch-name>               # 合并分支到当前分支
git merge --no-ff <branch-name>       # 禁用快进合并

# 删除分支
git branch -d <branch-name>           # 删除已合并的分支
git branch -D <branch-name>           # 强制删除分支

# 重命名分支
git branch -m <new-name>              # 重命名当前分支
git branch -m <old-name> <new-name>   # 重命名指定分支

远程操作

复制代码
# 查看远程仓库
git remote                            # 列出远程仓库
git remote -v                         # 显示远程仓库URL
git remote show <remote>              # 显示远程仓库详情

# 添加远程仓库
git remote add <name> <url>           # 添加远程仓库

# 获取和拉取
git fetch <remote>                    # 获取远程更新
git fetch --all                       # 获取所有远程更新
git pull                              # 拉取并合并(git fetch + merge)
git pull --rebase                     # 拉取并使用rebase

# 推送
git push <remote> <branch>            # 推送到远程分支
git push -u <remote> <branch>         # 设置上游并推送
git push                              # 推送到默认上游
git push --force                      # 强制推送(慎用)

# 删除远程分支
git push <remote> --delete <branch>
git push <remote> :<branch>           # 旧语法

撤销与恢复

复制代码
# 取消暂存
git reset HEAD <file>                 # 取消暂存文件
git reset                             # 取消所有暂存

# 撤销工作区修改
git checkout -- <file>                # 撤销文件修改
git restore <file>                    # Git 2.23+ 新方式

# 重置提交
git reset --soft HEAD~1               # 撤销提交,保留修改
git reset --mixed HEAD~1              # 撤销提交,取消暂存(默认)
git reset --hard HEAD~1               # 撤销提交,删除修改(慎用)

# 恢复删除的文件
git checkout HEAD -- <file>           # 恢复已删除的文件

# 撤销合并
git merge --abort                     # 取消合并冲突

变基(Rebase)

复制代码
git rebase <branch>                   # 变基当前分支
git rebase --interactive HEAD~3       # 交互式变基最近3次提交
git rebase --abort                    # 中止变基
git rebase --continue                 # 继续变基

标签管理

复制代码
# 创建标签
git tag <tag-name>                    # 创建轻量标签
git tag -a <tag-name> -m "message"    # 创建带注释标签

# 查看标签
git tag                               # 列出所有标签
git show <tag-name>                   # 显示标签信息

# 推送标签
git push origin <tag-name>            # 推送单个标签
git push origin --tags                # 推送所有标签

# 删除标签
git tag -d <tag-name>                 # 删除本地标签
git push origin --delete <tag-name>   # 删除远程标签

查看与比较

复制代码
# 查看差异
git diff                              # 工作区与暂存区差异
git diff --staged                     # 暂存区与最新提交差异
git diff HEAD                         # 工作区与最新提交差异
git diff <commit1> <commit2>          # 两个提交之间的差异

# 查看具体文件历史
git log -- <file>                     # 文件的提交历史
git blame <file>                      # 逐行显示文件修改历史

清理

复制代码
git clean -n                          # 预览将被删除的文件
git clean -f                          # 删除未跟踪的文件
git clean -fd                         # 删除未跟踪的文件和目录
git clean -x                          # 包括忽略的文件

高级命令

复制代码
# 储藏(Stash)
git stash                             # 储藏当前修改
git stash save "message"              # 带消息储藏
git stash list                        # 查看储藏列表
git stash apply                       # 应用最新储藏
git stash pop                         # 应用并删除储藏
git stash drop                        # 删除储藏

# 子模块
git submodule add <url>               # 添加子模块
git submodule update --init --recursive # 初始化并更新子模块

# Cherry-pick
git cherry-pick <commit-hash>         # 应用某个提交

# 二分查找
git bisect start                      # 开始二分查找
git bisect good <commit>              # 标记为正确
git bisect bad <commit>               # 标记为错误
git bisect reset                      # 结束二分查找

常用组合命令

复制代码
# 日常更新工作流
git pull --rebase                     # 更新代码
git add .                             # 添加修改
git commit -m "message"               # 提交
git push                              # 推送

# 功能分支工作流
git checkout -b feature/xxx           # 创建功能分支
git add .                             # 开发完成后添加
git commit -m "add feature"           # 提交
git checkout main                     # 切换回主分支
git pull origin main                  # 更新主分支
git checkout feature/xxx              # 回到功能分支
git rebase main                       # 变基到主分支
git checkout main                     # 切换回主分支
git merge feature/xxx                 # 合并功能分支
git branch -d feature/xxx             # 删除功能分支

危险命令(慎用)

复制代码
git push --force                      # 强制推送,会覆盖远程历史
git reset --hard                      # 硬重置,会丢失所有未提交的修改
git clean -f                          # 强制清理未跟踪文件

帮助

复制代码
git help <command>                    # 查看命令帮助
git <command> --help                  # 查看命令帮助

记住:重要操作前先备份 ,特别小心 --force--hard 参数!

相关推荐
jiayong236 小时前
Detached HEAD 状态详解
git
李少兄16 小时前
在 IntelliJ IDEA 中修改 Git 远程仓库地址
java·git·intellij-idea
先跑起来再说21 小时前
Git 入门到实战:一篇搞懂安装、命令、远程仓库与 IDEA 集成
ide·git·后端·elasticsearch·golang·intellij-idea
承渊政道1 天前
Linux系统学习【Linux系统的进度条实现、版本控制器git和调试器gdb介绍】
linux·开发语言·笔记·git·学习·gitee
Doro再努力1 天前
【Linux操作系统12】Git版本控制与GDB调试:从入门到实践
linux·运维·服务器·git·vim
摇滚侠1 天前
MAC IDEA GIT 提交区显示了几个不存在的目录
git·idea
城东1 天前
Git使用[远程仓库远端的head比本地和提交的head旧,其他人拉不到最新代码]
git·head·远程仓库远端·比本地和提交的head旧·其他人拉不到最新代码
何中应2 天前
使用SSH地址拉取远程仓库代码报下面的错误
git
何中应2 天前
Git本地仓库命令补充
git