π Git Basics β Quick Commands & Tips#
A compact cheat-sheet with the Git commands youβll use every day. Fast, practical, no theory β just what works.
π Initialize & Clone#
1
2
3
4
5
| # Start a new repo
git init
# Clone a remote repo
git clone https://github.com/user/repo.git
|
π Staging & Committing
1
2
3
4
5
| git status # see changes
git add file.py # stage file
git add . # stage everything
git commit -m "message" # commit
git commit --amend # edit last commit
|
Tip: Use git add -p to stage changes interactively.
πΏ Branching
1
2
3
4
| git branch # list branches
git branch new-feature # create branch
git checkout new-feature # switch
git switch main # modern switch
|
Tip:
Create & switch in one command:
1
| git switch -c new-branch
|
β¬οΈ Pull / Push
1
2
3
4
| git pull # fetch + merge
git pull --rebase # cleaner history
git push # push commits
git push -u origin main # set upstream
|
π Merging
1
2
| git merge branch-name # merge branch
git merge --no-ff branch-name # preserve history
|
π§½ Cleaning Up
1
2
3
| git stash # save changes temporarily
git stash pop # restore them
git clean -fd # remove untracked files
|
π Diff & Log
1
2
3
4
| git diff # show unstaged changes
git diff --staged # staged changes
git log --oneline # compact history
git log --graph --decorate --all
|
π Undoing Things (Safe)
1
2
3
| git restore file.txt # discard unstaged changes
git restore --staged file.py # unstage
git revert <commit> # new commit reversing changes
|
β’οΈ Undoing Things (Dangerous)
1
2
| git reset --hard <commit> # erase history (careful!)
git push -f # force push
|
π‘ Final Tips
Commit small, meaningful chunks.
Prefer git pull –rebase for a clean history.
Create branches for everything β even tiny fixes.
Use git stash before switching branches.