MagmaNex LogoMagmaNex
Home/Blog/Essential Git Commands Every Developer Should Know
Essential Git Commands Every Developer Should Know
GuideMay 22, 2026· 10 min read

Essential Git Commands Every Developer Should Know

A comprehensive guide to the most important Git commands — from basics to advanced workflows. Bookmark this for your daily development.

#git#version control#devops#cheatsheet

Essential Git Commands Every Developer Should Know

Git is the backbone of modern software development. Whether you're working solo or in a team of hundreds, mastering Git is non-negotiable. This cheatsheet covers the commands you'll use every single day.

Initial Setup

Before your first commit, configure your identity:

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait"   # Use VS Code as editor
git config --global init.defaultBranch main      # Default branch name

Starting a Repository

git init                    # Initialize a new repo in the current directory
git clone <url>             # Clone a remote repository
git clone <url> my-folder   # Clone into a specific folder

Staging & Committing

git status                  # Show working tree status
git add file.txt            # Stage a specific file
git add .                   # Stage all changes
git add -p                  # Interactively stage hunks

git commit -m "message"     # Commit with a message
git commit --amend          # Amend the last commit (before pushing)
git commit --amend --no-edit  # Amend without changing the message

Writing Good Commit Messages

Follow the conventional commits format for cleaner history:

feat: add user authentication
fix: resolve null pointer in payment flow
docs: update README with setup instructions
refactor: extract validation logic to separate module
chore: upgrade dependencies

Branching

git branch                  # List local branches
git branch -a               # List all branches (including remote)
git branch feature/login    # Create a new branch
git checkout feature/login  # Switch to a branch
git checkout -b feature/login  # Create AND switch (shorthand)
git switch -c feature/login    # Modern equivalent (Git 2.23+)

git branch -d feature/login    # Delete a branch (safe — merged only)
git branch -D feature/login    # Force delete a branch

Merging & Rebasing

git merge feature/login     # Merge a branch into current
git merge --no-ff feature   # Always create a merge commit
git merge --squash feature  # Squash all commits into one

git rebase main             # Rebase current branch onto main
git rebase -i HEAD~3        # Interactive rebase (last 3 commits)

Merge vs Rebase:

  • Merge preserves history exactly as it happened — good for public/shared branches
  • Rebase rewrites history for a cleaner, linear log — good for local feature branches

Remote Repositories

git remote -v               # List remote connections
git remote add origin <url> # Add a remote
git fetch origin            # Download changes (don't apply)
git pull                    # Fetch + merge
git pull --rebase           # Fetch + rebase (cleaner history)
git push origin main        # Push to remote
git push -u origin feature  # Push and set upstream tracking
git push --force-with-lease # Safe force push (checks remote hasn't changed)

Viewing History

git log                     # Full commit history
git log --oneline           # Condensed one-line-per-commit
git log --oneline --graph   # Branch graph view
git log -p                  # Show diffs per commit
git log --author="Name"     # Filter by author
git log --since="2 weeks ago"

git diff                    # Unstaged changes
git diff --staged           # Staged changes
git diff main..feature      # Compare two branches

Undoing Changes

# Discard unstaged changes in a file
git checkout -- file.txt
git restore file.txt        # Modern equivalent

# Unstage a file
git reset HEAD file.txt
git restore --staged file.txt  # Modern equivalent

# Undo the last commit (keep changes staged)
git reset --soft HEAD~1

# Undo the last commit (keep changes unstaged)
git reset HEAD~1

# Undo the last commit and DISCARD changes (destructive!)
git reset --hard HEAD~1

# Create a new commit that reverts a previous one (safe for shared branches)
git revert <commit-hash>

Stashing

Stash is your "save for later" drawer:

git stash                   # Stash current changes
git stash push -m "WIP auth"  # Stash with a label
git stash list              # See all stashes
git stash pop               # Apply latest stash and remove it
git stash apply stash@{2}   # Apply specific stash without removing
git stash drop stash@{0}    # Delete a stash
git stash clear             # Delete all stashes

Tags

git tag                     # List all tags
git tag v1.0.0              # Create a lightweight tag
git tag -a v1.0.0 -m "Release 1.0"  # Annotated tag
git push origin v1.0.0      # Push a tag
git push origin --tags      # Push all tags

Advanced: Cherry-Pick

Apply a specific commit from another branch:

git cherry-pick <commit-hash>        # Apply one commit
git cherry-pick abc123..def456       # Apply a range of commits
git cherry-pick --no-commit <hash>   # Apply without committing

Advanced: Git Bisect

Find the commit that introduced a bug using binary search:

git bisect start
git bisect bad              # Current commit is buggy
git bisect good v1.0.0      # This version was fine
# Git checks out the midpoint — test and mark:
git bisect good             # or
git bisect bad
# Repeat until Git finds the offending commit
git bisect reset            # Return to original state

Useful Aliases

Add these to ~/.gitconfig:

[alias]
  st = status
  co = checkout
  br = branch
  lg = log --oneline --graph --all
  undo = reset HEAD~1
  aliases = config --get-regexp alias

Gitignore Tips

Use our .gitignore Generator to create the perfect .gitignore for your project. Common patterns:

node_modules/
.env
.env.local
dist/
build/
*.log
.DS_Store

Quick Reference Card

Task Command
New branch git checkout -b name
Stage all git add .
Commit git commit -m "msg"
Push git push origin branch
Pull latest git pull --rebase
Check status git status
View log git log --oneline
Undo last commit git reset HEAD~1
Stash changes git stash

Mastering these commands will make you significantly more productive. For timestamp conversions and other dev utilities, check out our Unix Timestamp Converter and other tools at MagmaNex.


🛠 Related Tools

📚 Related posts

← All posts🛠 Explore tools