git init — Creating a Repository
Every Git journey begins with git init. This command creates a new Git repository in the current directory.
What happens?
When you run git init, Git creates a hidden .git directory that stores all the tracking information. Your files aren't affected — Git just starts watching.
Try it!
In the terminal on the right, initialize a new repository and create your first commit:
- Run
git initto create a repository - Create a file:
echo "Hello Git" > hello.txt - Stage the file:
git add hello.txt - Commit it:
git commit -m "Initial commit"
Common Mistakes
Initializing in the wrong directory — Always make sure you're in the right folder before running git init. Running it in your home directory can cause confusion.
Forgetting to add files before committing — git init only creates the repository. You still need to git add files before you can git commit.