What is git init?

Why Interviewers Ask This

Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Git & GitHub development. It reveals whether you understand the building blocks that more complex concepts rely on.

Answer

git init initializes a new Git repository in the current directory by creating a .git/ subdirectory. This is the first command you run when starting version control on a new project. The .git/ folder contains all Git metadata: object database, refs, configuration, and hooks. The directory itself is not committed — it is Git's private workspace. After git init, the directory becomes a Git repository, but it has no commits and no tracking history yet. Create initial commit: git add . then git commit -m "Initial commit". Create a bare repository (for servers): git init --bare repo.git — contains only Git data, no working files; this is what GitHub/GitLab host internally. Initialize with a custom branch name: git init -b main (sets default branch to "main" instead of legacy "master"). If you accidentally run git init in the wrong directory, simply delete the .git/ folder: rm -rf .git.

Pro Tip

This topic has Git & GitHub-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.