Git init Command
In this page:
What is git init?
git init is the single command that turns any ordinary folder into an active Git repository -- it creates the hidden .git directory containing the object database, refs, and configuration Git needs to start tracking changes.
Example: What is git init?
git init
Initializing an Empty Repository
You can also run git init <folder-name> to create a brand-new directory and initialize it as a repository in one step, which is handy when starting a project from scratch rather than converting an existing folder.
Example: Initializing an Empty Repository
git init my-new-project
Custom Default Branch
New repositories need a starting branch name, and modern Git lets you choose it explicitly with git init -b main (or set a global default via init.defaultBranch) instead of accepting whatever the installed default happens to be.
Example: Custom Default Branch
git init -b main
Re-initializing a Repository
Running git init again inside a folder that's already a repository is completely safe -- Git will not touch your existing commits or files, it only re-creates any missing configuration files, which is occasionally useful for repairing a corrupted setup.
Example: Re-initializing a Repository
git init
Initializing a Bare Repository
A bare repository, created with git init --bare, has no working directory of checked-out files at all -- it holds only the raw tracking data, which is exactly what you want on a server acting purely as a central hub that others push to and pull from.
Example: Initializing a Bare Repository
git init --bare
Chapter Quiz — Complete all 18 topics to unlock
0/18 topics done
Complete these topics first:
- Git init Command
- Git New Files
- Git Staging Environment
- Git clone Command
- Git status Command
- Git help Command
- Git add Command
- Git commit Command
- Git tags Command
- Git stash Command
- Git log Command
- Git diff Command
- Git show Command
- Git rm Command
- Git mv Command
- Git restore Command
- Git clean Command
- Git ignore (.gitignore)