Clone, Push, Pull, and Fetch
10 min read
Getting a Repository: git clone
git clone downloads an entire repository — all history, all branches — and automatically sets up origin pointing to the source URL.
bash
git clone git@github.com:cpcode/project.git # SSH (preferred)
git clone https://github.com/cpcode/project.git # HTTPSSending Changes: git push
After committing locally, push your branch to the remote so teammates and CI can see your work.
bash
git push origin feature/login # push a branch
git push -u origin feature/login # push and set upstream tracking
git push # push to tracked remote (after -u)Fetching vs Pulling
git fetch downloads remote changes but does not modify your working directory or local branches. git pull is git fetch followed by git merge (or git rebase if configured). Understanding the difference matters:
git fetch origin— safe: update your knowledge of the remote without touching your code.git pull origin main— convenient but can create unexpected merge commits.git pull --rebase origin main— cleaner: replays your commits on top of the latest main.
bash
git fetch origin
git log origin/main --oneline # inspect what changed before merging
git merge origin/main # merge on your termsAt CPCODELAB, run git fetch origin && git rebase origin/main before opening a pull request. This ensures your branch is up to date and reduces merge conflicts for your reviewer.
Ready to test yourself?
Practice Git & GitHub— quiz & coding exercises