πŸš€ 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.