KevsRobots Learning Platform
By , Minute
Once your project lives on GitHub, there can be several copies of it: one on your laptop, one on your Raspberry Pi, and maybe some on your friends’ computers. When someone pushes new commits, everyone else needs to bring them down. There are two commands for that:
git fetch downloads new commits from the remote, but doesn’t change your filesgit pull downloads new commits and merges them into your current branchgit fetch
git status
On branch main
Your branch is behind 'origin/main' by 2 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)
origin/main is your computer’s record of where main is on GitHub. Fetching updates it. You can see what’s new before you take it:
git log --oneline main..origin/main
f2b9d31 (origin/main) Add OLED display
a87c5e0 Show distance on screen
That means “commits on origin/main that aren’t on my main”.
git pull
Updating c7d2e19..f2b9d31
Fast-forward
display.py | 22 ++++++++++++++++++++++
main.py | 4 +++-
2 files changed, 25 insertions(+), 1 deletion(-)
Your files now include the new commits. git pull is really just git fetch followed by git merge origin/main, so everything you learned about merging, including conflicts, applies here too.
Pull before you start
Make it a habit: pull when you sit down to work, and push when you finish. The more often you sync, the smaller and rarer conflicts are.
If you’ve committed on your laptop and someone else has pushed to GitHub, your histories have split. Your push gets rejected:
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/your-username/pico-bot.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally.
The fix is to pull first, so Git can combine the two, and then push:
git pull
git push
Recent versions of Git may stop and ask how you want to combine the branches:
fatal: Need to specify how to reconcile divergent branches.
Tell Git once to use a normal merge, which is what this course teaches:
git config --global pull.rebase false
Then run git pull again. (Rebase is a different way of combining branches that rewrites your commits on top of the new ones. It’s worth learning later, but you don’t need it now.)
When people work together on GitHub, they usually don’t push straight to main. Instead they use a pull request (or PR): a request to merge one branch into another, with a page where people can see the changes, discuss them and suggest fixes before anything is merged.
Here’s the flow:
flowchart TD
A["Create a branch<br/>git switch -c add-buzzer"] --> B["Commit your changes"]
B --> C["Push the branch<br/>git push -u origin add-buzzer"]
C --> D["Open a pull request on GitHub"]
D --> E["Review and discuss"]
E -- "changes needed" --> B
E -- "looks good" --> F["Merge into main on GitHub"]
F --> G["Update your copy<br/>git switch main, then git pull"]
After you push a branch, GitHub shows a Compare & pull request button on the repository page. Click it, write a short description of what you changed and why, and click Create pull request. When it’s merged, switch back to main and pull so your copy has the merged code.
Pull requests are useful even on your own projects: they give you one tidy page per feature, showing everything that changed.
You can’t push to other people’s repositories. Instead you fork it: click Fork on GitHub to make your own copy under your account. Clone your fork, make a branch, commit, push it to your fork, and open a pull request back to the original project. That’s how almost all open source contributions work.
README.md directly on GitHub (click the file, then the pencil icon) and commit it there.git fetch and git status. Then git pull.main locally.README.md on GitHub and locally, commit both, then git pull. Resolve the conflict and push.fatal: Need to specify how to reconcile divergent branches.git config --global pull.rebase false, then git pull again.Why: Git wants you to choose between merging and rebasing when both sides have new commits.
git pull reports a merge conflictgit add, git commit, then git push.Why: Your commits and the remote’s commits changed the same lines.
There is no tracking information for the current branchgit push -u origin <branch-name> once, or git branch --set-upstream-to=origin/<branch-name>.Quick quiz 1 of 3
What’s the difference between git fetch and git pull?
git pull is git fetch followed by a merge.Quick quiz 2 of 3
Your git push is rejected because the remote has work you don’t have. What do you do?
Quick quiz 3 of 3
What is a pull request?
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments