This file is a practical reference for everyday Git usage when working with local projects and GitHub.
git-cheat-sheet.md
Lowercase, hyphen separated, and descriptive is common practice in repositories.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.autocrlf true # recommended on WindowsCheck config:
git config --global --listInitialize local repo and create first commit:
git init
git add . # stage all files in project
git commit -m "Initial commit"Connect to GitHub and push:
git branch -M main # ensure branch name is main
git remote add origin https://github.com/USERNAME/REPO.git
git push -u origin main # push and set upstreamgit init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/USERNAME/REPO.git
git push -u origin mainCheck current state of your working directory (what is modified, staged, or untracked):
git statusStage files (add changes to the next commit):
git add file.py # stage only one file
git add . # stage ALL files in the current folder and subfoldersCommit staged changes with a short message:
git commit -m "Short clear message"Push your commits to GitHub:
git pushPull latest changes from GitHub and merge into your branch:
git pullList local branches (current branch has *):
git branchCreate and switch to a new branch:
git checkout -b feature-xSwitch to an existing branch:
git checkout mainMerge another branch into current branch:
git merge feature-xDelete branch after merge:
git branch -d feature-xCheck status:
git statusStage files:
git add file.py
git add .Commit:
git commit -m "Short clear message"Push:
git pushPull latest changes:
git pullList branches:
git branchCreate and switch:
git checkout -b feature-xSwitch branch:
git checkout mainMerge branch into main:
git checkout main
git merge feature-xDelete branch:
git branch -d feature-xDiscard local changes in a file (before commit):
git restore file.py # restore single file
git restore . # restore ALL files to last commitUnstage file (keep changes, just remove from staging area):
git restore --staged file.pyUndo last commit but keep files (commit message or staging mistake):
git reset --soft HEAD~1Undo last commit and discard files (dangerous, permanent):
git reset --hard HEAD~1Restore file exactly as it was in last commit:
git checkout -- file.pyUndo changes in file (before commit):
git restore file.py
git restore .Unstage file:
git restore --staged file.pyUndo last commit but keep files:
git reset --soft HEAD~1Undo last commit and discard files:
git reset --hard HEAD~1Restore file from last commit:
git checkout -- file.pyCommit history:
git log
git log --oneline --graph --decorateShow changes:
git diff
git diff --stagedShow details of commit:
git show COMMIT_HASHShow configured remotes:
git remote -vChange remote URL (for example when switching GitHub accounts):
git remote set-url origin https://github.com/USERNAME/REPO.gitRemove remote completely:
git remote remove originShow remotes:
git remote -vChange remote URL:
git remote set-url origin https://github.com/USERNAME/REPO.gitRemove remote:
git remote remove originPreview what will be deleted (safe):
git clean -nRemove untracked files only:
git clean -fRemove untracked files and folders (dangerous):
git clean -fdRemove untracked files:
git clean -fRemove untracked files and folders:
git clean -fdPreview first:
git clean -nCreate file:
touch .gitignoreTypical Python ignores:
__pycache__/
*.pyc
.env
.venv/
Abort merge:
git merge --abortReset to remote state:
git fetch origin
git reset --hard origin/main- Add python math examples
- Fix README file
- Refactor sorting logic
- Update folder structure
Avoid:
- update
- fix stuff
- changes
- Commit often, small changes
- Push at least once per day
- Use branches for experiments
- Never be afraid to break things locally, Git can restore almost everything
If you want, you can later split this into:
- Git basics
- GitHub workflow
- Advanced recovery
and turn this repo into a learning resource as well.