Git Push, Pull and Fetch
git push, git pull and git fetch move commits between your local repository and a remote. Push uploads your commits, fetch downloads new commits without changing your files, and pull fetches and then merges (or rebases) them into your current branch.
git push
git push # push current branch to its upstream
git push -u origin feature/login # first push of a new branch
git push origin --delete old-branchTo github.com:you/portfolio.git a1b2c3d..9e1f3b7 main -> main
git fetch
fetch is completely safe: it updates your remote-tracking branches (origin/main) but never touches your working files. Use it to see what others have done before deciding how to integrate.
git fetch
git log --oneline main..origin/main # commits on remote not yet in mainFrom github.com:you/portfolio 9e1f3b7..c4d5e6f main -> origin/main c4d5e6f Fix mobile menu b3c4d5e Add dark mode
git pull
If you have local commits that are not on the remote, a plain pull creates a merge commit. pull --rebase keeps a cleaner history by replaying your commits on top.
git pull # fetch + merge
git pull --rebase # fetch + rebase local commits on topUpdating 9e1f3b7..c4d5e6f Fast-forward menu.js | 8 +++++--- theme.css | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-)
When a Push Is Rejected
If someone else pushed first, the remote has commits you do not have, and Git refuses to overwrite them.
git push! [rejected] main -> main (fetch first) error: failed to push some refs to 'github.com:you/portfolio.git' hint: Updates were rejected because the remote contains work that you do not have locally.
The fix is to integrate first: run git pull --rebase (resolve any conflicts), then git push again.
Force Pushing Safely
After rewriting your own branch with amend or rebase, a normal push is rejected. Use --force-with-lease, which only overwrites if the remote still matches what you last fetched, protecting teammates' work. Avoid plain --force, and never force-push to main.
git rebase main
git push --force-with-lease+ 4d5e6f7...9f0a1b2 feature/search -> feature/search (forced update)
Daily Sync Routine
- Start the day with git switch main && git pull.
- Update your feature branch: git switch feature/x && git rebase main (or git merge main).
- Push often so your work is backed up.
- Run git status before and after syncing to know exactly where you are.
Frequently asked questions
What is the difference between git fetch and git pull?
git fetch downloads new commits and updates origin/* branches without changing your files. git pull does a fetch and then merges or rebases those commits into your current branch.
How do I fix "Updates were rejected" in Git?
The remote has commits you do not have. Run git pull --rebase (or git pull), resolve any conflicts, and push again.
What is git push --force-with-lease?
A safer force push that only overwrites the remote branch if it has not changed since your last fetch, so you do not accidentally delete a teammate's commits.