Learn how to create and initialize Git repositories, and understand the initial repository structure
Learn how to create and initialize Git repositories, understand basic setup and configuration.
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.
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
Try creating a repository yourself with this interactive demo:
# Create a new directory and initialize it
mkdir my-project
cd my-project
git init
# Navigate to existing project
cd existing-project
git init
git add .
git commit -m "Initial commit"
- #### 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.
Avoid initializing a Git repository inside another Git repository unless you have a specific need for submodules.
Make sure you're in the root directory of your project before running git init.
Running git init on an existing repository is safe but unnecessary.
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