
Module 5 Lesson 2: Pushing and pulling changes
Sync your work with the world. Master the 'git push' and 'git pull' commands to upload your local commits and download the latest changes from your teammates.
Module 5 Lesson 2: Pushing and pulling changes
Connecting a remote is the first step. The second step is actually synchronizing the data. In Git, we "push" our work up to the server and "pull" others' work down.
In this lesson, we master the fundamental commands for daily collaboration.
1. Sending Work Up (git push)
git push takes your local commits and uploads them to the remote repository.
The first push (Setting Upstream)
The very first time you push a new branch, you need to tell Git which remote branch it should "track."
git push -u origin main
The -u (upstream) flag links your local main to the origin/main. In the future, you can just type git push.
Pushing specific branches
git push origin feature-login
2. Pulling Work Down (git pull)
git pull is used to update your local repository with the latest changes from the server.
git pull origin main
What git pull actually does:
git pull is actually a combination of two separate commands:
git fetch: Downloads the new data from the server (without changing your files).git merge: Merges that data into your current local branch.
graph TD
Remote["Remote Server"] -- "git pull" --> Local["Local Machine"]
Local -- "git push" --> Remote
subgraph "Internal Pull Action"
Fetch["1. Fetch (Download)"] --> Merge["2. Merge (Integrate)"]
end
3. The "Rejected" Push
If a teammate has pushed changes to the same branch since you last pulled, Git will reject your push with an error like: "failed to push some refs... Updates were rejected because the remote contains work that you do not have locally."
How to fix it:
- Pull the new changes:
git pull origin main. - Resolve any conflicts (see Module 4).
- Commit the merge.
- Push again:
git push origin main.
4. Force Pushing (The "Nuclear" Option)
Sometimes you want the server to match your local history exactly, even if it means erasing what's on the server.
git push --force
CAUTION: Only use this on private branches. If you force-push to a shared branch (like main), you will break the repository for everyone else on your team!
Lesson Exercise
Goal: Understand the Sync check.
- Go to any GitHub repo you own (or create a temporary one).
- Create a file directly on the GitHub website (using the "Add file" button). This simulates a "teammate" making a change.
- Now, in your local terminal, make a different change and try to
git push. - Observe the rejection error.
- Run
git pull. Resolve the merge if necessary. - Now try
git pushagain.
Observation: You'll see that Git enforces a "History First" rule—you must have the latest version of the world before you are allowed to change it.
Summary
In this lesson, we established:
git pushuploads local commits to the remote.-usets the default upstream tracking for a branch.git pulldownloads AND merges remote changes.- You must pull before you can push if the remote has new work.
--forceis powerful but dangerous for team collaboration.
Next Lesson: git pull can be a bit aggressive. We’ll learn about its safer sibling, Lesson 3: Fetch vs pull.