Home·Terminal commands

git init

git

Usage

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

Turns a folder into a new git repository; init alone tracks nothing, so add and commit still have to happen.

Common flags

FlagMeaning
-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

Examples

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

The confusion is that undo has several shapes — moving where a branch points is not the same as adding a commit that reverses it.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does git init do?

Turns a folder into a new git repository; init alone tracks nothing, so add and commit still have to happen.

Q. How do I type it?

git init [-b <branch>] [<directory>] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

5 are listed here; the full set is in man git. This command sits under git.

Related commands

man page: man git-init