git init is the command used to create a brand new, empty Git repository. It transforms an ordinary, plain folder on your computer into a Git-tracked folder, allowing you to start recording changes.
Before you run git init, Git has no idea your folder exists. It doesn't track any of the files inside it. When you run git init, Git creates a hidden directory called .git inside your folder. This .git folder is the brain of your repository; it contains all the databases, configuration files, and history that Git needs to function.
You only ever need to run git init once per project.
git init| Part | Meaning |
|---|---|
git |
Calls the Git program |
init |
Short for "initialize" (start up / create) |
Let's say you want to start a new web project on your Desktop.
- Open your terminal and create a new folder:
mkdir my-website
- Navigate into that folder:
cd my-website - Initialize the Git repository:
Git will output:
git init
Initialized empty Git repository in /path/to/my-website/.git/
When you run git init, it creates a hidden folder named .git. If you are on Mac/Linux, you can see it by running ls -a. If you are on Windows, you have to enable "Show Hidden Files" in Explorer.
WARNING: NEVER delete or manually edit the contents of the .git folder unless you know exactly what you are doing. If you delete the .git folder, your entire project's version history is permanently destroyed, and it goes back to being just a normal folder.
- Use
git initwhen you are starting a brand new project from scratch on your own computer. - Use
git clonewhen the project already exists on GitHub and you just want to download it. (Runninggit cloneautomatically runsgit initin the background for you).
- Running
git initin your home directory: Beginners sometimes open their terminal (which opens in~orC:\Users\Name) and blindly rungit init. This turns their ENTIRE computer's user directory into a Git repository, which is a massive mess. Alwayscdinto your specific project folder first. - Running
git initinside another Git repository: This creates a Git repo inside a Git repo (a submodule), which causes endless headaches. Only initialize the root folder of your project.
git initcreates a new local repository.- It creates a hidden
.gitfolder containing all version history. - Run it exactly once per new project.
- Always make sure you are inside your project folder before running it.