Git 使用保姆级教程(2026)

📅 2026/7/9 16:24:53 👁️ 阅读次数 📝 编程学习
Git 使用保姆级教程(2026)

本文档详细记录了从配置到日常使用的完整 Git 工作流程

📋 目录


目录

    • 一、首次配置
      • 1.1 全局配置
      • 1.2 查看当前配置
    • 二、基础操作命令
      • 2.1 查看状态
      • 2.2 查看差异
      • 2.3 添加文件到暂存区
      • 2.4 提交
    • 三、分支操作
      • 3.1 查看分支
      • 3.2 创建分支
      • 3.3 切换分支
      • 3.4 删除分支
      • 3.5 重命名分支
    • 四、远程仓库操作
      • 4.1 查看远程仓库
      • 4.2 添加/修改远程仓库
      • 4.3 拉取远程代码
      • 4.4 推送代码到远程
    • 五、完整工作流程
      • 场景一:从 main 分支拉取最新代码,开发新功能
      • 场景二:更新 fork 的仓库
      • 场景三:合并分支
    • 六、常见问题与解决方案
      • 6.1 凭证问题
      • 6.2 分支冲突
      • 6.3 推送被拒绝
      • 6.4 撤销操作
      • 6.5 查看历史
      • 6.6 暂存工作
    • 七、常用命令速查表
    • 八、最佳实践
      • 提交信息规范
      • 注意事项

一、首次配置

1.1 全局配置

# 设置用户名gitconfig--globaluser.name"你的名字"# 设置邮箱gitconfig--globaluser.email"your_email@example.com"# 设置默认分支名gitconfig--globalinit.defaultbranch main# 启用凭证存储(避免每次输入密码)gitconfig--globalcredential.helper store

1.2 查看当前配置

gitconfig--list

二、基础操作命令

2.1 查看状态

# 查看工作目录状态gitstatus# 简洁模式gitstatus-s

2.2 查看差异

# 查看工作区 vs 暂存区gitdiff# 只看暂存区 vs 最新提交gitdiff--cached# 查看某个文件的改动gitdifffilename

2.3 添加文件到暂存区

# 添加单个文件gitaddfilename# 添加所有改动gitadd.# 添加所有已跟踪文件的改动(不包括新文件)gitadd-u# 交互式添加gitadd-i

2.4 提交

# 提交暂存区的改动gitcommit-m"提交信息"# 提交所有已跟踪文件的改动(跳过 git add)gitcommit-am"提交信息"# 修改最后一次提交(还没 push 的情况下)gitcommit--amend-m"新的提交信息"

三、分支操作

3.1 查看分支

# 查看本地分支(* 表示当前分支)gitbranch# 查看所有分支(包括远程)gitbranch-a# 查看分支详细信息gitbranch-v# 查看已合并到当前分支的分支gitbranch--merged# 查看未合并到当前分支的分支gitbranch --no-merged

3.2 创建分支

# 基于当前分支创建新分支gitbranch new-branch-name# 创建并切换到新分支gitcheckout-bnew-branch-name# 基于远程分支创建本地分支gitcheckout-blocal-branch-name origin/remote-branch-name# 使用 switch(现代命令,更清晰)gitswitch-cnew-branch-name# 创建并切换gitswitch existing-branch-name# 直接切换

3.3 切换分支

# 切换到已有分支gitcheckout branch-name# 切换到上一个分支gitcheckout -# 使用 switch(推荐)gitswitch branch-name

3.4 删除分支

# 删除已合并的分支gitbranch-dbranch-name# 强制删除分支(即使未合并)gitbranch-Dbranch-name# 删除远程分支gitpush origin--deleteremote-branch-name

3.5 重命名分支

# 重命名当前分支gitbranch-mnew-name# 重命名其他分支gitbranch-mold-name new-name

四、远程仓库操作

4.1 查看远程仓库

# 查看远程仓库地址gitremote-v# 查看远程仓库详细信息gitremote show origin

4.2 添加/修改远程仓库

# 添加远程仓库gitremoteaddorigin https://gitee.com/xxx/repo.git# 修改远程仓库地址gitremote set-url origin https://gitee.com/xxx/repo.git# 重命名远程仓库gitremoterenameorigin upstream

4.3 拉取远程代码

# 从远程拉取最新代码(不合并)gitfetch origin# 拉取并合并到当前分支gitpull origin branch-name# 简化写法(默认拉取当前分支对应的远程分支)gitpull# 拉取并rebase(保持提交历史线性)gitpull--rebaseorigin branch-name

4.4 推送代码到远程

# 推送当前分支到远程gitpush origin branch-name# 简化写法(推送当前分支)gitpush# 推送并设置上游分支(首次推送)gitpush-uorigin branch-name# 强制推送(谨慎使用!)gitpush--force

五、完整工作流程

场景一:从 main 分支拉取最新代码,开发新功能

# 1. 确保工作区干净,切换到 main 分支gitcheckout main# 2. 拉取远程最新代码gitpull origin main# 3. 创建并切换到新功能分支gitcheckout-bfeature/your-feature-name# 4. 开始开发...# 5. 查看改动gitstatusgitdiff# 6. 添加文件到暂存区gitadd.# 7. 提交gitcommit-m"feat: 添加新功能"# 8. 推送分支到远程(首次推送需要 -u)gitpush-uorigin feature/your-feature-name# 9. 在 Gitee/GitHub 上创建 Pull Request

场景二:更新 fork 的仓库

# 1. 添加上游仓库(如果还没有)gitremoteaddupstream https://xxx/upstream-repo.git# 2. 获取上游最新代码gitfetch upstream# 3. 切换到 main 分支gitcheckout main# 4. 合并上游代码gitmerge upstream/main# 5. 推送更新到自己的远程仓库gitpush origin main

场景三:合并分支

# 1. 切换到要合并到的分支(通常是 main)gitcheckout main# 2. 合并功能分支gitmerge feature/your-feature# 3. 解决冲突(如果有的话)# 编辑冲突文件,保留需要的代码gitaddresolved-file# 4. 完成合并提交gitcommit# 5. 推送到远程gitpush origin main

六、常见问题与解决方案

6.1 凭证问题

问题:每次 push 都要求输入用户名密码

解决方案

# 方法1:使用凭证存储(已配置)gitconfig--globalcredential.helper store# 方法2:使用 SSH 密钥ssh-keygen-ted25519-C"your_email@example.com"# 将 ~/.ssh/id_ed25519.pub 内容添加到 Gitee/GitHub# 方法3:使用 Gitee 令牌# 在 Gitee 设置 -> 私人令牌 中生成令牌# 然后使用格式:https://your_token@gitee.com/xxx/repo.git

6.2 分支冲突

问题:两个分支对同一文件做了不同的修改

解决方案

# 1. 打开冲突文件,会看到类似这样的标记:<<<<<<<HEAD 当前分支的代码=======要合并分支的代码>>>>>>>feature-branch# 2. 手动编辑,保留需要的代码,删除标记# 3. 添加文件gitaddfilename# 4. 完成合并gitcommit-m"解决冲突"

6.3 推送被拒绝

问题! [rejected] non-fast-forward

原因:远程分支有你本地没有的提交

解决方案

# 方案1:先拉取远程代码,再推送gitpull origin branch-name# 解决冲突后gitpush origin branch-name# 方案2:如果确定远程代码可以覆盖,使用强制推送(危险!)gitpush--forceorigin branch-name# 方案3:使用 rebase 保持历史整洁gitpull--rebaseorigin branch-name# 解决冲突后gitrebase--continue

6.4 撤销操作

# 撤销工作区的改动(未 add)gitcheckout -- filenamegitrestore filename# 撤销暂存区(已 add 但未 commit)gitreset HEAD filenamegitrestore--stagedfilename# 撤销最后一次提交(保留改动在工作区)gitreset--softHEAD~1# 撤销最后一次提交(保留改动在暂存区)gitreset--mixedHEAD~1# 撤销最后一次提交(完全丢弃)gitreset--hardHEAD~1

6.5 查看历史

# 查看提交历史gitlog# 简洁模式gitlog--oneline# 图形化显示分支gitlog--graph--oneline--all# 查看某个文件的修改历史gitlog-pfilename# 查看某次提交的具体改动gitshow commit-id

6.6 暂存工作

# 暂存当前工作(还没完成不想提交的改动)gitstash# 暂存并添加备注gitstash save"暂时保存:修改了 XX 功能"# 查看暂存列表gitstash list# 恢复最新暂存(同时保留暂存)gitstash apply# 恢复最新暂存(删除暂存)gitstash pop# 删除暂存gitstash drop stash@{0}

七、常用命令速查表

操作命令
查看状态git status
查看分支git branch -a
切换分支git checkout main
创建并切换git checkout -b new-branch
拉取更新git pull origin main
添加文件git add .
提交代码git commit -m "描述"
推送代码git push origin branch-name
查看历史git log --oneline
暂存工作git stash

八、最佳实践

提交信息规范

前缀用途示例
feat新功能feat: 添加用户登录功能
fix修复 bugfix: 修复登录页面闪退问题
docs文档更新docs: 更新 README
style格式调整style: 格式化代码
refactor重构refactor: 重构用户模块
test测试相关test: 添加单元测试
chore构建/工具chore: 更新依赖版本

注意事项

  • 提交粒度:每次提交只包含一个逻辑改动,便于回溯
  • 推送前:先git pull拉取最新代码,减少冲突
  • 谨慎操作
    • 永远不要强制推送 (--force) 到 main/master 分支
    • 删除分支前确认已合并
    • 重要操作前先git status确认当前状态

📝 文档创建时间:2026/07/08