How to initialize a Git repository
· Category: Git
Short answer
Use git init inside your project directory to create a new Git repository.
Steps
- Open your terminal and navigate to the project folder.
- Run the command:
git init
- This creates a hidden
.gitfolder that tracks all changes. - Add files to staging and make your first commit:
git add .
git commit -m "Initial commit"
Tips
- You can also initialize a bare repository with
git init --barefor shared remote repos. - Use
git statusfrequently to check the repository state. - Consider adding a
.gitignorefile before your first commit to exclude unnecessary files.
Common issues
- If you run
git initin the wrong directory, simply delete the.gitfolder and reinitialize in the correct location. - Ensure Git is installed and available in your system PATH before running the command.