git进阶02_Git 核心概念与企业环境配置

Git 核心概念与企业环境配置

本章目标:理解 Git 的底层原理,配好企业环境,做到"知其然也知其所以然"。


一、Git 到底是什么?

很多同学把 Git 当成"保存代码的网盘",这是最大的误区。

工具 本质 类比
SVN 集中式版本控制,所有操作依赖中央服务器 共享文档服务器
Git 分布式版本控制,每个人拥有完整的仓库副本 每人都有一份完整的图书馆

为什么企业选 Git?

  1. 离线工作:断网也能 commit、看历史、切分支
  2. 分支极其廉价:创建/切换一个分支只需要改一个指针
  3. 数据完整性:每个 commit 都有 SHA-1 哈希,防篡改
  4. 协作灵活:支持多种协作模型(Git Flow / GitHub Flow / Trunk-based)

二、Git 三大区域(必须理解)

复制代码
工作区(Working Directory)  ←  你正在编辑的文件
        │ git add
        ▼
暂存区(Staging Area / Index)  ←  准备提交的快照
        │ git commit
        ▼
本地仓库(Local Repository)  ←  完整的版本历史
        │ git push
        ▼
远程仓库(Remote Repository)  ←  GitHub / GitLab / Gitea

实操理解

bash 复制代码
# 1. 创建一个新文件
echo "hello" > test.txt
git status          # 此时文件在"工作区",状态是 Untracked

# 2. 加入暂存区
git add test.txt
git status          # 状态变为 Staged(绿色)

# 3. 提交到本地仓库
git commit -m "feat: add test file"
git status          # 状态变为 Clean

# 4. 推送到远程
git push origin main

三、HEAD、分支、标签的本质

复制代码
HEAD  →  当前分支的指针
         │
         ▼
main  →  commit C3  →  commit C2  →  commit C1
              ↑
         feature/login  →  commit C4  →  commit C2

关键理解:

  • 分支只是一个指向 commit 的指针,不是文件夹的副本
  • git checkout feature/login 只是把 HEAD 指向 C4
  • 这就是为什么 Git 切分支那么快

标签(Tag)

bash 复制代码
# 给当前 commit 打标签(常用于发版)
git tag v1.0.0

# 带注释的标签(企业推荐)
git tag -a v1.0.0 -m "Release version 1.0.0"

# 推送标签到远程
git push origin v1.0.0

四、企业环境配置(入职第一天就要做)

4.1 安装 Git

bash 复制代码
# Windows
# 下载 https://git-scm.com/download/win
# 安装时选择 "Git from the command line and also from 3rd-party software"

# 验证安装
git --version

4.2 基础配置

bash 复制代码
# ===== 必须配置 =====
git config --global user.name "你的名字"          # 用英文名或拼音
git config --global user.email "your.email@chinaunicom.cn"  # 用公司邮箱

# ===== 企业推荐配置 =====
git config --global core.autocrlf true           # Windows 自动转换换行符
git config --global core.editor "code --wait"    # 默认编辑器用 VS Code
git config --global pull.rebase true             # pull 时默认 rebase(重要!)
git config --global push.default current         # push 只推当前分支
git config --global init.defaultBranch main      # 新仓库默认分支叫 main

# ===== 效率配置 =====
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --all --decorate"
git config --global alias.last "log -1 --stat"
git config --global alias.unstage "reset HEAD --"

4.3 SSH 密钥配置(企业必须)

bash 复制代码
# 生成密钥对(用公司邮箱)
ssh-keygen -t ed25519 -C "your.email@chinaunicom.cn"

# 查看公钥
cat ~/.ssh/id_ed25519.pub

# 把公钥添加到 GitLab / GitHub 的 SSH Keys 设置中
# 测试连接
ssh -T git@gitlab.your-company.com

4.4 企业 .gitignore 模板

bash 复制代码
# 创建全局 .gitignore
cat > ~/.gitignore_global << 'EOF'
# IDE
.idea/
.vscode/
*.swp
*.swo

# OS
.DS_Store
Thumbs.db
desktop.ini

# Build
node_modules/
dist/
build/
*.log
.env
.env.local

# Java
target/
*.class
*.jar

# Python
__pycache__/
*.pyc
.venv/
venv/

# Docker
docker-compose.override.yml
EOF

git config --global core.excludesfile ~/.gitignore_global

4.5 GPG 签名(可选,但大厂推荐)

bash 复制代码
# 生成 GPG 密钥
gpg --full-generate-key

# 查看密钥 ID
gpg --list-secret-keys --keyid-format=long

# 配置 Git 使用 GPG 签名
git config --global user.signingkey <你的密钥ID>
git config --global commit.gpgsign true

# 提交时会显示 "Verified" 标记
git commit -S -m "feat: signed commit"

五、.gitignore 的正确写法

常见错误

bash 复制代码
# ❌ 错误:已经在 Git 中的文件,加 .gitignore 没用
echo "node_modules/" >> .gitignore   # 这样做是错的

# ✅ 正确:先从 Git 中删除(保留本地文件)
git rm -r --cached node_modules/
git commit -m "chore: remove node_modules from tracking"

语法规则

gitignore 复制代码
# 忽略所有 .log 文件
*.log

# 但保留 important.log
!important.log

# 忽略 build 目录
/build/

# 忽略根目录下的 TODO 文件,但不忽略子目录的
/TODO

# 忽略 doc 目录下所有 .pdf 文件
doc/**/*.pdf

六、企业 Git 服务器选型

平台 特点 适用场景
GitLab 私有化部署、功能全 企业内网(最常见)
Gitea 轻量级、Go 语言 中小团队自建
GitHub 全球最大 开源项目
Bitbucket Atlassian 生态 Jira 用户

联通产业互联网大概率使用 GitLab 私有化部署,本文档后续内容以 GitLab 为基准。


七、Git 工作区状态速查

bash 复制代码
# 这是最常用的命令,必须熟练
git status

# 输出解读:
# On branch main
# Changes to be committed:       ← 已暂存(绿色)
#   new file:   index.html
# Changes not staged:             ← 已修改但未暂存(红色)
#   modified:   style.css
# Untracked files:                ← 新文件,未被追踪
#   app.js

完整状态流转图

复制代码
                    ┌─────────────────────────────────────┐
                    │           Untracked                   │
                    │      (新文件,Git 不知道)              │
                    └──────────────┬──────────────────────┘
                                   │ git add
                                   ▼
┌──────────┐    git modify    ┌──────────┐    git add    ┌──────────┐
│  Clean   │ ◄────────────── │ Modified  │ ────────────► │  Staged  │
│ (已提交)  │                  │ (已修改)   │               │ (已暂存)  │
└────┬─────┘                  └──────────┘               └────┬─────┘
     │                                                        │
     │ git checkout                                           │ git commit
     │ (丢弃修改)                                              │
     ▼                                                        ▼
   恢复到                                              ┌──────────┐
   最近提交                                              │ Committed│
                                                         └──────────┘

八、练习清单

学完本章,请完成以下操作:

  • 在公司 GitLab 上创建一个练习仓库
  • 配置好 SSH 密钥并测试连接
  • 克隆仓库,创建文件,走完 add → commit → push 流程
  • git log --oneline --graph 查看提交历史
  • 尝试 git diff 查看工作区和暂存区的差异
  • 配置好 .gitignore,确保 IDE 文件不被提交

下一章02-企业分支管理策略