git init 初始化仓库
git init 命令用于在当前目录创建一个新的 Git 仓库。
基本用法
Bash
# 在当前目录初始化仓库
git init
# 创建新目录并初始化
git init <project-name>
执行效果
Bash
$ git init my-project
Initialized empty Git repository in /path/my-project/.git/
$ ls -la my-project
total 0
drwxr-xr-x 1 user staff 0 May 11 10:00 .
drwxr-xr-x 1 user staff 0 May 11 10:00 ..
drwxr-xr-x 9 user staff 0 May 11 10:00 .git
.git 目录结构
| 目录/文件 | 说明 |
|---|---|
| HEAD | 当前分支引用 |
| config | 仓库配置文件 |
| objects | 存储所有数据对象 |
| refs | 存储分支和标签引用 |
| hooks | 钩子脚本目录 |
常用选项
Bash
# 指定初始分支名
git init --initial-branch=main
# 裸仓库(无工作区,用于服务器)
git init --bare
# 指定模板目录
git init --template=<template-directory>
初始化后配置
Bash
# 设置当前仓库用户信息
git config user.name "Your Name"
git config user.email "your@email.com"
# 添加文件并首次提交
git add .
git commit -m "Initial commit"
初始化后仓库为空,需添加文件并提交才能开始版本控制。
要点总结
git init在当前目录创建仓库git init <name>创建目录并初始化- 生成
.git隐藏目录存储仓库数据 --bare创建裸仓库,用于服务器--initial-branch指定初始分支名
📝 发现内容有误?点击此处直接编辑