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:
-
Use the
git remote -v
command to check all the remote repository URLs in your local repository. -
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.
-
Add the upstream repository URL to your local Git repository using the command
git remote add upstream <upstream-repository-url>
. -
Fetch the latest changes from the upstream repository using the command
git fetch upstream
. -
Switch to your local main branch, usually
main
ormaster
, using the commandgit checkout main
. -
Merge the upstream branch into your local main branch using the command
git merge upstream/main
(replacemain
withmaster
if you’re using themaster
branch). -
Finally, push the changes to your forked remote repository using the command
git push origin main
(replacemain
withmaster
if you’re using themaster
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:
-
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>>>>>>>
. -
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.
-
Save the file and use the
git add
command to mark the file as resolved. For example, if you edited a file namedexample.txt
, you can use the commandgit add example.txt
. -
Repeat steps 1-3 until all conflicts are resolved.
-
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 namedexample.txt
, you can use the commandgit commit -m "Resolve merge conflict in example.txt"
. -
Finally, push the committed changes to your forked remote repository using the command
git push origin main
(replacemain
withmaster
if you’re using themaster
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.