Git is a distributed version control system that tracks changes to files, enabling developers to collaborate on code, maintain history, and manage branches independently. Created by Linus Torvalds in 2005 for Linux kernel development, Git is now the standard version control tool across the software industry.
How Git works
Git stores snapshots of your project, not diffs. Each commit is a complete snapshot of all tracked files, compressed and deduplicated using content-addressable storage. Every object (commits, trees, blobs) is identified by its SHA-1 hash.
The three areas of a Git repository:
- Working directory: Your actual files on disk
- Staging area (index): Changes marked for the next commit
- Repository (.git): The complete history of all commits
git add file.js # Working dir → staging area
git commit -m "msg" # Staging area → repository
Distributed by design
Every Git clone is a full repository with complete history. You can commit, branch, merge, and view logs entirely offline. This is fundamentally different from centralized systems like SVN, where the server holds the single source of truth.
Remote repositories (GitHub, GitLab, Bitbucket) serve as shared coordination points, but no single copy is authoritative by default.
Branching and merging
Branches in Git are lightweight — a branch is just a pointer to a commit. Creating, switching, and deleting branches is nearly instant.
git checkout -b feature/login # Create and switch to branch
# ... make changes, commit ...
git checkout main
git merge feature/login # Merge branch into main
Common branching strategies: trunk-based development (short-lived branches merged quickly), GitHub Flow (feature branches with pull requests), and GitFlow (develop/release/hotfix branches).
Essential commands
git status # See what's changed
git log --oneline # Compact commit history
git diff # See uncommitted changes
git stash # Temporarily shelve changes
git rebase main # Reapply commits on top of main
git reset --soft HEAD~1 # Undo last commit, keep changes staged
git cherry-pick abc123 # Apply a specific commit
The reflog safety net
git reflog records every change to HEAD, even across resets and rebases. If you “lose” commits, the reflog likely still has them. Entries persist for 90 days by default.
.gitignore
A .gitignore file specifies which files Git should not track: build outputs, node_modules/, .env files, IDE settings. Get it right early — removing already-tracked files from history is painful.
Generate .gitignore files with the .gitignore Generator, reference commands with the Git Cheatsheet, or compare text changes with the Text Diff tool.