When you're working on a GitHub project from a fork, you need to make sure your branch stays updated with the latest changes from the original (upstream) repository.
Here's how to do that without overwriting your own work.
- Go to the GitHub repository provided by your instructor.
- Click "Fork" (top-right corner).
- Clone your fork to your computer:
git clone https://github.com/YOUR_USERNAME/REPO_NAME.git
cd REPO_NAMEThe upstream is the original repository you forked from (e.g., your instructor's or the class repo).
git remote add upstream https://github.com/NM-TAFE/civ-ipos-sessions.gitYou can check that both remotes are set correctly:
git remote -vExpected output:
origin https://github.com/YOUR_USERNAME/REPO_NAME.git (fetch)
origin https://github.com/YOUR_USERNAME/REPO_NAME.git (push)
upstream https://github.com/NM-TAFE/civ-ipos-sessions.git (fetch)
upstream https://github.com/NM-TAFE/civ-ipos-sessions.git (push)
To get the latest changes from the smesters branch branch of the original repo into your own branch, follow either merge or rebase instructions below.
This is safer and easier for beginners.
-
Switch to the branch you're working on:
git checkout your-branch-name
-
Fetch the latest changes from upstream:
git fetch upstream
-
Merge those changes into your branch:
git merge upstream/<BRANCH_NAME>
-
If you see conflicts, Git will guide you to resolve them. Open the conflicting files, fix them, then:
git add <file> git commit
This keeps your history cleaner, but should only be used before pushing your branch to GitHub.
-
Checkout your working branch:
git checkout your-branch-name
-
Fetch latest updates from upstream:
git fetch upstream
-
Rebase your branch onto upstream/main:
git rebase upstream/<BRANCH_NAME>
-
If there are conflicts:
- Fix them in the file(s)
- Then run:
git add <file> git rebase --continue
-
If you already pushed this branch to GitHub, you need to force push:
git push --force-with-lease
| Merge | Rebase |
|---|---|
| Easier, especially for beginners | Cleaner commit history |
| Creates a merge commit | Rewrites commit history |
| Keeps record of when you merged | Avoids extra merge commits |
| Safe for shared branches | Should only be done before pushing or with care |