git init
Description:
Initialize a new Git repository in the current directory. This creates a .git subdirectory that contains all the necessary repository metadata.
Syntax:
bash
git init
git init [directory]
git init --bareExamples:
bash
# Initialize Git in current directory
git init
# Create a new directory and initialize Git
git init my-project
# Create a bare repository (for servers)
git init --bare repo.gitDetailed Explanation:
- Creates
.gitfolder: This hidden folder stores all Git metadata, commit history, and configuration - Not tracked yet: Files in the directory are not automatically tracked after
git init - Local only: This command only affects your local machine; nothing is pushed to remote servers
- Safe to run: Running
git initin an existing repository is safe and won't overwrite existing history
Common Errors:
Reinitialized existing Git repository→ Solution: This is just a warning, not an error. Your existing repository is fine.fatal: not a git repository(after deleting.gitfolder) → Solution: Rungit initagain to recreate the repository structure.
Pro Tips:
- Use
git init --initial-branch=mainto set the default branch name to "main" instead of "master" - The
.gitfolder is hidden by default. Usels -la(Unix) ordir /a(Windows) to see it - Never manually edit files inside
.git/unless you know exactly what you're doing - Warning: Deleting the
.gitfolder removes all Git history permanently
Related Commands:
git clone– Copy an existing repositorygit add– Start tracking filesgit commit– Save changes to history