无写入权限时如何Rebase两个远程分支?代码移植同步技术问询
blessed/newserver Updated with blessed/develop (No Write Access Needed) Since you don’t have permission to push directly to blessed/newserver, you’ll need to handle the rebase locally and then propose the change via a Pull Request. Here’s the play-by-play:
1. Fetch all latest remote changes
First, make sure your local repo has the very latest commits from both the main team’s branches:
git fetch blessed
This pulls down the develop branch’s A-B-C commits and the newserver branch’s D-E commits, so you’re working with up-to-date code.
2. Switch to your forked newserver branch
If you already have a local copy of your fork’s newserver branch, switch to it:
git checkout newserver
If not, create it and track your remote fork’s branch:
git checkout -b newserver myfork/newserver
3. Rebase the official transplant branch onto blessed/develop
Your goal is to move the D-E commits from blessed/newserver to come after the A-B-C commits from blessed/develop. Run this rebase command to make that happen:
git rebase blessed/develop blessed/newserver
Or, if you’re already in your local newserver branch, you can simplify it to:
git rebase blessed/develop
Heads up: If you hit merge conflicts during the rebase, don’t panic! Just edit the conflicting files to resolve the issues, stage the fixes with
git add <conflicted-file>, then rungit rebase --continueto finish the process. Repeat until the rebase completes.
4. Force-push the rebased branch to your fork
Since rebasing rewrites commit history, you’ll need to force-push the updated branch to your own fork (this is safe here because it’s only your personal branch):
git push -f myfork newserver
5. Open a Pull Request to blessed/newserver
Head over to your Git platform (GitHub, GitLab, etc.) and create a Pull Request from your myfork/newserver branch to the official blessed/newserver branch. In the PR description, clearly explain that you’ve rebased the transplant branch onto the latest develop commits (A-B-C) to get the linear history A-B-C-D-E. The team can then review and merge your PR to update the official branch.
Alternative: Use Merge if Rebase Feels Too Risky
If you’d rather avoid rewriting history, you can merge blessed/develop into your local newserver branch instead:
git merge blessed/develop
Resolve any conflicts, commit the merge, push to your fork, and open a PR. This will create a merge commit, so the final history on blessed/newserver will look like A-B-C-[merge commit]-D-E. This is totally valid if the team doesn’t require strict linear history.
内容的提问来源于stack exchange,提问作者GuitarStrum

