← Journal

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

CommandDescriptionExample
git initStart a new repogit init my-project
git cloneCopy an existing repogit clone https://github.com/user/repo.git
git addStage changesgit add .
git commitSave staged changesgit commit -m "Add login page"
git statusCheck working treegit status
git logView historygit 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 main always deployable
  • Pull from remote before you push to minimise conflicts
  • Add a .gitignore immediately — before committing node_modules or .env