git init
创建新 Git 仓库使用 git init and 什么 happens 后面 scenes.
你将学到
- 如何 initialize 新 Git 仓库
- 什么 git init 创建 in .git folder
- 如何 reinitialize or 检查 existing 仓库
- 默认分支 name and 如何 更改它
概念讲解
git init 的作用
git init turns an ordinary directory into a Git repository. It creates the hidden .git subdirectory and populates it with the structures Git needs to start tracking changes.
mkdir my-project
cd my-project
git init
After running this, the directory is a Git repository — but it is empty. No files are being tracked yet. The next steps are usually to create a file, git 添加 it, and git 提交.
Behind the Scenes
When you run git init, Git creates:
对象/— where file contents and commits are stored as compressed objectsrefs/heads/— where branch pointers live (initially empty)refs/标签/— where tag pointers liveHEAD— a file that points to the current branch (initiallyrefs/heads/主要orrefs/heads/master)配置— repository-specific configuration描述— a placeholder file (rarely used)
Nothing is committed yet — the repository has zero commits and no branches until you make your first commit.
The Default Branch Name
Historically, Git's default branch was called master. Since 2020, the community has moved toward using 主要 as the default, and Git itself changed its default in version 2.28.
You can set the default branch name globally:
git config --global init.defaultBranch main
If your repository was created with master and you want to rename it:
git branch -m main
Reinitializing
Running git init in an existing repository is safe — it does not overwrite anything, just makes sure the .git structure is complete. You might do this after manually copying a .git folder or if something got corrupted.
Cloning Instead of Initializing
If you want to work on an existing repository (for example, an open-source project on GitHub), use git 克隆 instead of git init:
git clone https://github.com/user/repo.git
This downloads the entire repository, including all history, and sets up the remote connection automatically.
示例代码
# Start a brand new project
mkdir weather-app
cd weather-app
# Initialize Git — this creates the .git folder
git init
# Output: "Initialized empty Git repository in .../weather-app/.git/"
# Set the default branch name (do this once globally for all new repos)
git config --global init.defaultBranch main
# Create an initial file
echo "# Weather App" > README.md
# Stage and commit the first file
git add README.md
git commit -m "Initial commit: add README"
# See the result — you now have a repository with one commit
git log --oneline
git status
# If you initialized with the wrong branch name, rename it
git branch -m main
完整首先-运行 workflow: 创建 directory, initialize Git, 设置默认分支, 制作首先提交, and 验证 result. 这个是 如何每个新 Git 项目开始.
动手试一试
- .gitignore Generator
Generate .gitignore 到 accompany 你的新 仓库.
- 环境 File Generator
创建 .环境 files — 哪个你 应该 gitignore, 从不提交.
常见错误
错误写法
运行 git init 内部 already-initialized 仓库
正确做法
git init 是 safe 到运行再次, 但它 是通常 sign 你是 in 错误 directory. 检查 for existing .git folder 首先使用 ls -. 如果你 see .git, 仓库是 already initialized.
错误写法
Initializing Git in 你的 home directory or 根 folder
正确做法
总是 CD 到你的特定项目 directory 之前运行 git init. Initializing in 你的 home directory 会 track 每个 file on 你的 computer, 哪个是 catastrophic.
相关资源
相关速查表
- Git Cheatsheet
git init and 仓库设置 commands.
练习 git init
5 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。