首页·终端命令

git init

git

用法

git init [-b <branch>] [<directory>]

把一个文件夹变成新的 git 仓库;只用 init 不会跟踪任何文件,还要再 add 和 commit。

常用选项

选项含义
-b <name>name the first branch, e.g. main
--barerepository with no working tree, for a server
--template=<dir>copy hooks and files from a template directory
--separate-git-dir=<dir>keep the .git directory somewhere else
-qprint nothing but errors

示例

git init

turns the current folder into a repository by creating .git

git init -b main my-app

creates my-app/ with its first branch named main

git init --separate-git-dir=~/gitdirs/app.git

keeps the .git directory elsewhere and leaves a pointer file

难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. git init 是做什么的?

把一个文件夹变成新的 git 仓库;只用 init 不会跟踪任何文件,还要再 add 和 commit。

Q. 怎么写?

git init [-b <branch>] [<directory>] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 5 个,完整列表在 man git。这条命令属于git。

相关命令

man 手册: man git-init