全部笔记All notes

Git 基本命令大全

阅读 9m 12s9m 12s read

概述

Git 是目前最流行的分布式版本控制系统,用于跟踪文件的变化并协调多人协作开发。本文档提供了 Git 的核心命令和最佳实践。

💡 学习目标: 掌握 Git 的基本操作,理解版本控制的核心概念,能够独立完成项目的版本管理。


环境配置

初始化用户信息

在开始使用 Git 前,需要配置用户信息:

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

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

# 保存凭证,避免重复输入密码
git config --global credential.helper store

# 设置默认编辑器
git config --global core.editor vim

# 设置默认分支名
git config --global init.defaultBranch main

查看配置信息

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

# 查看特定配置
git config user.name
git config user.email

# 查看配置来源
git config --show-origin user.name

🔧 配置层级:

  • --global: 全局配置,影响所有仓库
  • --local: 本地配置,仅影响当前仓库
  • --system: 系统配置,影响所有用户

仓库操作

创建仓库

# 在当前目录初始化仓库
git init

# 在指定目录初始化仓库
git init <project-name>

# 创建裸仓库(用于服务器)
git init --bare <project-name>

克隆仓库

# 克隆远程仓库
git clone <repository-url>

# 克隆到指定目录
git clone <repository-url> <directory-name>

# 克隆指定分支
git clone -b <branch-name> <repository-url>

# 浅克隆(只克隆最新提交)
git clone --depth 1 <repository-url>

基本概念

Git 的四个区域

区域描述位置
工作区实际编辑文件的地方项目根目录
暂存区临时存储待提交的文件.git/index
本地仓库存储项目的完整历史.git/ 目录
远程仓库托管在远程服务器的仓库GitHub、GitLab 等

文件状态

# 查看文件状态
git status

# 简洁模式显示
git status -s
状态描述标记
Untracked未被 Git 跟踪的新文件??
Modified已修改但未暂存 M
Staged已暂存待提交M
Committed已提交到本地仓库无标记

重要概念

概念说明
HEAD指向当前分支的最新提交
origin默认远程仓库别名
main/master主分支名称
HEAD~1上一个提交
HEAD~n上 n 个提交

特殊文件

文件作用
.git/Git 仓库的所有元数据
.gitignore忽略不需要跟踪的文件
.gitattributes文件属性配置
.gitkeep保持空目录被跟踪
.gitmodules子模块配置信息

文件操作

添加文件到暂存区

# 添加单个文件
git add <file>

# 添加多个文件
git add <file1> <file2>

# 添加当前目录所有文件
git add .

# 添加所有已跟踪文件的修改
git add -u

# 交互式添加
git add -i

# 添加文件的部分修改
git add -p <file>

提交更改

# 提交暂存区的文件
git commit -m "提交信息"

# 提交并自动暂存已修改的文件
git commit -am "提交信息"

# 修改最后一次提交
git commit --amend -m "新的提交信息"

# 空提交(触发 CI/CD)
git commit --allow-empty -m "触发构建"

文件操作

# 移动/重命名文件
git mv <old-file> <new-file>

# 删除文件
git rm <file>

# 只从暂存区删除,保留工作区
git rm --cached <file>

# 删除目录
git rm -r <directory>

查看差异

# 查看工作区与暂存区的差异
git diff

# 查看暂存区与最后提交的差异
git diff --cached

# 查看两次提交之间的差异
git diff <commit1> <commit2>

# 查看特定文件的差异
git diff <file>

# 查看统计信息
git diff --stat

分支管理

查看分支

# 查看本地分支
git branch

# 查看远程分支
git branch -r

# 查看所有分支
git branch -a

# 查看分支详细信息
git branch -v

# 查看已合并的分支
git branch --merged

# 查看未合并的分支
git branch --no-merged

创建和切换分支

# 创建分支
git branch <branch-name>

# 切换分支
git checkout <branch-name>

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

# 基于指定提交创建分支
git checkout -b <branch-name> <commit-id>

# 切换到上一个分支
git checkout -

合并分支

# 快速合并
git merge <branch-name>

# 禁用快速合并(保留分支历史)
git merge --no-ff <branch-name>

# 压缩合并(所有提交压缩为一个)
git merge --squash <branch-name>

# 合并指定提交
git cherry-pick <commit-id>

# 合并多个提交
git cherry-pick <commit1> <commit2>

变基操作

# 将当前分支变基到目标分支
git rebase <target-branch>

# 交互式变基(修改提交历史)
git rebase -i HEAD~3

# 继续变基
git rebase --continue

# 跳过当前提交
git rebase --skip

# 终止变基
git rebase --abort

删除分支

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

# 强制删除分支
git branch -D <branch-name>

# 删除远程分支
git push origin --delete <branch-name>

# 清理本地的远程分支引用
git remote prune origin

标签管理

# 创建轻量标签
git tag <tag-name>

# 创建带注释的标签
git tag -a <tag-name> -m "标签描述"

# 查看所有标签
git tag

# 查看标签信息
git show <tag-name>

# 推送标签到远程
git push origin <tag-name>

# 推送所有标签
git push origin --tags

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

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

撤销与恢复

撤销修改

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

# 撤销暂存区的修改
git reset HEAD <file>

# 撤销所有暂存区的修改
git reset HEAD .

# 使用 restore 命令(Git 2.23+)
git restore <file>
git restore --staged <file>

重置提交

# 软重置(保留工作区和暂存区)
git reset --soft <commit-id>

# 混合重置(保留工作区,清空暂存区)
git reset --mixed <commit-id>

# 硬重置(清空工作区和暂存区)
git reset --hard <commit-id>

# 重置到上一次提交
git reset --hard HEAD~1

反向提交

# 创建反向提交
git revert <commit-id>

# 反向提交不自动提交
git revert --no-commit <commit-id>

# 反向提交范围
git revert <commit1>..<commit2>

储藏操作

# 储藏当前工作
git stash

# 储藏包含未跟踪文件
git stash -u

# 储藏所有文件(包括忽略的)
git stash -a

# 储藏并添加描述
git stash save "工作进度说明"

# 查看储藏列表
git stash list

# 应用最近的储藏
git stash pop

# 应用指定的储藏
git stash pop stash@{2}

# 应用但不删除储藏
git stash apply

# 删除储藏
git stash drop stash@{2}

# 清空所有储藏
git stash clear

远程仓库

远程仓库管理

# 查看远程仓库
git remote -v

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

# 重命名远程仓库
git remote rename <old-name> <new-name>

# 删除远程仓库
git remote remove <remote-name>

# 修改远程仓库URL
git remote set-url <remote-name> <new-url>

# 查看远程仓库详细信息
git remote show <remote-name>

同步操作

# 获取远程更新(不合并)
git fetch <remote-name>

# 获取所有远程分支
git fetch --all

# 拉取并合并
git pull <remote-name> <branch-name>

# 拉取并变基
git pull --rebase <remote-name> <branch-name>

# 推送到远程
git push <remote-name> <branch-name>

# 推送并设置上游分支
git push -u <remote-name> <branch-name>

# 强制推送(危险操作)
git push --force-with-lease <remote-name> <branch-name>

# 推送所有分支
git push --all <remote-name>

工作流程

Git Flow 模型

Git Flow 是一种广泛使用的分支管理模型:

分支类型用途生命周期
master/main生产环境稳定版本永久
develop开发主分支永久
feature功能开发分支临时
release发布准备分支临时
hotfix紧急修复分支临时

典型工作流程

# 1. 克隆项目
git clone <repository-url>
cd <project-directory>

# 2. 创建功能分支
git checkout -b feature/new-feature

# 3. 开发功能
# ... 编写代码 ...
git add .
git commit -m "添加新功能"

# 4. 推送功能分支
git push -u origin feature/new-feature

# 5. 合并到主分支
git checkout main
git pull origin main
git merge feature/new-feature

# 6. 推送合并结果
git push origin main

# 7. 删除功能分支
git branch -d feature/new-feature
git push origin --delete feature/new-feature

实践示例

日常开发流程

# 开始新的工作日
git checkout main
git pull origin main

# 创建新功能分支
git checkout -b feature/user-authentication

# 开发过程中的提交
git add src/auth.js
git commit -m "添加用户认证模块"

git add tests/auth.test.js
git commit -m "添加认证模块测试"

# 推送到远程
git push -u origin feature/user-authentication

# 合并到主分支
git checkout main
git pull origin main
git merge --no-ff feature/user-authentication
git push origin main

# 清理分支
git branch -d feature/user-authentication
git push origin --delete feature/user-authentication

修复紧急问题

# 从主分支创建热修复分支
git checkout main
git pull origin main
git checkout -b hotfix/critical-bug-fix

# 修复问题
git add .
git commit -m "修复关键安全漏洞"

# 推送热修复
git push -u origin hotfix/critical-bug-fix

# 合并到主分支
git checkout main
git merge hotfix/critical-bug-fix
git push origin main

# 合并到开发分支
git checkout develop
git merge hotfix/critical-bug-fix
git push origin develop

# 清理分支
git branch -d hotfix/critical-bug-fix
git push origin --delete hotfix/critical-bug-fix

常见问题

问题1:合并冲突

❓ 问题: 合并分支时出现冲突

解决方案:

# 查看冲突文件
git status

# 手动解决冲突后
git add <conflict-file>
git commit -m "解决合并冲突"

# 或者使用合并工具
git mergetool

问题2:误删分支

❓ 问题: 意外删除了重要分支

解决方案:

# 查看操作历史
git reflog

# 恢复删除的分支
git checkout -b <branch-name> <commit-id>

# 或者直接恢复
git branch <branch-name> <commit-id>

问题3:提交信息写错

❓ 问题: 提交信息有误需要修改

解决方案:

# 修改最后一次提交信息
git commit --amend -m "正确的提交信息"

# 修改历史提交信息
git rebase -i HEAD~3
# 在编辑器中将要修改的提交前的 pick 改为 edit
# 保存后执行
git commit --amend -m "新的提交信息"
git rebase --continue

问题4:推送被拒绝

❓ 问题: git push 被拒绝

解决方案:

# 先拉取最新代码
git pull --rebase origin main

# 解决冲突后推送
git push origin main

# 或者使用强制推送(谨慎使用)
git push --force-with-lease origin main

分支策略建议

  • 主干开发(推荐)
    • main 始终可发布,develop 可选
    • 新功能:feat/* 分支开发,经 PR 合并
    • 缺陷修复:fix/* 从 main 切出,回合并 develop
  • Git Flow(适合大型项目)
    • 长期分支:main、develop
    • 辅助分支:feature/*、release/*、hotfix/*
  • 保护规则
    • 保护 main:强制 PR、评审 + CI 通过
    • 合并策略:squash 或 rebase 保持历史整洁

冲突解决流程

# 1. 更新远端与本地
git fetch origin

# 2. 衍合或合并主干
git checkout feature/awesome
git rebase origin/main      # 或:git merge origin/main

# 3. 解决冲突并标记已解决
#   打开冲突文件,清理 <<<<<<< / ======= / >>>>>>>
git add <conflicted-file>

# 4. 继续操作
git rebase --continue       # 若使用 rebase
# 或
git commit                  # 若使用 merge

# 5. 推送(rebase 后需强推同名分支)
git push -u origin feature/awesome
git push -f origin feature/awesome

# 6. 发起 PR,等待 CI 通过与评审

相关文章

基础篇

进阶篇

  • Git 高级技巧 - 进阶操作
  • GitHub Actions - GitHub 自动化

工具篇

  • VS Code Git 集成 - 编辑器集成

团队协作

  • 代码审查流程 - 团队规范
  • 分支管理策略 - 协作模式

总结

Git 是现代软件开发必不可少的工具,掌握其核心命令和工作流程对提高开发效率至关重要。

🎯 核心要点

  • 基本操作: add、commit、push、pull 是日常必备
  • 分支管理: 合理使用分支提高协作效率
  • 历史管理: 善用 log、diff、blame 追踪变更
  • 冲突解决: 熟练处理合并冲突

🛠️ 最佳实践

  • 提交信息要清晰明确
  • 经常提交,保持提交粒度适中
  • 使用分支进行功能开发
  • 定期同步远程仓库