版本控制系统(VCS)记录文件内容变化,让你可以回溯历史、比较差异、协作开发、分支实验。没有VCS的团队只能靠文件名备份——report_v2_final_final2.docx就是前车之鉴。版本控制是现代软件开发的基石,不只是代码——文档、配置、设计稿都需要版本控制。
| 时代 | 代表工具 | 架构模型 | 致命缺陷 |
|---|---|---|---|
| 本地VCS | RCS (1982) | 本地补丁数据库 | 无法协作,单人使用 |
| 集中式CVCS | CVS/SVN/Perforce | 单一中央服务器 | 单点故障、必须联网、分支昂贵 |
| 分布式DVCS | Git/Mercurial (2005) | 每个克隆=完整仓库 | 学习曲线较陡峭 |
2005年,Linux内核团队使用的商业工具BitKeeper撤销了免费授权。Linus Torvalds评估了SVN、Monotone等工具后都不满意——要么太慢、要么分支模型不好、要么不支持离线。他花了约2周时间自己写了Git。
核心优势:速度(几乎所有操作都是本地操作,不需要网络通信)、完整性(SHA-1校验和保证数据不被篡改或损坏)、分支模型(创建/切换分支几乎零开销)、离线工作(在飞机上、地铁里也能commit)、生态系统(GitHub/GitLab/Gitee等托管平台)。
理解三大区域是掌握Git的基础。几乎所有Git操作都是在三个区域间移动数据:工作目录是你看得见摸得着的文件,暂存区是你精心准备的下次提交快照,仓库是所有历史版本的永久存储。
.git/index二进制文件,保存了下次将提交的文件快照列表。这个设计让你可以精确控制每次提交包含哪些改动——你可以只提交某个文件的部分修改git clone就是复制这个目录$ sudo apt update && sudo apt install git -y # Debian/Ubuntu
$ sudo add-apt-repository ppa:git-core/ppa # PPA最新版
$ sudo apt update && sudo apt install git -y
$ brew install git # macOS
winget install Git.Git # Windows
从源码编译安装(获取最新特性)
$ sudo apt install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev
$ wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.43.0.tar.gz
$ tar -zxf git-2.43.0.tar.gz && cd git-2.43.0
$ make prefix=/usr/local all && sudo make prefix=/usr/local install
$ git version
git version 2.43.0
$ which git
/usr/local/bin/git
$ git --exec-path
/usr/local/lib/git-core
$ man git
查看Git手册页(按q退出)
| 级别 | 文件位置 | 优先级 | 参数 |
|---|---|---|---|
| 仓库级 | .git/config | 最高(覆盖全局) | --local |
| 全局级 | ~/.gitconfig | 中 | --global |
| 系统级 | /etc/gitconfig | 最低 | --system |
$ git config --global user.name "Zhang San"
$ git config --global user.email "zhangsan@example.com"
$ git config --global init.defaultBranch main
$ git config --global color.ui auto
$ git config --global core.editor "vim"
$ git config --global core.autocrlf input # Linux/macOS
$ git config --global core.quotepath false # 中文文件名
实用别名
$ git config --global alias.st status
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.lg "log --oneline --graph --all --decorate"
$ git config --global alias.last "log -1 HEAD --stat"
$ git config --global alias.unstage "reset HEAD --"
验证
$ git config --list --show-origin
file:/home/user/.gitconfig user.name=Zhang San
file:/home/user/.gitconfig user.email=zhangsan@example.com
file:/home/user/.gitconfig alias.lg=log --oneline --graph --all
git st=git status,git unstage file=git reset HEAD -- file。也可以直接编辑~/.gitconfig的[alias]节来批量设置。$ git init demo && cd demo
Initialized empty Git repository in /tmp/demo/.git/
$ find .git -type f | head -15
.git/HEAD
.git/config
.git/description
.git/hooks/pre-commit.sample
.git/hooks/pre-push.sample
.git/hooks/prepare-commit-msg.sample
.git/info/exclude
.git/refs/heads/
.git/refs/tags/
$ cat .git/HEAD
ref: refs/heads/main
| 文件/目录 | 作用 | 关键细节 |
|---|---|---|
HEAD | 当前分支引用 | 内容如ref: refs/heads/main,指向当前分支的最新提交。分离HEAD时直接指向commit SHA |
config | 仓库级配置 | INI格式,本地配置覆盖全局。可设置remote URL、分支跟踪等 |
objects/ | 对象数据库 | Git核心!所有内容以SHA-1哈希前2位/后38位命名存储。包含blob、tree、commit、tag四类对象 |
refs/ | 引用指针 | heads/存分支、tags/存标签、remotes/存远程引用。本质是40字符SHA-1文件的目录 |
index | 暂存区文件 | 二进制格式,记录文件路径和blob SHA映射。首次git add后出现 |
hooks/ | 钩子脚本 | pre-commit、commit-msg等事件触发脚本。去掉.sample后缀即启用 |
$ ssh-keygen -t ed25519 -C "zhangsan@example.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/user/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_ed25519
Your public key has been saved in /home/user/.ssh/id_ed25519.pub
$ eval "$(ssh-agent -s)"
Agent pid 12345
$ ssh-add ~/.ssh/id_ed25519
Identity added: /home/user/.ssh/id_ed25519
$ ssh -T git@github.com
Hi zhangsan! You've successfully authenticated, but GitHub does not provide shell access.
公钥内容复制到 GitHub → Settings → SSH Keys
$ cat ~/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... zhangsan@example.com
当你同时有个人GitHub和公司GitLab账号时,需要配置~/.ssh/config文件:
$ cat ~/.ssh/config
# 个人GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
# 公司GitHub
Host github-company
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_company
# 公司GitLab
Host gitlab.company.com
HostName gitlab.company.com
User git
IdentityFile ~/.ssh/id_rsa_gitlab
使用时:git clone git@github-company:org/repo.git
永久保存密码(明文存储,方便但不安全)
$ git config --global credential.helper store
内存缓存15分钟(默认,更安全)
$ git config --global credential.helper cache
自定义缓存时间
$ git config --global credential.helper "cache --timeout=3600"
macOS Keychain(推荐)
$ git config --global credential.helper osxkeychain
Windows Credential Manager(推荐)
$ git config --global credential.helper manager
core.autocrlf,否则出现满屏diff——推荐项目根目录添加.gitattributes文件统一处理ssh-add或忘把公钥加到GitHub的Settings → SSH Keys~/.ssh/config区分,仓库级user.email覆盖credential.helper store或cachecore.quotepath false解决中文文件名显示为\XXX的问题Git是版本控制工具(命令行程序),GitHub是基于Git的代码托管平台(网站)。就像Linux和Ubuntu的关系。除GitHub外还有GitLab、Bitbucket、Gitee等。
SSH更安全且免密码,推荐日常开发。HTTPS配置更简单,适合临时克隆。同一仓库可以同时有HTTPS和SSH两个remote。
不带--global写入.git/config,带--global写入~/.gitconfig。用git config --list --show-origin查看每个配置的来源文件。
--show-origin验证git init创建测试仓库,查看.git目录结构~/.ssh/config支持多GitHub账号user.name和user.email