Git Basics & Workflow
Git is a distributed version control system built around three states: working directory, staging area and repository. Master init, add, commit, log and status to understand every other command.
What is this?
Git tracks changes to files as a series of snapshots. Every commit records a full snapshot plus metadata (author, message, timestamp) and gets a unique SHA-1 hash. Git is distributed: every clone contains the entire history, so it works fully offline.
Why do you need it?
- Every commit is a complete, hash-addressed snapshot, so any state in history can be reproduced exactly.
- It is distributed — every developer has a full copy of history and can commit without a network connection.
- Changes are atomic and reviewable: each commit is a self-contained unit of work with an author and reason.
- git blame and git log turn history into an audit trail for who changed what and when.
Mental model
A lab notebook
Git history
Every dated entry records what changed and why, and you can flip back to any page. Git pages are signed with a hash so nobody can quietly rewrite the record.
How it works
- git init creates a hidden .git directory that stores all history and configuration for the repository.
- Files live in the working directory; git add moves changes from there into the staging area (the index).
- git commit records the staged snapshot, generates a SHA-1 hash, and stores it in the repository.
- git status shows which files are modified, staged or untracked; git log shows the committed history.
- git diff compares the working tree, index and commits so you can review changes before committing.
Git's three-tree model: working directory, staging area, repository.
Key commands
git initCreate a new empty repository in the current directory.
git statusShow which files are modified, staged or untracked.
On branch main\nChanges to be committed: ...\nUntracked files: ...
git add <file>Stage a file or directory so it is included in the next commit.
git commit -m 'message'Record the staged snapshot with a message.
git log --onelineShow commit history, one line per commit.
a1b2c3d (HEAD -> main) Add project README
git diffShow unstaged changes between the working directory and the index.
Examples
Your first commit
git initecho "# My Project" > README.mdgit add README.mdgit commit -m "Add project README"git log --onelinegit init creates the repository, README.md is created and staged with git add, then git commit records the snapshot. git log --oneline shows one line per commit so you can see the hash and message.
In production
- CI/CD pipelines run a build and tests on every commit pushed to the repository.
- Pull requests group commits into reviewable units, and git blame traces regressions to the commit that introduced them.
- Release tags and commit SHAs make every deployed artifact traceable back to source.
- Semantic commit conventions feed automated changelog and version-bumping tools.
Best practices
- Commit small, focused units of work with a clear message; one commit = one logical change.
- Review your staged diff with git diff --staged before committing.
- Use a .gitignore to keep build artifacts, local configs and secrets out of the repository.
- Configure user.name and user.email before your first commit.
- Prefer Conventional Commits (fix:, feat:, chore:) so tooling and release notes can parse history.
Common mistakes
- Running git add . and committing secrets, logs or build artifacts by accident.
- Making huge 'WIP' commits that are impossible to review or bisect.
- Forgetting to configure user.email — git refuses to commit without it.
- Tracking generated files, which produces noisy diffs and merge conflicts.
Common issues
Please tell me who you are. fatal: unable to auto-detect email addressuser.name or user.email is not configured.
Run git config --global user.name 'Your Name' and git config --global user.email 'you@example.com'.
fatal: not a git repository (or any of the parent directories)You ran a git command outside a repository.
Run git init to create a repository, or cd into the correct repo directory.
nothing to commit, working tree cleanNothing is staged or modified.
Make or edit a file, stage it with git add, then commit.
Debugging tips
- git status is always the first step — it tells you exactly where your changes are.
- git diff shows unstaged changes; git diff --staged shows what the next commit will contain.
- git log -p shows each commit's patch so you can see how files evolved.
- git status --short gives a compact one-character-per-file view for scripting.
Interview checkpoint
Hands-on lab
- Configure Git for the first time
- Initialize a repository
- Stage and commit changes
- Inspect history with git log and git diff
Your First Repository
20 min · beginner
Quick quiz
Which command moves changes from the working directory into the staging area?
Git requires a network connection to make a commit.
Which command shows the commit history in compact one-line form?
Cheat sheet
git initCreate a repositorygit statusCheck working tree stategit add <file>Stage a changegit commit -m 'msg'Commit staged changesgit log --onelineCompact historygit diffShow unstaged changes