How to initialize a Git repository

· Category: Git

Short answer

Use git init inside your project directory to create a new Git repository.

Steps

  1. Open your terminal and navigate to the project folder.
  2. Run the command:
git init
  1. This creates a hidden .git folder that tracks all changes.
  2. 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 --bare for shared remote repos.
  • Use git status frequently to check the repository state.
  • Consider adding a .gitignore file before your first commit to exclude unnecessary files.

Common issues

  • If you run git init in the wrong directory, simply delete the .git folder and reinitialize in the correct location.
  • Ensure Git is installed and available in your system PATH before running the command.