Lesson 6: Push and Pull
What You'll Learn
In this lesson, you'll learn how to synchronize your local repository with GitHub using git push and git pull. These are the commands you'll use daily to share your work and get updates from others.
Understanding Push and Pull
| Command | Direction | Purpose |
|---|---|---|
| git push | Local → GitHub | Upload your commits to GitHub |
| git pull | GitHub → Local | Download commits from GitHub |
Think of it like:
- Push = Uploading photos to Google Photos
- Pull = Downloading photos from Google Photos
Part 1: Pushing Changes to GitHub
The Basic Push Command
git push
This uploads your committed changes to GitHub.
First Time Push
The first time you push a branch, use:
git push -u origin main
Understanding the Flags
| Part | Meaning |
|---|---|
-u |
Sets upstream (tells Git where to push by default) |
origin |
Name of the remote (GitHub repository) |
main |
Name of the branch you're pushing |
After the first time, you can just use git push.
Part 2: Complete Push Workflow
Here's the typical workflow for making changes and pushing them:
# 1. Make changes to your files
# (edit index.html, add new files, etc.)
# 2. Check what changed
git status
# 3. Stage the changes
git add .
# 4. Commit with a message
git commit -m "Add contact form"
# 5. Push to GitHub
git push
Example Scenario
Let's say you added a new feature:
$ git add .
$ git commit -m "Add navigation menu"
[main abc1234] Add navigation menu
1 file changed, 15 insertions(+)
$ git push
Counting objects: 3, done.
Writing objects: 100% (3/3), 287 bytes | 287.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:username/my-project.git
def5678..abc1234 main -> main
Your changes are now on GitHub! 🎉
Part 3: Pulling Changes from GitHub
The Pull Command
git pull
This downloads commits from GitHub and merges them into your local branch.
When to Pull
- Before starting work - get the latest code
- When collaborating - get changes from teammates
- When you edit on different computers - sync your work
- Before pushing - ensure you have the latest changes
Part 4: Understanding git fetch
Git pull is actually two commands combined:
git pull = git fetch + git merge
| Command | What It Does |
|---|---|
git fetch |
Downloads commits but doesn't merge them |
git pull |
Downloads and merges commits automatically |
When to Use Fetch
# Check for remote changes without merging
git fetch
# See what would change
git log HEAD..origin/main
# If you like the changes, merge them
git merge origin/main
Part 5: Common Push/Pull Scenarios
Scenario 1: Push Rejected
If someone else pushed changes first, you'll see:
$ git push
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'github.com:username/project.git'
Solution: Pull first, then push
git pull
git push
Scenario 2: Working on Multiple Computers
On Computer A:
git add .
git commit -m "Update homepage"
git push
On Computer B:
git pull # Get the changes from Computer A
# Continue working...
Scenario 3: Edited Same File on GitHub and Locally
If you edit a file on GitHub's website and also locally:
# Pull first to get GitHub changes
git pull
# If there's a conflict, resolve it (we'll cover this later)
# Then commit and push
git add .
git commit -m "Merge remote changes"
git push
Part 6: Viewing Remote Information
Check Your Remote
git remote -v
Output shows:
origin git@github.com:username/project.git (fetch)
origin git@github.com:username/project.git (push)
Add a Remote
git remote add origin https://github.com/username/project.git
Change Remote URL
git remote set-url origin git@github.com:username/project.git
Practice Exercise
- In your
git-practicerepository from last lesson:- Edit the README.md file
- Add a new file called
notes.txt
- Stage and commit your changes:
git add . git commit -m "Update README and add notes" - Push to GitHub:
git push - Go to GitHub and verify your changes appear
- Edit README.md directly on GitHub
- Pull the changes back to your local machine:
git pull
Summary
Key Commands
| Command | Purpose |
|---|---|
git push |
Upload commits to GitHub |
git pull |
Download and merge commits from GitHub |
git fetch |
Download commits without merging |
git remote -v |
View remote repository URLs |
Best Practice: Always pull before you push to avoid conflicts!
What's Next?
In the next lesson, you'll learn about branches - how to create parallel versions of your code to work on features without affecting the main codebase.