# Git & GitHub Cheatsheet > A field guide to the Git commands you reach for when you need to sync safely, branch without fear, inspect history, and recover from the most common mistakes. Fetch first. Read the diff before pulling. Branch before you break things. Source: https://git.cobanov.dev/ Part of the cobanov.dev cheatsheet set. Companions: https://vim.cobanov.dev/llms.txt, https://tmux.cobanov.dev/llms.txt ## 01. Start the day without surprises Refresh your local view of the remote before changing anything. ``` git fetch origin git status git log HEAD..origin/main --oneline ``` ## 02. Stage, commit, and push your changes The classic path when your work is ready to leave your machine. ``` git add . git commit -m "message" git push ``` ## 03. See whether you need to pull or push Compare local and remote after a fetch, without mutating the branch. ``` git fetch origin # commits you need to pull git log HEAD..origin/main --oneline # commits you still need to push git log origin/main..HEAD --oneline ``` ## 04. Start a new feature branch Keep main stable and isolate the new work from the start. ``` git checkout main git pull git checkout -b feature/name git push -u origin feature/name ``` ## 05. Undo the latest commit locally Use the soft reset when you want the changes back in your working tree. ``` # undo commit, keep the changes git reset --soft HEAD~1 # undo commit and discard changes git reset --hard HEAD~1 ``` ## 06. Pause work and switch context quickly Useful when you need to jump to another branch before this task is clean. ``` git stash git checkout other-branch # do the quick fix git checkout - git stash pop ``` ## 07. Resolve a merge conflict step by step Fix the marked sections, then stage and commit the resolved files. ``` git status # edit files and remove conflict markers <<<<<<< HEAD your change ======= their change >>>>>>> origin/main git add . git commit -m "resolve merge conflict" ``` ## 08. Throw away changes in a file Restore the file from the current commit, or unstage it if needed. ``` git restore path/to/file git restore --staged path/to/file ``` ## 09. Inspect what changed and who changed it The fastest history checks when you are debugging or reviewing. ``` git log --oneline -10 git diff git blame path/to/file ``` ## 10. List branches, switch, and clean up Useful after a feature lands and you want to prune local leftovers. ``` git branch -a git checkout main git branch -d feature/name ``` ## 11. Force-push with the safer option Only use this on your own branch when rewriting history is intentional. ``` git push --force-with-lease # safer than --force # it stops if someone else updated remote ``` ## 12. Create and push a version tag Mark a release point so the team can reference it later. ``` git tag v1.0.0 git push origin v1.0.0 git tag --list ``` ## Rules of thumb - git fetch before git pull - branch small, commit clearly, push intentionally