Easy Steps to Work with Forked Repositories on GitHub
Working with forked repositories on GitHub might seem tricky, but it’s actually quite straightforward. This guide will walk you through the basic steps to sync your fork with the original repo, create new branches, and merge your changes.
📌 Getting Started: Sync Your Fork
Add the Original Repo as a Remote:
Open your terminal and go to your forked repo.
Run
git remote add upstream [original_repo_URL]
.Check with
git remote -v
to make sure it’s added.
Fetch the Latest Changes:
- Run
git fetch upstream
.
- Run
📌 Creating a Branch in Your Fork
Create a New Branch:
- Run
git checkout -b my-new-branch
.
- Run
Push It to Your Fork:
- After making your changes and committing them, run
git push origin my-new-branch
.
- After making your changes and committing them, run
📌 Merging Your Branch with the Original Repo
Update Your Branch:
Fetch the latest changes from the original repo:
git fetch upstream
.Merge them into your branch:
git merge upstream/main
(replace ‘main’ with the branch you want).
Push Your Changes:
- Run
git push origin my-new-branch
.
- Run
Create a Pull Request on GitHub:
Go to your fork on GitHub and click “New Pull Request”.
Select your branch and the branch from the original repo you want to merge into.
Click “Create Pull Request”.
📌 Dealing with Merge Conflicts
Fix the Conflicts:
Open the conflicted files and edit them to fix the issues.
Save the files.
Finish the Merge:
- Run
git add [file_with_conflicts]
andgit commit
.
- Run
Bonus:
Using git branch -vv
To check Your Branch Status
To see the status of all your branches (original and forked repo) and track which branches are set up to track remote branches, run
git branch -vv
.Look for the branch you are currently on (it will be marked with an asterisk) and check the information next to it. It will show you the remote branch it is tracking and the commit status.
Conclusion:
And that’s it! You've now learned how to sync your fork, create branches, make changes, and merge them back into the original repository. Remember, practice makes perfect, so keep trying, and you'll get the hang of it in no time!