skip to content
Cabeza's Blog

How to Update a Forked Repository and Resolve Merge

/ 3 min read

Updating a Forked Repository

If you’ve forked a repository on GitHub and made changes to it, you may want to update your fork with the changes from the original repository without losing your own modifications. This is a common scenario, and Git makes it easy to accomplish this. In this post, we’ll explain how to update a forked repository without affecting your modifications and how to resolve merge conflicts.

To update a forked repository, follow these steps:

  1. Use the git remote -v command to check all the remote repository URLs in your local repository.

  2. Identify the upstream repository URL for the forked repository (i.e., the original repository from which you forked). You can usually find this on the forked repository’s GitHub page.

  3. Add the upstream repository URL to your local Git repository using the command git remote add upstream <upstream-repository-url>.

  4. Fetch the latest changes from the upstream repository using the command git fetch upstream.

  5. Switch to your local main branch, usually main or master, using the command git checkout main.

  6. Merge the upstream branch into your local main branch using the command git merge upstream/main (replace main with master if you’re using the master branch).

  7. Finally, push the changes to your forked remote repository using the command git push origin main (replace main with master if you’re using the master branch).

After following these steps, you will have merged the changes from the upstream repository into your forked repository while preserving your own modifications. Note that if there are any conflicts between the upstream and your modifications, you will need to resolve them manually.

Resolving Merge Conflicts

When you merge changes from the upstream repository and your own modifications, there may be conflicts that you’ll need to resolve manually. Here are the steps to resolve merge conflicts:

  1. Open the file that has conflicts and locate the conflicting parts. The conflicting parts will usually be marked in the file with special markers, such as <<<<<<<, =======, and >>>>>>>.

  2. Edit the file to resolve the conflicts as needed. You’ll need to decide how to merge the different changes from the two branches together. If you’re not sure how to resolve the conflicts, you can contact the project maintainers or look for documentation on resolving conflicts.

  3. Save the file and use the git add command to mark the file as resolved. For example, if you edited a file named example.txt, you can use the command git add example.txt.

  4. Repeat steps 1-3 until all conflicts are resolved.

  5. When all conflicts are resolved, use the git commit command to commit the merge. You can describe how you resolved the conflicts in the commit message. For example, if you resolved conflicts in a file named example.txt, you can use the command git commit -m "Resolve merge conflict in example.txt".

  6. Finally, push the committed changes to your forked remote repository using the command git push origin main (replace main with master if you’re using the master branch).

After following these steps, your branch will contain the changes from the upstream repository, your own modifications, and the merge conflicts that you resolved manually.