A cheatsheet of commands for my workflow.
Most frequently used commands for daily Git workflow.
- git status – check what's changed
- git add . – stage all changes
- git commit -m "msg" – commit changes
- git push – push to remote
- git pull – update from remote
- git log --oneline --graph --decorate --all – view history
- git branch -av – list branches
- git switch – switch branches
Basic commands to move around directories and view files in your terminal.
- cd – go to home
- cd ~/Desktop – absolute path
- cd .. – up one directory
- pwd – print current path
- ls – names
- ls -la – incl. hidden + details
Check what files have changed, what's staged, and view differences before committing.
- git status – what's changed/staged?
- git status -sb – short, with branch
- git diff – unstaged changes
- git diff --staged – staged changes
Commands to clone a repository, view remotes, and sync with the remote repository.
- git clone – HTTPS or SSH
- cd
- git remote -v – view remotes
- git fetch – update refs (no merge)
- git pull – fetch + merge (or rebase; see config below)
The core workflow for saving your changes: stage files, commit them, and push to remote.
- git add . – stage all modified/new files
- git add -p – stage hunks interactively
- git commit -m "meaningful msg" – commit staged changes
- git commit --amend – fix the last commit message or add missed files
- git commit --amend --no-edit – add files to last commit without changing message
- git push -u origin – first push; sets upstream
- git push – subsequent pushes
- git pull --rebase – pull with rebase instead of merge
Initialize a new Git repository and connect it to a remote repository for the first time.
- git init
- git add --all
- git commit -m "initial commit"
- git branch -M main – (optional) rename to main
- git remote add origin
- git push -u origin main
Create and switch between branches to work on features or fixes in isolation.
- git branch -av – list local & remote branches
- git branch – create branch
- git checkout – switch (old syntax)
- git switch – switch (newer, nicer)
- git switch -c – create + switch
- git branch -d – delete merged branch (safe)
- git branch -D – force delete branch (unsafe, use with caution)
Safely integrate changes from one branch into another, keeping a clean history.
- git checkout main
- git pull --ff-only – update main safely
- git checkout
- git merge main – bring latest main into feature
- git checkout main
- git merge – merge feature into main
- git push
Reapply your commits on top of the latest main branch for a linear history, useful before merging.
- git checkout
- git fetch origin
- git rebase origin/main – rebase feature on latest main
- git add – resolve conflicts if needed
- git rebase --continue – continue after fixing conflicts
- git rebase --abort – cancel rebase if needed
- git push --force-with-lease –
⚠️ Only force push if rebasing (never on shared branches)
Temporarily save uncommitted changes so you can switch branches or pull updates without committing.
- git stash – stash tracked changes
- git stash -u – include untracked
- git stash list – list all stashes
- git stash pop – restore & remove from stash
- git stash apply – restore but keep in stash
- git stash drop – delete a stash entry
- git stash clear – delete all stashes
Revert changes, unstage files, undo commits, or recover lost work using reflog.
-
git restore – discard unstaged changes
-
git checkout -- – older syntax
-
git reset HEAD – unstage a staged file
-
git reset --soft HEAD~1 – undo commit, keep staged
-
git reset --mixed HEAD~1 – undo commit, keep changes unstaged
-
git reset --hard HEAD~1 –
⚠️ DESTRUCTIVE: undo commit AND changes (use with caution) -
git revert – make a new commit that undoes (safe for shared branches)
-
git reflog – find lost commits & HEAD moves
-
git reflog show – show reflog for specific branch
View commit history, see who changed what in a file, and inspect specific commits.
- git log --oneline --graph --decorate --all – compact visual history
- git log -p – show patches (full diffs)
- git log --stat – show file stats (additions/deletions)
- git log --follow – track file through renames
- git log --oneline – commits on specific branch
- git blame – see who changed each line
- git show – inspect specific commit
Compare differences between branches, commits, or files to see what changed.
- git diff .. – compare two branches
- git diff HEAD – compare working directory to last commit
- git diff --name-only HEAD~1 – show only changed filenames
- git diff – compare two specific commits
Mark specific commits as release versions for easy reference and deployment.
- git tag v1.0.0
- git tag -a v1.0.0 -m "msg"
- git push origin v1.0.0
- git push --tags
Manage connections to remote repositories, including adding, updating, and syncing remote branches.
- git remote -v – view all remotes
- git remote add origin – add remote
- git remote set-url origin – update remote URL
- git remote remove – remove a remote
- git remote show origin – detailed remote info
- git fetch origin --prune – fetch and remove deleted remote branches
Example patterns to exclude files and directories from being tracked by Git. node_modules/ *.log .env .DS_Store
Remove already-tracked files from Git's index so .gitignore rules take effect.
- git rm -r --cached .
- git add .
- git commit -m "apply .gitignore"
Set up your Git identity and configure default behaviors to streamline your workflow.
- git config --global user.name "Your Name"
- git config --global user.email "you@example.com"
- git config --global pull.rebase true – use rebase instead of merge for pulls
- git config --global init.defaultBranch main – default branch name
- git config --global alias.lg "log --oneline --graph --decorate --all" – custom log alias
- git config --global core.editor "code --wait" – set VS Code as editor (or vim, nano, etc.)
Important warnings and best practices to avoid losing work.
git reset --hard– permanently deletes uncommitted changesgit push --force– overwrites remote history (use--force-with-leaseinstead)git clean -fd– permanently deletes untracked filesgit branch -D– force deletes branch even if not merged
✅ Safe Practices:
- Always use
--force-with-leaseinstead of--forcewhen pushing - Use
git reverton shared branches instead ofgit reset - Check
git statusbefore destructive operations - Use
git reflogto recover lost commits - Stash changes before switching branches if unsure
Apply a specific commit from another branch to your current branch.
- git cherry-pick – apply commit to current branch
- git cherry-pick – pick multiple commits
- git cherry-pick --abort – cancel cherry-pick if conflicts occur
Remove untracked files and delete branches that are no longer needed.
- git clean -n – preview untracked files to be removed (dry run)
- git clean -fd – remove untracked files and directories
- git clean -fX – remove only files ignored by .gitignore
Troubleshoot and fix common issues when pushing to remote, like branch name mismatches or conflicts.
Upstream tracking issues:
- git push -u origin – set upstream and push
Branch name fixes:
- git branch -m – rename branch locally
- git push origin : – delete old remote, push new
- git push -u origin – set upstream for new branch
Sync with remote:
- git fetch origin – update remote refs
- git rebase origin/main – rebase on latest main
- git pull --rebase – pull with rebase
- git push --force-with-lease – safer than --force (checks remote first)
- git push --force –
⚠️ DANGEROUS: overwrites remote (avoid on shared branches)
Common issues and their solutions when things go wrong.
"Your branch is behind origin/main":
- git fetch origin
- git rebase origin/main (or git merge origin/main)
"Updates were rejected because the tip of your current branch is behind":
- git pull --rebase (preferred)
- git pull (creates merge commit)
Accidentally committed to wrong branch:
- git reset --soft HEAD~1 (undo commit, keep changes)
- git stash (save changes)
- git switch
- git stash pop (restore changes)
Lost a commit:
- git reflog – find the commit hash
- git cherry-pick – restore it
Merge conflicts:
- git status – see conflicted files
- Edit files to resolve conflicts
- git add
- git commit (or git rebase --continue if rebasing)