๐Ÿ‡ฎ๐Ÿ‡ณ
๐Ÿ‡ฎ๐Ÿ‡ณ
Limited-Time Offer!Get 20% OFF on all live courses
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