Phase 4GitHub

#14 Collaborating

fetch, pull requests, rebase

git fetch -- Download without merging

The git fetch command downloads the latest changes from the remote repository, but without merging them into your local branch. This lets you see what has changed before deciding what to do:

Download changes
$ git fetch
remote: Enumerating objects: 3, done.
remote: Total 3 (delta 1), reused 3 (delta 1)
From https://github.com/alice/my-project
   a1b2c3d..d4e5f6g  main -> origin/main

After a git fetch, the changes are in origin/main but your local main branch has not changed.

fetch + merge = pull

To integrate the downloaded changes, use git merge:

Merge after fetch
$ git fetch
$ git merge origin/main
Updating a1b2c3d..d4e5f6g
Fast-forward
 README.md | 5 +++++
 1 file changed, 5 insertions(+)

In fact, git pull does exactly these two steps in a single command. Using git fetch separately is useful when you want to inspect the changes before merging them.

Pull Requests (PR)

A Pull Request (or PR) is a GitHub feature that lets you propose changes to a project. The typical workflow is as follows:

Pull Request workflow
# 1. Create a branch for your work
$ git checkout -b feature

# 2. Make commits
$ git add .
$ git commit -m "Add feature X"

# 3. Push the branch to GitHub
$ git push -u origin feature

# 4. Create the PR on GitHub (or with gh pr create)
# 5. Code review by other developers
# 6. Merge the PR into main

PRs enable code review: your colleagues can comment, suggest changes, and approve before the code is merged into the main branch.

git rebase -- Replay your commits

The git rebase main command takes your commits and replays them on top of the main branch. This produces a linear and cleaner history:

Rebase a branch
$ git rebase main
Successfully rebased and updated refs/heads/feature.
First, rewinding head to replay your work on top of it...
Applying: add feature X

After the rebase, your branch appears to start from the latest commit on main:

History after rebase
$ git log --oneline
d4e5f6g (HEAD -> feature) add feature X
a1b2c3d (origin/main, main) Initial commit

Rebase vs Merge

Both allow you to integrate changes, but with different approaches:

  • Merge: creates a merge commit. Safer, preserves the complete history as-is.
  • Rebase: rewrites the history to make it linear. Cleaner, but should be used with caution on shared branches.

Simple rule: use rebase to update your local working branch, and merge to integrate into main.

Summary

Overview
git fetch                  # Download without merging
git merge origin/main      # Merge remote changes
git pull                   # fetch + merge in one command
git rebase main            # Replay your commits on main

# PR workflow:
# branch → push → create PR → review → merge

Your turn

Try the collaboration commands in the terminal below. Start with git fetch to download remote changes.

terminal — bash
user@stemlegacy:~/my-project$