🇮🇳
🇮🇳
Limited-Time Offer!Get 20% OFF on all live courses
Enroll Now
P
PrakalpanaLive online tech training
DevOps⏱️ 9 min read📅 Jul 10

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

SK
Sanjay KulkarniDevOps 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

Join 5000+ developers

Explore Courses →