Git is a version control system. It tracks every change you make to your code, lets you undo mistakes, and makes it possible for multiple people to work on the same project without overwriting each otherβs work.
Every professional developer uses Git. Itβs not optional β itβs as fundamental as knowing how to use a text editor.
The problem Git solves
Without Git:
project/
app.js
app_backup.js
app_backup2.js
app_FINAL.js
app_FINAL_v2.js
app_FINAL_ACTUALLY_FINAL.js
With Git:
project/
app.js β Git tracks every version internally
Git remembers every change, who made it, and when. You can go back to any previous version at any time.
Key concepts
Repository (repo) β a project folder tracked by Git. Contains your code plus a hidden .git folder with the full history.
Commit β a snapshot of your code at a point in time. Like a save point in a video game.
Branch β a parallel version of your code. Work on a feature without affecting the main code. Merge it back when itβs ready.
Remote β a copy of your repo on a server (like GitHub). Lets you share code and collaborate.
Getting started
# Install Git
# macOS: brew install git
# Ubuntu: sudo apt install git
# Windows: download from git-scm.com
# Configure your identity
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Create a new repo
mkdir my-project
cd my-project
git init
# Or clone an existing repo
git clone https://github.com/user/repo.git
The basic workflow
# 1. Make changes to your files
echo "Hello World" > index.html
# 2. Stage the changes (tell Git what to include)
git add index.html
git add . # Stage everything
# 3. Commit (save a snapshot)
git commit -m "Add homepage"
# 4. Push to GitHub
git push
Thatβs the daily loop: change β add β commit β push.
Checking status and history
# What's changed?
git status
# What exactly changed in each file?
git diff
# View commit history
git log
git log --oneline # Compact view
Branches
Branches let you work on features without breaking the main code:
# Create and switch to a new branch
git checkout -b feature/login
# Make changes, commit them
git add .
git commit -m "Add login page"
# Switch back to main
git checkout main
# Merge the feature branch
git merge feature/login
# Delete the branch (it's merged, you don't need it)
git branch -d feature/login
Working with GitHub
# Push your local repo to GitHub
git remote add origin https://github.com/user/repo.git
git push -u origin main
# Get changes from GitHub
git pull
# Push your changes
git push
The typical team workflow:
- Create a branch
- Make changes and commit
- Push the branch to GitHub
- Open a Pull Request (PR)
- Team reviews the code
- Merge the PR
Undoing things
# Undo changes to a file (before staging)
git checkout -- file.txt
git restore file.txt # Modern syntax
# Unstage a file (after git add, before commit)
git reset HEAD file.txt
git restore --staged file.txt # Modern syntax
# Undo the last commit (keep changes)
git reset --soft HEAD~1
# Undo the last commit (discard changes)
git reset --hard HEAD~1
.gitignore
Tell Git to ignore certain files:
# .gitignore
node_modules/
.env
dist/
*.log
.DS_Store
See: .gitignore cheat sheet for templates.
Common issues
- Git merge conflict β two people changed the same line
- Permission denied (publickey) β SSH key not set up
- fatal: not a git repository β youβre not in a Git repo
- git push rejected β someone pushed before you
Next steps
- Create a GitHub account
- Make a repo, push some code
- Learn branching β itβs the most powerful feature
- Check out the full Git cheat sheet for every command youβll need
FAQ
Whatβs the difference between Git and GitHub?
Git is the version control tool that runs locally on your computer and tracks changes to your code. GitHub is a website that hosts Git repositories online, adding collaboration features like pull requests, issues, and code review. You can use Git without GitHub (with GitLab, Bitbucket, or no remote at all).
How often should I commit?
Commit whenever you complete a small, logical unit of work β a bug fix, a new function, a config change. Aim for commits that are small enough to understand at a glance but complete enough that the code still works. Multiple small commits per day is normal and preferred over one giant commit at the end.
What do I do when I get a merge conflict?
A merge conflict means two people changed the same lines. Git marks the conflicting sections in your file with <<<<<<<, =======, and >>>>>>> markers. Open the file, decide which version to keep (or combine both), remove the markers, then git add and git commit. Your editor likely has a built-in merge conflict resolver that makes this visual.
Related: GitHub vs. GitLab β which platform should you use? | fatal: not a git repository β how to fix it