This guide explains how to fork a repository, set the upstream, fetch changes, rebase updates, and push changes to your personal fork using GitHub and the Visual Studio Code terminal.
These instructions assume you’re the only one working in the fork. Steps may differ if the repository is shared with collaborators.
Prerequisites
- Git installed on your computer
- GitHub account
- Visual Studio Code
Terminologies
Local: Your copy of the repo on your computer.
Remote: A version of the repo hosted online (e.g., GitHub).
Origin: The default name for your fork's remote on GitHub.
Upstream: The original repo you forked from.
⭐ Step 1: Fork the Repository
- Go to the original repository on GitHub.
- Click Fork (top right) and select your account.
Deselect the option to copy only the main branch if you want to make a copy of all available branches.
You'll be taken to your page for your newly created fork.
⭐ Step 2: Clone Your Fork Locally
- On the page for your newly created fork, navigate to <> Code and copy the web URL.
- In your VSCode terminal, move into the folder you want the repository to be based in. You can do this one of two ways:
- Clicking File -> Open Folder -> Select the folder
- Use the command
cd
to navigate the directory of a folder that's already open. Read more aboutcd
here -> cd in Git
- Once you're in the right folder, use the command
git clone
and paste the URL. -
cd
into the newly created repo folder.
⭐ Step 3: Create a New Branch
⚠️ Before creating a new branch, make sure you're on main. Always do your work in a branch other than main (whether it's for features, experiments, or practice) so you keep main clean and conflict-free.
-
git branch
to check which branch you're on.- It will list all your local branches, and the one you’re currently on will be marked with an asterisk *.
- It will list all your local branches, and the one you’re currently on will be marked with an asterisk *.
-
git checkout -b new-branch-name
creates and moves to your new branch.
⭐ Step 4: Check and Add Upstream Remote
-
git remote -v
to list the current remotes associated with the local repository. (On a new fork, you'll only see origin) -
git remote add upstream https://github.com/ORIGINAL-OWNER/REPO-NAME.git
adds the upstream. - Verify with
git remote -v
.
Remember: Upstream is the original repository you forked from. Adding an upstream lets you pull in updates from the source project to keep your copy up to date.
⭐ Step 5: Work on Your New Branch
⚠️ Make sure you switch to your new branch so your changes don't touch main.
Now you can edit files, using these commands as you go:
- Check changed files:
git status
- Stage files
- Stage one file:
git add insert-file-name
- Stage all changed files:
git add .
- Stage one file:
- Commit staged changes:
git commit -m "Your commit message"
⭐ Step 6: Fetch Updates from Upstream
- If you have any uncommitted changes, stage and commit them.
- Switch to your main branch.
-
git fetch upstream
to fetch the latest changes from the original repo.
NOTE: This fetches changes for all branches, not just main.
⭐ Step 7: Sync Upstream Changes with Local Main
-
git merge upstream/main
.
This updates your main branch with the latest changes from the original repo (upstream). (If you want to sync other branches, you need to check out each branch first and merge it following the same steps.)
⭐ Step 8: Bring Changes from main to Your Branch
This ensures your branch has the latest changes you synced with main in Step 7.
git checkout example-branch
-
git merge main
- A file will open where you can edit the merge commit message.
- Closing the file confirms the merge.
If there are conflicts, there are a few options you can read about here -> How to Resolve Merge Conflicts in Git
⭐ Step 9: Push Updates on Your Local Branch to Your Fork on GitHub
⚠️ Make sure all your changes are staged and committed before pushing.
git checkout example-branch
-
git push origin new-branch-name
pushes your local branch to your GitHub fork (origin).
⭐ Step 10: Update Your Fork's main on GitHub
git checkout main
git push origin main
This pushes your local main to your fork's main on GitHub.
Top comments (0)