Tech News, Tech & Coding · Free Online Tools

IT Question Answer
Tools
Back to Cheatsheet
Git Commands Cheat Sheet

Git Commands Cheat Sheet

CheatsheetBy Muhammad Fareed
cheatsheetcommandscheatsheet

Short answer: a typical Git day is git status, git add, git commit, git pull and git push. This sheet groups the rest of the commands by what you are trying to do, so you can find the right one quickly.

Setup

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git init                      # start a repository in the current folder
git clone URL                 # copy a remote repository

Daily workflow

CommandPurpose
git statusShow changed and staged files
git add file / git add .Stage a file / everything
git add -pStage changes piece by piece
git commit -m "message"Save staged changes as a commit
git diff / git diff --stagedSee unstaged / staged changes
git pullFetch and merge remote changes
git pushUpload your commits

Branches

CommandPurpose
git branchList local branches
git switch -c feature-xCreate and switch to a branch
git switch mainSwitch branches
git merge feature-xMerge a branch into the current one
git branch -d feature-xDelete a merged branch
git rebase mainReplay your commits on top of main

Older tutorials use git checkout for switching; git switch is clearer. Compare strategies in Git branching strategies and merge vs rebase.

Remotes

git remote -v                          # list remotes
git remote add origin URL              # connect a remote
git fetch                              # download without merging
git push -u origin main                # first push, set upstream
git push origin --delete old-branch    # delete a remote branch

Inspect history

git log --oneline --graph --decorate --all
git log -p file.txt          # changes to one file
git show abc1234             # one commit
git blame file.txt           # who changed each line
git reflog                   # history of where HEAD has been

Undo things safely

SituationCommandSafe?
Unstage a filegit restore --staged fileYes
Discard local edits to a filegit restore fileLoses uncommitted edits
Fix the last commit messagegit commit --amendOnly if not pushed
Undo a commit by adding a new onegit revert abc1234Yes, safe for shared branches
Move back, keep changesgit reset --soft HEAD~1Yes
Move back, delete changesgit reset --hard HEAD~1Destructive

If you lose a commit, git reflog often lets you find and recover it.

Stash: save work temporarily

git stash push -m "wip login"
git stash list
git stash pop            # reapply and remove the latest stash

Details in Git stash.

Tags

git tag v1.0.0
git push origin v1.0.0

Resolve merge conflicts

  1. Run git status to see conflicted files.
  2. Open each file and edit the sections between <<<<<<<, ======= and >>>>>>>.
  3. git add file and then git commit (or git rebase --continue).

Full walkthrough: how to resolve Git merge conflicts.

Ignore files

# .gitignore
node_modules/
.env
dist/

Golden rules

  • Commit small, with clear messages.
  • Never rewrite history (--amend, rebase, reset --hard then force push) on branches others use.
  • Pull before you push.
  • Never commit secrets.

Learn more

Start with what Git is and Git basics.