Essential Git Commands for Developers

    February 12, 20258 min read

    Git is the backbone of modern software development. Whether you're building websites, mobile apps, machine learning models, or full-scale SaaS platforms, Git keeps your code safe, trackable, and shareable.

    But most beginners only learn a few commands — and miss out on the real power Git provides.

    This guide teaches the essential Git commands every developer must know, explained simply, with real-world use cases.

    1. What Is Git and Why It Matters

    Git is a version control system that:

    • ✔ Tracks every change
    • ✔ Lets you undo mistakes
    • ✔ Helps teams collaborate
    • ✔ Allows branching and experimenting safely
    • ✔ Stores your code online (GitHub/GitLab/Bitbucket)

    Even if you're working alone, Git is your safety net.

    2. Setting Up Git

    Check Git version:

    git --version

    Configure your information:

    git config --global user.name "Your Name"
    git config --global user.email "your@email.com"

    This info shows up in commits.

    3. Initialize a New Repository

    Create a new Git repo inside your project:

    git init

    This creates a .git folder — the brain of your repository.

    4. Checking Status of Your Code

    Before committing, always check what changed:

    git status

    It tells you:

    • Which files changed
    • Which files are staged or unstaged
    • If you're ahead/behind remote

    5. Adding Files

    Add a single file:

    git add file.js

    Add ALL files:

    git add .

    This stages your changes for commit.

    6. Creating Commits

    A commit is a snapshot of your code.

    git commit -m "Add login feature"

    Good commit messages:

    • ✔ Short
    • ✔ Clear
    • ✔ Action-based ("Fix bug", "Add feature")

    7. Viewing Commit History

    git log

    For a cleaner history:

    git log --oneline

    This is useful when debugging or understanding project evolution.

    8. Creating and Switching Branches

    Branches let you experiment without breaking main code.

    Create a branch:

    git branch feature-ui

    Switch to it:

    git checkout feature-ui

    Create and switch:

    git checkout -b feature-ui

    9. Merging Branches

    When your feature is ready:

    1. Switch to main branch

    git checkout main

    2. Merge feature branch

    git merge feature-ui

    If there are conflicts, fix them manually in your editor.

    10. Cloning Repositories

    Clone a project from GitHub:

    git clone https://github.com/user/project.git

    This downloads the entire repo to your computer.

    11. Pushing Code to GitHub

    Connect to a remote:

    git remote add origin https://github.com/user/project.git

    Push your code:

    git push -u origin main

    After the first push:

    git push

    12. Pulling Latest Changes

    Get new updates from the online repo:

    git pull

    Equivalent to:

    • fetch (download)
    • merge (apply)

    13. Stashing Work

    Need to switch branches but not ready to commit?

    Use stash:

    git stash

    Restore work later:

    git stash pop

    Perfect for multitasking.

    14. Undoing Mistakes (Lifesavers)

    Unstage a file:

    git reset HEAD file.js

    Undo last commit (keep changes):

    git reset --soft HEAD~1

    Undo last commit completely:

    git reset --hard HEAD~1

    ⚠️ Hard reset is destructive — use carefully.

    15. Deleting Branches

    Delete local branch:

    git branch -d feature-ui

    Force delete:

    git branch -D feature-ui

    16. Best Practices for Developers

    • ✔ Commit small changes often
    • ✔ Use meaningful commit messages
    • ✔ Keep main clean, stable, deploy-ready
    • ✔ Create feature branches
    • ✔ Pull before pushing
    • ✔ Use .gitignore to clean your repo

    Add .gitignore file quickly:

    npx gitignore node
    npx gitignore python

    Conclusion

    Whether you're a beginner or building production apps, mastering Git is non-negotiable.

    These essential commands will help you:

    • ✔ Work faster
    • ✔ Collaborate better
    • ✔ Avoid disasters
    • ✔ Build cleaner projects

    And once Git becomes part of your muscle memory, coding becomes smoother, safer, and far more professional.

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service