Git Version Control: A Beginner's Guide
Learn the fundamentals of Git — from basic commands and branching to working with remote repositories and common workflows.
Git is a distributed version control system that lets you track changes in your code, collaborate with others, and manage different versions of your project — a time machine for code.
Installation
# macOS
brew install git
# Ubuntu / Debian
sudo apt update && sudo apt install git
# Windows — download from git-scm.com
# Verify
git --version
Essential Commands
| Command | Description | Example |
|---|---|---|
git init | Start a new repo | git init my-project |
git clone | Copy an existing repo | git clone https://github.com/user/repo.git |
git add | Stage changes | git add . |
git commit | Save staged changes | git commit -m "Add login page" |
git status | Check working tree | git status |
git log | View history | git log --oneline |
Your First Workflow
git init
echo "# My Project" > README.md
git status
git add README.md
git commit -m "Add README"
git log
Branching
# Create and switch to a new branch in one step
git checkout -b feature/login
# See all branches
git branch
# Merge back to main
git checkout main
git merge feature/login
# Delete the merged branch
git branch -d feature/login
Working with Remotes
git remote add origin https://github.com/username/repo.git
git push -u origin main # first push (sets tracking)
git pull origin main # pull latest changes
git fetch origin # fetch without merging
Best Practices
- Commit often with meaningful, imperative messages (
"Fix null pointer in login"not"fix stuff") - Keep each commit focused on a single logical change
- Use branches for every feature or bug fix — keep
mainalways deployable - Pull from remote before you push to minimise conflicts
- Add a
.gitignoreimmediately — before committingnode_modulesor.env