Version control is not just a tool—it’s a superpower for developers.
Whether you’re a beginner in programming or a seasoned coder tired of “just winging it” with backups, Git is a must-have skill. And good news: you can learn the basics in under 10 minutes.
This post is your crash course in Git—complete with commands, examples, and best practices—so you can stop being afraid of your terminal and start tracking your code like a pro.
What is Git?
Git is a distributed version control system that tracks changes in your code over time. It lets you:
- Collaborate with others.
- Revert to previous versions.
- Create experimental features without breaking your main codebase.
- Avoid the classic
final_final_THISONE_v2_REALLY_FINAL.zip
disaster.
Prerequisites
Make sure Git is installed:
git --version
If not, install it:
- Windows: git-scm.com
- macOS:
brew install git
- Linux:
sudo apt install git
Step 1: Initialize a Git Repository
Let’s start tracking a project with Git.
mkdir my-project
cd my-project
git init
This creates a hidden .git
folder that starts tracking your project.
Step 2: Add Files and Track Changes
Create a file:
echo "Hello World" > hello.txt
Check Git status:
git status
You’ll see:
Untracked files:
hello.txt
Now stage the file (prepare it for commit):
git add hello.txt
Or to add everything:
git add .
Files are now staged and ready to commit.
Step 3: Commit Your Changes
git commit -m "Initial commit"
You’ve just saved a version of your project.
Step 4: Make Changes and View History
Edit the file:
echo "Learning Git is fun!" >> hello.txt
See the difference:
git diff
Stage and commit:
git add hello.txt
git commit -m "Added a new line to hello.txt"
View commit history:
git log
Use q
to quit the log view.
Step 5: Branchin
Branches let you work on features without affecting the main codebase.
Create a branch:
git branch new-feature
Switch to it:
git checkout new-feature
Make a change, then:
git add .
git commit -m "Added a new feature"
Switch back to main:
git checkout main
Merge your feature in:
git merge new-feature
You just branched and merged!
Step 6: Connect to GitHub
Create a repo on GitHub (e.g., https://github.com/yourname/my-project.git
).
Add remote:
git remote add origin https://github.com/yourname/my-project.git
Push code:
git push -u origin main
Next time, just:
git push
Step 7: Cloning Repos
To download a project:
git clone https://github.com/anotheruser/their-repo.git
Useful Git Commands Cheat Sheet
Action | Command |
---|---|
Check status | git status |
Add file(s) | git add . or git add filename |
Commit | git commit -m "message" |
View history | git log |
Show changes | git diff |
Create branch | git branch branch-name |
Switch branch | git checkout branch-name |
Create and switch | git checkout -b branch-name |
Merge branches | git merge branch-name |
Push to remote | git push |
Pull latest changes | git pull |
Clone repo | git clone URL |
Bonus: Undoing Mistakes
- Undo changes in a file:
git checkout -- filename
- Unstage a file:
git reset filename
- Amend your last commit (e.g., to fix a message):
git commit --amend
- Revert a commit:
git revert <commit-hash>
Best Practices
- Commit often with meaningful messages.
- Use branches for features and fixes.
- Always pull before pushing to avoid conflicts.
- Don’t commit secrets (e.g., API keys) — use
.gitignore
.
Example Workflow
git clone https://github.com/yourname/project.git
cd project
git checkout -b new-feature
# make changes
git add .
git commit -m "Implemented new feature"
git checkout main
git pull origin main
git merge new-feature
git push
Wrapping Up
In just 10 minutes, you’ve learned:
How to create a Git repo
Track and commit files
Use branches
Merge code
Push to GitHub
Work with teams
Git might seem intimidating at first, but it’s an essential tool that’ll make your life as a developer significantly easier. Mastering Git is not just about memorizing commands—it’s about building confidence in your workflow and collaborating effectively.