FresherTechnicalGit
What does git init do and what is inside the .git folder?
Try answering first
Model answer
git init turns a folder into a Git repository by creating a hidden .git directory. That folder holds the entire history and configuration; the files you edit are only the working tree on top of it. Delete .git and the project stops being a repository.
Main contents:
HEAD: a file that says which branch or commit is checked out.objects/: the database of blobs, trees, commits and tags.refs/: branches, tags and remote-tracking branches, each a file holding a hash.index: the staging area.config: repository-level settings such as remotes.hooks/: sample scripts that run on events like commit.info/exclude: personal ignore rules that are not shared.
git init -b main
ls .git
cat .git/HEAD # ref: refs/heads/main
-b main sets the initial branch name. Cloning a repo also creates this folder and copies the full history into it.
Never edit files in .git by hand unless you know exactly why; use Git commands.
Key points
- git init creates a hidden .git directory
- .git holds all history, refs and config
- HEAD, objects, refs, index and hooks are the main parts
- Deleting .git removes the repository
Likely follow-ups
- What does git clone do beyond git init?
- What is a bare repository and why does a server use one?
Common mistakes
- Thinking the history lives in the working files
- Running git init inside an existing repository by accident