Pushing and Pulling
With your local repository connected to GitHub, the two commands you use every day are git push (send commits up) and git pull (receive commits down). This lesson covers both, plus git fetch, which pulls without merging.
Pushing commits
Section titled “Pushing commits”git pushAfter the first push with -u origin main, subsequent pushes only need git push. Git already knows the upstream.
If you are on a branch that has no upstream yet:
git push -u origin feature/add-trailheadThis pushes the branch to GitHub and sets the upstream in one step.
What happens when you push
Section titled “What happens when you push”Git sends only the commits GitHub does not already have — not the entire repository. If you have made three commits locally since your last push, only those three objects travel over the network.
If GitHub’s branch has commits that your local branch does not, Git will refuse the push:
! [rejected] main -> main (non-fast-forward)hint: Updates were rejected because the remote contains work that you do not have locally.This means someone else (or you from another machine) pushed commits after your last sync. You need to pull first, then push.
Pulling changes
Section titled “Pulling changes”git pullgit pull does two things in sequence:
- Fetches new commits from the remote (same as
git fetch) - Merges them into your current branch
If the remote has commits you do not, Git merges them. If there are no conflicts, the merge is a fast-forward and your branch simply advances. If there are conflicts, Git pauses and lets you resolve them — the same conflict resolution process as any other merge.
git fetch — download without merging
Section titled “git fetch — download without merging”git fetchgit fetch downloads new commits from GitHub and updates your remote-tracking branches (origin/main, etc.) but does not touch your local branches. Your working tree is unchanged.
After fetching, you can inspect what arrived:
git log main..origin/main --onelineThis shows commits that are on origin/main but not yet on your local main. You can review them before deciding to merge:
git merge origin/mainWhy use fetch instead of pull?
Fetch lets you see what changed before integrating it. On a shared project, you might want to review incoming commits before they touch your working branch. On a solo project, git pull is almost always fine.
The upstream tracking relationship
Section titled “The upstream tracking relationship”When a branch has an upstream set, git status tells you where you stand:
On branch mainYour branch is ahead of 'origin/main' by 2 commits. (use "git push" to publish your local commits)Or, if the remote has commits you lack:
On branch mainYour branch is behind 'origin/main' by 1 commit, and can be fast-forwarded. (use "git pull" to update your local branch)Or, if both sides have diverged:
On branch mainYour branch and 'origin/main' have diverged,and have 1 and 1 different commits each, respectively. (use "git pull" to merge the remote branch into yours)This last state — diverged — is where you need to pull, resolve any conflicts, and then push.
Pull with rebase
Section titled “Pull with rebase”An alternative to git pull (which merges):
git pull --rebaseInstead of creating a merge commit, this replays your local commits on top of the fetched commits. It produces a cleaner linear history. Many teams prefer this for solo-branch work. You do not need to use it now, but you will encounter it in team workflows.
Exercise
Section titled “Exercise”- In your
git-practicefolder (connected to GitHub from the previous lesson), make a new commit locally:
echo "New local note" >> pine-ridge.txtgit add pine-ridge.txtgit commit -m "Add local note for push exercise"-
Run
git statusand observe the “ahead of ‘origin/main’” message. -
Push to GitHub:
git push-
Visit your GitHub repository in a browser and confirm the new commit appears.
-
Run
git fetch(nothing to fetch since you just pushed, but observe the output — it will be silent when there is nothing new). -
Run
git log --oneline --alland observe bothmainandorigin/mainpointing to the same commit.
git pushsends local commits to GitHub. After the first push with-u, you only needgit push.- Git rejects a push if the remote has commits you lack — pull first, then push.
git pullfetches remote commits and merges them into your current branch.git fetchdownloads remote commits and updates remote-tracking branches without touching your local branches.git statusshows how your branch relates to its upstream (ahead / behind / diverged).
Next: cloning — how to get a copy of an existing GitHub repository onto your machine.