Learning Objectives
- Learn how to initialize a new Git repository using git init
- Understand the structure of a newly created repository
- Explore common initialization options and best practices
- Practice creating and verifying repository setup
Initializing a Git Repository
Creating a Git repository is your first step in version control. There are two main ways to start with Git: initializing a new repository or cloning an existing one. In this lesson, we'll focus on creating a new repository from scratch.
The git init Command
git init [project-name]
This command creates a new Git repository by:
- Creating a .git directory with all necessary repository files
- Initializing a default branch (usually main)
- Setting up the basic repository structure
Interactive Repository Creation
Try creating a repository yourself with this interactive demo:
Common Initialization Scenarios
New Project
# Create a new directory and initialize itmkdir my-projectcd my-projectgit init
Existing Project
# Navigate to existing projectcd existing-projectgit initgit add .git commit -m "Initial commit"
Best Practices
Initialize Early
Start version control at the beginning of your project, not after significant development.
Use .gitignore
Set up your .gitignore file before your first commit to exclude unnecessary files.
Verify Installation
Always check the repository was created correctly with git status.
Common Issues and Solutions
Nested Git Repositories
Avoid initializing a Git repository inside another Git repository unless you have a specific need for submodules.
Wrong Directory Level
Make sure you're in the root directory of your project before running git init.
Reinitializing Existing Repository
Running git init on an existing repository is safe but unnecessary.
What's Next?
Now that you know how to create a repository, in the next lesson you'll learn:
- How to add files to your repository
- Making your first commit
- Checking repository status