📌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 repogit clone <url> # copy an existing repogit remote add origin <url> # connect to a remote📌The Daily Loop
git status # what changedgit add <file> # stage a filegit add . # stage everythinggit commit -m "message" # commit staged changesgit push origin main # upload to remotegit pull origin main # fetch + merge remote changes📌Branching (Your Bread and Butter)
git branch # list branchesgit branch feature-x # create a branchgit checkout feature-x # switch to itgit checkout -b feature-x # create + switch in one stepgit switch feature-x # modern alternative to checkoutgit merge feature-x # merge into current branchgit branch -d feature-x # delete a merged branch📌Inspecting History
git log --oneline --graph --all # pretty historygit diff # unstaged changesgit diff --staged # staged changesgit show <commit> # details of one commitgit blame <file> # who changed each line📌Undoing Mistakes (The Life-Savers)
git restore <file> # discard unstaged changesgit restore --staged <file> # unstage a filegit commit --amend # fix the last commit message/contentgit reset --soft HEAD~1 # undo last commit, keep changes stagedgit reset --hard HEAD~1 # undo last commit, DISCARD changes (careful!)git revert <commit> # safely undo a pushed commitgit 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 workgit stash pop # restore itgit stash list # see all stashes📌Rewriting & Syncing
git rebase main # replay your commits on top of maingit rebase -i HEAD~3 # squash/edit last 3 commitsgit cherry-pick <commit> # apply one commit onto current branchgit 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. commitgit 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.