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

  1. 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.

  2. Fetch the Latest Changes:

    • Run git fetch upstream.

📌 Creating a Branch in Your Fork

  1. Create a New Branch:

    • Run git checkout -b my-new-branch.
  2. Push It to Your Fork:

    • After making your changes and committing them, run git push origin my-new-branch.

📌 Merging Your Branch with the Original Repo

  1. 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).

  2. Push Your Changes:

    • Run git push origin my-new-branch.
  3. 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

  1. Fix the Conflicts:

    • Open the conflicted files and edit them to fix the issues.

    • Save the files.

  2. Finish the Merge:

    • Run git add [file_with_conflicts] and git commit.

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!