Git is the industry-standard version control system that every developer needs to know. Whether you're working solo or on a team, Git helps you track changes, collaborate effectively, and never lose your work. This guide will walk you through real-world scenarios you'll encounter as a developer.
What is Git and Why Should You Care?
Imagine you're building a website and accidentally delete an important file. Or you try a new feature that breaks everything, and you can't remember what you changed. Or three developers are working on the same project and keep overwriting each other's work. Git solves all these problems.
Git is like a time machine for your code. It tracks every change, lets you experiment without fear, and makes collaboration seamless.
Getting Started: Your First Repository
Use Case 1: Starting a New Project
You're building a personal portfolio website. Here's how to set up Git tracking from day one.
# Create your project folder
mkdir my-portfolio
cd my-portfolio
# Initialize Git
git init
# Create your first file
echo "# My Portfolio" > README.md
# Check what Git sees
git status
# Stage the file
git add README.md
# Make your first commit
git commit -m "Initial commit: Add README"
What just happened? You created a Git repository and saved your first snapshot. Now you can always return to this point.
Use Case 2: Building Your Project with Regular Commits
You're adding pages to your portfolio. Here's how to commit as you work:
# Create an HTML file
touch index.html
# Add some code, then check status
git status
# Stage and commit
git add index.html
git commit -m "Add homepage structure"
# Add CSS
touch styles.css
git add styles.css
git commit -m "Add base styling"
# View your history
git log --oneline
Pro tip: Commit whenever you complete a meaningful piece of work. Good commit messages are like breadcrumbs that help you understand your project's evolution.
Working with Remote Repositories (GitHub)
Use Case 3: Backing Up Your Work
You want to save your portfolio online and show it to potential employers.
# On GitHub, create a new repository (don't initialize with README)
# Connect your local repo to GitHub
git remote add origin https://github.com/yourusername/my-portfolio.git
# Push your code
git branch -M main
git push -u origin main
Now your code is backed up in the cloud and has a shareable URL.
Use Case 4: Cloning a Project to Work On
You're joining a team project or want to contribute to open source.
# Clone the repository
git clone https://github.com/company/project-name.git
# Navigate into it
cd project-name
# Check the branches
git branch -a
# Start working
# ... make changes ...
# Stage, commit, and push
git add .
git commit -m "Fix navigation bug"
git push origin main
Branching: Experimenting Safely
Use Case 5: Adding a New Feature
You want to add a blog section to your portfolio but don't want to break the working site.
# Create and switch to a new branch
git checkout -b add-blog-section
# Make your changes
touch blog.html
# ... add blog code ...
# Commit on the branch
git add blog.html
git commit -m "Add blog page with recent posts"
# Switch back to main to see your site without the blog
git checkout main
# Once you're happy with the blog, merge it
git checkout main
git merge add-blog-section
# Delete the feature branch (optional)
git branch -d add-blog-section
Why branches matter: You can experiment without breaking your working code. If the experiment fails, just delete the branch.
Use Case 6: Working on Multiple Features
You're building a contact form and redesigning the header simultaneously.
# Create branch for contact form
git checkout -b contact-form
# ... work on contact form ...
git add contact.html
git commit -m "Add contact form"
# Switch to work on header
git checkout main
git checkout -b redesign-header
# ... work on header ...
git add styles.css index.html
git commit -m "Redesign header with new logo"
# View all branches
git branch
# Merge both when ready
git checkout main
git merge contact-form
git merge redesign-header
Collaboration Scenarios
Use Case 7: Team Development Workflow
You're working with two other developers on an e-commerce site.
# Start your day by getting the latest code
git pull origin main
# Create a branch for your task
git checkout -b add-shopping-cart
# Work and commit
# ... build shopping cart ...
git add cart.js cart.html
git commit -m "Implement shopping cart functionality"
# Push your branch to GitHub
git push origin add-shopping-cart
# On GitHub, create a Pull Request for code review
# After approval, merge on GitHub
# Then update your local main
git checkout main
git pull origin main
Use Case 8: Handling Merge Conflicts
You and a teammate both edited the same file. Git doesn't know which changes to keep.
# You try to merge and get a conflict
git merge teammate-branch
# Git tells you: CONFLICT in styles.css
# Open styles.css and you'll see:
# <<<<<<< HEAD
# Your changes
# =======
# Teammate's changes
# >>>>>>> teammate-branch
# Edit the file to keep what you want
# Remove the conflict markers
# Save the file
# Stage and commit the resolution
git add styles.css
git commit -m "Resolve styling conflict"
Common Mistakes and How to Fix Them
Use Case 9: Undoing Changes
You made changes but haven't committed yet and want to start over.
# Discard changes in a specific file
git checkout -- index.html
# Discard all uncommitted changes
git reset --hard
Use Case 10: Fixing Your Last Commit
You committed but forgot to include a file or made a typo in the message.
# Add the forgotten file
git add forgotten-file.js
# Amend the previous commit
git commit --amend -m "Corrected commit message"
Use Case 11: Reverting a Pushed Commit
You pushed a commit that broke production. You need to undo it.
# Find the commit hash
git log --oneline
# Create a new commit that undoes it
git revert abc1234
# Push the revert
git push origin main
Advanced But Essential Commands
Use Case 12: Viewing Changes Before Committing
You've been coding for hours and want to review what you changed.
# See what changed in tracked files
git diff
# See what changed in staged files
git diff --staged
# See changes in a specific file
git diff index.html
Use Case 13: Working with Stash
You're working on a feature but need to quickly switch to fix a bug.
# Save your work temporarily
git stash
# Switch branches and fix the bug
git checkout main
# ... fix bug ...
git commit -m "Fix critical bug"
# Go back to your feature
git checkout feature-branch
# Restore your work
git stash pop
Essential .gitignore Patterns
Create a .gitignore
file to exclude files Git shouldn't track:
# Dependencies
node_modules/
vendor/
# Environment variables
.env
.env.local
# Build outputs
dist/
build/
*.min.js
# IDE files
.vscode/
.idea/
*.swp
# OS files
.DS_Store
Thumbs.db
# Logs
*.log
logs/
Best Practices for Entry-Level Developers
1. Commit often, commit early
Make small, focused commits rather than giant ones. It's easier to understand and revert if needed.
2. Write meaningful commit messages
Bad: "fixed stuff"
Good: "Fix login button alignment on mobile devices"
3. Pull before you push
Always get the latest code before pushing your changes to avoid conflicts.
4. Use branches for everything
Never work directly on main. Always create a branch for new features or fixes.
5. Review before committing
Use git diff
to see what you're about to commit. Avoid committing debug code or console.logs.
6. Don't commit sensitive data
Never commit passwords, API keys, or personal information. Use environment variables and .gitignore.
7. Keep commits atomic
Each commit should represent one logical change. Don't mix bug fixes with new features.
Quick Reference Cheat Sheet
# Setup
git init # Start tracking a folder
git clone <url> # Copy a remote repository
# Daily workflow
git status # Check what changed
git add <file> # Stage specific file
git add . # Stage all changes
git commit -m "message" # Save snapshot
git push origin main # Upload to GitHub
git pull origin main # Download latest
# Branching
git branch # List branches
git branch <name> # Create branch
git checkout <name> # Switch branch
git checkout -b <name> # Create and switch
git merge <branch> # Merge branch into current
# Inspection
git log # View commit history
git log --oneline # Compact history
git diff # See uncommitted changes
git diff --staged # See staged changes
# Undo
git checkout -- <file> # Discard file changes
git reset HEAD <file> # Unstage file
git commit --amend # Fix last commit
git revert <commit> # Undo a commit
# Collaboration
git remote -v # Show remote URLs
git fetch origin # Download without merging
git pull origin <branch> # Fetch and merge
git push origin <branch> # Upload branch
Next Steps
Now that you understand the basics, practice with a real project. Start with your own code, then contribute to open-source projects. The more you use Git, the more natural it becomes.
Remember: everyone makes Git mistakes. The key is learning how to fix them. When in doubt, git status
and git log
are your best friends.
Alternatively you code use GUI for git manipulation like Gitkraken, or source code extensions in your IDE, it would help you at first to be familiar and speed up things, but without good understanding of what your are doing, working on remote server, where you wont have a GUI, would be hard and unease.
As always if you have any thoughts, or criticizm, dont hesitate, leave a comment! Cheers
Top comments (0)