全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-11 5 分钟 ✍️ juanwangdev

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"

初始化后仓库为空,需添加文件并提交才能开始版本控制。

要点总结

  1. git init 在当前目录创建仓库
  2. git init <name> 创建目录并初始化
  3. 生成 .git 隐藏目录存储仓库数据
  4. --bare 创建裸仓库,用于服务器
  5. --initial-branch 指定初始分支名

📝 发现内容有误?点击此处直接编辑

← 上一篇 git diff 查看差异
下一篇 → git log 查看提交历史
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库