Appearance
仓库初始化
git init 创建新仓库
git init 命令将当前目录(或指定目录)初始化为一个新的 Git 仓库。
bash
# 在当前目录初始化仓库
cd my-project
git init
# 输出:
# Initialized empty Git repository in /path/to/my-project/.git/
# 指定目录初始化
git init my-new-project
cd my-new-project初始化后的操作
bash
# 查看仓库状态
git status
# On branch main
# No commits yet
# nothing to commit (create/copy files and use "git add" to track)
# 创建文件并进行第一次提交
echo "# My Project" > README.md
git add README.md
git commit -m "Initial commit"指定初始分支名
bash
# 初始化时指定主分支名(Git 2.28+)
git init -b main
git init --initial-branch=maingit clone 克隆远程仓库
git clone 从远程仓库复制完整的仓库到本地,包括所有历史记录。
基本用法
bash
# HTTPS 克隆
git clone https://github.com/user/repo.git
# SSH 克隆
git clone git@github.com:user/repo.git
# 克隆到指定目录名
git clone https://github.com/user/repo.git my-local-name常用克隆选项
bash
# 浅克隆:只获取最近 N 次提交的历史(减小下载体积)
git clone --depth 1 https://github.com/user/repo.git
# 克隆指定分支
git clone -b develop https://github.com/user/repo.git
# 单分支克隆(配合 --branch 减小体积)
git clone --single-branch -b main https://github.com/user/repo.git
# 克隆并初始化子模块
git clone --recurse-submodules https://github.com/user/repo.git
# 部分克隆(Git 2.19+,不下载大文件对象)
git clone --filter=blob:none https://github.com/user/repo.git克隆完成后的状态
bash
cd repo
git remote -v
# origin https://github.com/user/repo.git (fetch)
# origin https://github.com/user/repo.git (push)
git branch -a
# * main
# remotes/origin/HEAD -> origin/main
# remotes/origin/main
# remotes/origin/develop裸仓库(bare repository)
裸仓库是没有工作区的仓库,通常用作共享的中央仓库(服务器端)。
bash
# 创建裸仓库
git init --bare repo.git
# 裸仓库目录结构(直接就是 .git 的内容)
# repo.git/
# ├── HEAD
# ├── config
# ├── objects/
# └── refs/裸仓库的特点:
- 没有工作区,无法直接编辑文件
- 只用于接收和存储 push 的内容
- 命名惯例:以
.git结尾
将普通仓库转为裸仓库:
bash
git clone --bare my-project my-project.git裸仓库的典型用途:
- 自建 Git 服务器
- 作为团队共享的中央仓库
.git 目录结构详解
执行 git init 后,会在当前目录创建 .git/ 目录,这是 Git 仓库的核心。
.git/
├── HEAD # 当前分支指针
├── config # 仓库级配置
├── description # 仓库描述(GitWeb 用)
├── info/
│ └── exclude # 本地忽略规则(不提交)
├── hooks/ # 钩子脚本目录
│ ├── pre-commit.sample
│ ├── commit-msg.sample
│ └── ...
├── objects/ # 对象数据库
│ ├── info/
│ └── pack/
├── refs/ # 引用目录
│ ├── heads/ # 本地分支
│ │ └── main
│ ├── remotes/ # 远程追踪分支
│ │ └── origin/
│ └── tags/ # 标签
├── logs/ # 引用变更日志(reflog)
│ ├── HEAD
│ └── refs/
│ └── heads/
│ └── main
├── index # 暂存区(二进制格式)
├── COMMIT_EDITMSG # 最近一次提交信息(临时)
├── FETCH_HEAD # 最近一次 fetch 的结果
├── ORIG_HEAD # 危险操作前 HEAD 的位置
└── packed-refs # 打包的引用(优化后)关键文件说明
HEAD 文件
bash
cat .git/HEAD
# ref: refs/heads/main
# (分离 HEAD 时显示具体的 SHA-1)config 文件
bash
cat .git/config
# [core]
# repositoryformatversion = 0
# filemode = true
# bare = false
# logallrefupdates = true
# [remote "origin"]
# url = https://github.com/user/repo.git
# fetch = +refs/heads/*:refs/remotes/origin/*
# [branch "main"]
# remote = origin
# merge = refs/heads/mainindex 文件
暂存区以二进制格式存储,可以用命令查看:
bash
git ls-files --stage
# 100644 sha1... 0 README.md
# 100644 sha1... 0 src/main.jsobjects/ 目录
所有 Git 对象(blob、tree、commit、tag)都存储在这里:
bash
# 列出所有对象
find .git/objects -type f
# 查看对象类型
git cat-file -t <sha1>
# 查看对象内容
git cat-file -p <sha1>从已有项目开始
场景1:将已有项目纳入 Git 管理
bash
cd existing-project
# 初始化 Git 仓库
git init -b main
# 创建 .gitignore
echo "node_modules/" > .gitignore
echo ".env" >> .gitignore
# 添加所有文件
git add .
# 初次提交
git commit -m "Initial commit: add existing project files"
# 关联远程仓库并推送
git remote add origin git@github.com:user/repo.git
git push -u origin main场景2:从远程仓库开始新项目
bash
# 在 GitHub 上创建空仓库后
git clone git@github.com:user/new-repo.git
cd new-repo
# 开始开发
echo "# New Project" > README.md
git add README.md
git commit -m "Initial commit"
git push总结
| 命令 | 用途 |
|---|---|
git init | 创建新的本地仓库 |
git init --bare | 创建裸仓库(服务器用) |
git clone <url> | 克隆远程仓库 |
git clone --depth 1 | 浅克隆(只下载最新快照) |
git clone -b <branch> | 克隆指定分支 |
初始化仓库是 Git 工作流的起点。接下来将学习如何追踪文件状态和进行提交。