Appearance
初始化仓库
概述
Git 仓库(Repository)是版本控制的基本单位,它包含了项目的所有文件及其完整的历史记录。在使用 Git 管理项目之前,首先需要创建或获取一个 Git 仓库。
创建 Git 仓库有两种主要方式:
- 初始化新仓库:在本地创建一个全新的 Git 仓库
- 克隆现有仓库:从远程服务器复制一个已存在的仓库
本章将详细介绍这两种方式的操作方法,以及仓库的内部结构。
git init 创建仓库
基本用法
bash
# 在当前目录初始化仓库
git init
# 输出示例
Initialized empty Git repository in /path/to/project/.git/在指定目录初始化
bash
# 创建目录并初始化仓库
git init my-project
# 输出示例
Initialized empty Git repository in /path/to/my-project/.git/
# 进入项目目录
cd my-project初始化选项
bash
# 初始化裸仓库(没有工作目录,用于服务器)
git init --bare
# 初始化并指定初始分支名
git init --initial-branch=main
# 或
git init -b main
# 指定模板目录
git init --template=/path/to/template
# 指定对象存储格式
git init --object-format=sha256
# 分离工作目录和 Git 目录
git init --separate-git-dir=/path/to/git-dir初始化流程详解
bash
# 完整的项目初始化流程
# 1. 创建项目目录
mkdir my-project
cd my-project
# 2. 初始化 Git 仓库
git init
# Initialized empty Git repository in /path/to/my-project/.git/
# 3. 创建 .gitignore 文件
cat > .gitignore << 'EOF'
# 操作系统文件
.DS_Store
Thumbs.db
# IDE 配置
.idea/
.vscode/
*.swp
# 依赖目录
/vendor/
/node_modules/
# 环境配置
.env
.env.local
# 编译输出
/build/
/dist/
# 日志文件
*.log
EOF
# 4. 创建 README 文件
echo "# My Project" > README.md
# 5. 添加文件到暂存区
git add .
# 6. 查看状态
git status
# 7. 进行首次提交
git commit -m "feat: 初始化项目
- 添加 .gitignore 配置
- 添加 README 文件"
# 8. 查看提交历史
git log --oneline初始化后的目录结构
bash
# 查看项目结构
tree -a
# 输出
.
├── .git/
│ ├── HEAD
│ ├── config
│ ├── description
│ ├── hooks/
│ ├── info/
│ │ └── exclude
│ ├── objects/
│ │ ├── info/
│ │ └── pack/
│ └── refs/
│ ├── heads/
│ └── tags/
├── .gitignore
└── README.mdgit clone 克隆仓库
基本用法
bash
# 克隆远程仓库
git clone https://github.com/user/repo.git
# 输出示例
Cloning into 'repo'...
remote: Enumerating objects: 100, done.
remote: Counting objects: 100% (100/100), done.
remote: Compressing objects: 100% (80/80), done.
remote: Total 100 (delta 20), reused 100 (delta 20), pack-reused 0
Receiving objects: 100% (100/100), 10.00 KiB | 1.00 MiB/s, done.
Resolving deltas: 100% (20/20), done.克隆到指定目录
bash
# 克隆到指定目录名
git clone https://github.com/user/repo.git my-repo
# 克隆到指定路径
git clone https://github.com/user/repo.git /path/to/directory克隆选项
bash
# 浅克隆(只克隆最近的历史)
git clone --depth 1 https://github.com/user/repo.git
# 克隆单个分支
git clone --single-branch --branch main https://github.com/user/repo.git
# 克隆指定分支
git clone --branch develop https://github.com/user/repo.git
# 递归克隆子模块
git clone --recursive https://github.com/user/repo.git
# 克隆裸仓库
git clone --bare https://github.com/user/repo.git
# 克隆镜像仓库
git clone --mirror https://github.com/user/repo.git
# 指定克隆目录名
git clone https://github.com/user/repo.git my-project
# 使用 SSH 协议克隆
git clone git@github.com:user/repo.git
# 使用 Git 协议克隆
git clone git://github.com/user/repo.git不同协议的克隆
bash
# HTTPS(最常用)
git clone https://github.com/user/repo.git
# SSH(需要配置密钥)
git clone git@github.com:user/repo.git
# Git 协议(只读,速度快)
git clone git://github.com/user/repo.git
# 本地文件系统
git clone /path/to/local/repo.git
git clone file:///path/to/local/repo.git
# 使用用户名密码(HTTPS)
git clone https://username:password@github.com/user/repo.git
# 使用 token(GitHub)
git clone https://token@github.com/user/repo.git克隆大型仓库
bash
# 方法 1:浅克隆
git clone --depth 1 https://github.com/large/repo.git
# 方法 2:单分支克隆
git clone --single-branch --branch main https://github.com/large/repo.git
# 方法 3:稀疏检出
git clone --filter=blob:none --sparse https://github.com/large/repo.git
cd repo
git sparse-checkout init --cone
git sparse-checkout set src/app
# 方法 4:部分克隆
git clone --filter=blob:none https://github.com/large/repo.git
# 后续获取完整历史
git fetch --unshallow克隆后的配置
bash
# 克隆后配置用户信息
cd repo
git config user.name "Your Name"
git config user.email "your.email@example.com"
# 查看远程仓库信息
git remote -v
# 查看分支信息
git branch -a
# 查看提交历史
git log --oneline -10仓库结构说明
工作目录结构
bash
project/
├── .git/ # Git 仓库目录
│ ├── HEAD # 当前分支引用
│ ├── config # 仓库配置
│ ├── description # 仓库描述
│ ├── hooks/ # 钩子脚本
│ │ ├── pre-commit
│ │ ├── post-commit
│ │ └── ...
│ ├── info/ # 额外信息
│ │ └── exclude # 本地忽略规则
│ ├── objects/ # 对象数据库
│ │ ├── info/
│ │ └── pack/
│ └── refs/ # 引用
│ ├── heads/ # 本地分支
│ ├── remotes/ # 远程分支
│ └── tags/ # 标签
├── .gitignore # 忽略文件配置
├── .gitattributes # 文件属性配置
├── .gitmodules # 子模块配置
└── src/ # 项目源代码工作目录、暂存区和仓库
┌─────────────────────────────────────────────────────────────┐
│ Git 三个工作区域 │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────┐ │
│ │ 工作目录 │ 你编辑文件的地方 │
│ │ (Working) │ 包含项目的实际文件 │
│ └──────┬───────┘ │
│ │ │
│ │ git add │
│ ▼ │
│ ┌──────────────┐ │
│ │ 暂存区 │ 下次提交的文件快照 │
│ │ (Staging) │ .git/index 文件 │
│ └──────┬───────┘ │
│ │ │
│ │ git commit │
│ ▼ │
│ ┌──────────────┐ │
│ │ 仓库 │ 所有提交的历史记录 │
│ │ (Repository) │ .git/objects 目录 │
│ └──────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘.git 目录解析
HEAD 文件
bash
# 查看 HEAD 内容
cat .git/HEAD
# 输出(指向分支)
ref: refs/heads/main
# 输出(分离 HEAD 状态)
abc1234567890abcdef1234567890abcdef12345678
# HEAD 的作用
# 1. 指示当前所在的分支
# 2. 作为下一次提交的父提交
# 3. 影响工作目录的状态config 文件
bash
# 查看 config 内容
cat .git/config
# 输出示例
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/user/repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
[user]
name = Your Name
email = your.email@example.comobjects 目录
bash
# 查看 objects 目录结构
tree .git/objects
# 输出
.git/objects/
├── info/
│ └── packs
└── pack/
├── pack-abc123.idx
└── pack-abc123.pack
# 对象存储路径格式
.git/objects/<前两位哈希>/<剩余38位哈希>
# 示例
.git/objects/8d/01495a85a9f6e2f8e9bc4b5a5c5a5a5a5a5a5arefs 目录
bash
# 查看 refs 目录结构
tree .git/refs
# 输出
.git/refs/
├── heads/
│ ├── main
│ └── develop
├── remotes/
│ └── origin/
│ ├── main
│ └── develop
└── tags/
├── v1.0.0
└── v1.1.0
# 查看分支引用内容
cat .git/refs/heads/main
# 输出:40 位 SHA-1 哈希值
abc1234567890abcdef1234567890abcdef12345678hooks 目录
bash
# 查看 hooks 目录
ls -la .git/hooks/
# 输出
total 48
drwxr-xr-x 12 user staff 384 Jan 15 10:00 .
drwxr-xr-x 15 user staff 480 Jan 15 10:00 ..
-rw-r--r-- 1 user staff 478 Jan 15 10:00 pre-commit.sample
-rw-r--r-- 1 user staff 1234 Jan 15 10:00 commit-msg.sample
...
# 启用钩子(去掉 .sample 后缀)
mv .git/hooks/pre-commit.sample .git/hooks/pre-commit
chmod +x .git/hooks/pre-commitindex 文件(暂存区)
bash
# 查看暂存区内容
git ls-files --stage
# 输出示例
100644 8d01495a85a9f6e2f8e9bc4b5a5c5a5a5a5a5a5a 0 README.md
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 .gitignore
# 查看暂存区详细信息
git ls-files --debug
# index 文件存储了:
# 1. 文件路径
# 2. 文件模式(权限)
# 3. SHA-1 哈希
# 4. 时间戳logs 目录
bash
# 查看 logs 目录
tree .git/logs
# 输出
.git/logs/
├── HEAD
└── refs/
└── heads/
├── main
└── develop
# 查看 HEAD 日志
cat .git/logs/HEAD
# 输出(reflog)
0000000000000000000000000000000000000000 abc1234567890abcdef... Zhang San <zhang@example.com> 1705287600 +0800 commit (initial): Initial commit常见问题
问题 1:如何重新初始化仓库?
bash
# 删除 .git 目录
rm -rf .git
# 重新初始化
git init
# 注意:这会丢失所有历史记录!问题 2:如何克隆需要认证的私有仓库?
bash
# 方法 1:使用 SSH
git clone git@github.com:user/private-repo.git
# 方法 2:使用 HTTPS + Token
git clone https://token@github.com/user/private-repo.git
# 方法 3:使用凭据管理器
git config --global credential.helper manager
git clone https://github.com/user/private-repo.git问题 3:如何克隆子模块?
bash
# 方法 1:递归克隆
git clone --recursive https://github.com/user/repo.git
# 方法 2:克隆后初始化
git clone https://github.com/user/repo.git
cd repo
git submodule init
git submodule update
# 方法 3:更新子模块
git submodule update --init --recursive问题 4:如何将现有项目转换为 Git 仓库?
bash
# 进入项目目录
cd /path/to/existing-project
# 初始化仓库
git init
# 创建 .gitignore
cat > .gitignore << 'EOF'
# 添加忽略规则
EOF
# 添加所有文件
git add .
# 查看状态
git status
# 提交
git commit -m "feat: 初始化项目"
# 添加远程仓库
git remote add origin https://github.com/user/repo.git
# 推送到远程
git push -u origin main问题 5:如何关联本地仓库到远程?
bash
# 创建本地仓库
mkdir my-project
cd my-project
git init
echo "# My Project" > README.md
git add .
git commit -m "Initial commit"
# 在 GitHub/GitLab 创建空仓库后
# 添加远程仓库
git remote add origin https://github.com/user/repo.git
# 推送到远程
git push -u origin main
# 或强制推送(如果远程有不同历史)
git push -u origin main --force最佳实践
初始化项目清单
bash
# 1. 创建项目目录
mkdir my-project && cd my-project
# 2. 初始化 Git
git init -b main
# 3. 创建 .gitignore
curl -o .gitignore https://www.toptal.com/developers/gitignore/api/git,php,macos,windows
# 4. 创建 README
echo "# Project Name" > README.md
# 5. 创建目录结构
mkdir -p src tests docs
# 6. 添加文件
git add .
# 7. 首次提交
git commit -m "feat: 初始化项目"
# 8. 添加远程仓库
git remote add origin https://github.com/user/repo.git
# 9. 推送到远程
git push -u origin main克隆项目清单
bash
# 1. 克隆仓库
git clone https://github.com/user/repo.git
# 2. 进入目录
cd repo
# 3. 查看分支
git branch -a
# 4. 查看远程信息
git remote -v
# 5. 拉取最新代码
git pull
# 6. 创建开发分支
git checkout -b feature/my-feature
# 7. 开始开发总结
初始化仓库要点
| 操作 | 命令 | 说明 |
|---|---|---|
| 初始化新仓库 | git init | 在当前目录创建仓库 |
| 初始化指定目录 | git init <dir> | 创建目录并初始化 |
| 指定初始分支 | git init -b main | 设置初始分支名 |
克隆仓库要点
| 操作 | 命令 | 说明 |
|---|---|---|
| 基本克隆 | git clone <url> | 克隆远程仓库 |
| 指定目录 | git clone <url> <dir> | 克隆到指定目录 |
| 浅克隆 | git clone --depth 1 <url> | 只克隆最近历史 |
| 单分支 | git clone --single-branch <url> | 只克隆指定分支 |
下一步
学会创建和克隆仓库后,建议继续学习:
