When you have a fork (Repository B) based on an open-source project (Repository A), and you want to incorporate new features from A without overwriting your custom modifications in B, follow this workflow. This process uses a temporary merge branch to isolate conflict resolution before updating the main branch of the fork.
# 1. Add the original repository A as a remote named 'upstream'
cd path/to/B
git remote add upstream path/to/A
# 2. Fetch the latest code from the upstream repository
git fetch upstream
# 3. Create a new temporary branch for the merge operation
git checkout -b merge-upstream
# 4. Merge the upstream main branch into the temporary branch
# If the upstream branch is not 'main', replace it with the correct branch name
git merge upstream/main
# NOTE: If you see 'fatal: refusing to merge unrelated histories', use:
git merge upstream/main --allow-unrelated-histories
# 5. Resolve any merge conflicts manually, then stage and commit
git add .
git commit
# 6. Switch back to the main branch of Repository B and merge the temporary branch
git checkout main
git merge merge-upstream
# 7. Resolve any remaining conflicts and push the final result to Repository B
git push origin main