Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training
DevOps⏱️ 9 min readπŸ“… Jul 10

Git Commands Cheat Sheet: 40 Commands Every Developer Must Know (2026)

SK
Sanjay Kulkarniβ€’DevOps Engineer
πŸ“‘ Contents (12 sections)

πŸ“ŒWhy Every Developer Needs Git

Git is the number one tool interviewers assume you already know. You will use it every single day of your career. This cheat sheet covers the commands that matter, grouped by task. Level up with our Git & GitHub Actions course.

πŸ“ŒSetup & Config

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main

πŸ“ŒStarting a Repository

git init # create a new repo
git clone <url> # copy an existing repo
git remote add origin <url> # connect to a remote

πŸ“ŒThe Daily Loop

git status # what changed
git add <file> # stage a file
git add . # stage everything
git commit -m "message" # commit staged changes
git push origin main # upload to remote
git pull origin main # fetch + merge remote changes

πŸ“ŒBranching (Your Bread and Butter)

git branch # list branches
git branch feature-x # create a branch
git checkout feature-x # switch to it
git checkout -b feature-x # create + switch in one step
git switch feature-x # modern alternative to checkout
git merge feature-x # merge into current branch
git branch -d feature-x # delete a merged branch

πŸ“ŒInspecting History

git log --oneline --graph --all # pretty history
git diff # unstaged changes
git diff --staged # staged changes
git show <commit> # details of one commit
git blame <file> # who changed each line

πŸ“ŒUndoing Mistakes (The Life-Savers)

git restore <file> # discard unstaged changes
git restore --staged <file> # unstage a file
git commit --amend # fix the last commit message/content
git reset --soft HEAD~1 # undo last commit, keep changes staged
git reset --hard HEAD~1 # undo last commit, DISCARD changes (careful!)
git revert <commit> # safely undo a pushed commit
git reflog # recover "lost" commits

> Rule of thumb: revert for shared/pushed history, reset only for local commits.

πŸ“ŒStashing (Park Work Temporarily)

git stash # save uncommitted work
git stash pop # restore it
git stash list # see all stashes

πŸ“ŒRewriting & Syncing

git rebase main # replay your commits on top of main
git rebase -i HEAD~3 # squash/edit last 3 commits
git cherry-pick <commit> # apply one commit onto current branch
git fetch --prune # sync remote branch list

πŸ“ŒThe Interview Favourites

Merge vs Rebase? Merge preserves history with a merge commit; rebase creates a linear history by replaying commits. Never rebase shared/public branches.

What is HEAD? A pointer to your current commit/branch.

git fetch vs git pull? fetch downloads changes without merging; pull = fetch + merge.

How do you resolve a merge conflict? Open the conflicted files, fix the <<<<<<< / ======= / >>>>>>> markers, git add them, then commit.

How do you undo a pushed commit safely? git revert, it creates a new commit that reverses the change without rewriting shared history.

πŸ“ŒA Clean Feature-Branch Workflow

git switch -c feature/login # 1. branch off main
# ...make changes...
git add . && git commit -m "Add login" # 2. commit
git push -u origin feature/login # 3. push
# 4. open a Pull Request on GitHub β†’ review β†’ merge

πŸ“ŒNext Step: Automate It

Once you're comfortable with Git, automate testing and deployment on every push with GitHub Actions. That combo, Git + CI/CD, is what real teams run every day.

SK

Written by

Sanjay Kulkarni

DevOps Engineer

πŸš€ Master DevOps

Live online + 1-on-1 DevOps Engineering training Β· Join 5000+ developers

Not sure what to do? Book career counselling (β‚Ή200 / 30 min)

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min