Pull & push
Sharing work in both directions — and what "rejected" really means.
1 Learn the idea
Two commands move work between you and origin: git push sends your commits up, git pull brings theirs down.
What pull actually does
git fetch: download new commits and updateorigin/main.git merge origin/main: integrate them into your current branch (a fast-forward if you had nothing new, otherwise a merge commit).- With
git pull --rebasestep 2 becomes a rebase: your local commits are replayed on top of theirs, keeping history in a straight line.
2 Watch it happen
Press Play or use → to step through. Watch the graph as each command runs.
Read this demo as text
$ edit about.md "About us" $ git add about.md $ git commit -m "Add about page"
You commit locally.
mainis now 1 ahead oforigin/main.$ git push
git pushcopies the commit to origin and moves origin'smain(and yourorigin/mainbookmark) forward. Everybody can see it now.$ teammate push -m "Update README" -f README.md -c "# The Shop"
Meanwhile, Sam pushes a commit of their own to origin.
$ edit contact.md "mail@shop.test" $ git add contact.md $ git commit -m "Add contact page"
You commit something too, without knowing about Sam's work.
$ git push
Rejected! origin has a commit you do not have. Pushing would throw Sam's work away, so git refuses. Nothing is broken — you just need to integrate first.
$ git pull
git pull= fetch + merge. Histories diverged, so git creates a merge commit tying Sam's commit and yours together.$ git push
Now your
maincontains everything origin has, so the push is a simple move forward. Accepted.$ teammate push -m "Add prices" -f prices.md -c "5 EUR" $ edit faq.md "Q&A" $ git add faq.md $ git commit -m "Add FAQ"
Same situation again: Sam pushed, you committed. This time we will avoid the merge commit.
$ git pull --rebase
pull --rebasefetches, then replays your FAQ commit on top of Sam's. Straight-line history, no merge bubble. (The old FAQ commit is left behind, faded.)$ git push
Push works again. Many teams set
git config pull.rebase trueto make this the default.
- Keeps exact history
- Adds a merge commit when diverged
- Never rewrites your commits
- Linear history
- Replays your local commits (new hashes)
- Fine for commits you have not pushed yet
git pullfetch + merge the upstream into your branchgit pull --rebasefetch + replay your commits on topgit push -u origin <branch>First push of a branch; remembers the upstreamgit push --force-with-leaseOverwrite remote only if nobody else pushed3 Check your understanding
Your push is rejected with "fetch first". What is the usual fix?
git pull is a shortcut for…
You created branch search and run git push. Git says it has no upstream branch. What now?
Look at the graph: main and origin/main have diverged. You run git pull --rebase. What happens to your local commit?
4 Practice for real
Publish everything, cleanly
You are on branch search. 1) Publish it so it tracks origin/search. 2) Your local main has a commit "Add footer", but Sam already pushed "Add banner". Get your footer onto origin's main without a merge commit.