问题
I have 2 branches A and B. Both have diverged from master with lots of different commits, and the master has moved ahead.
What i want to do is replace all changes of B with A. i.e., B should have exactly the same code as A.
I tried using rebase/merge. Even -Xtheirs while rebasing B, but always running into loads of conflicts.
i tried
git checkout B
git reset --hard A
as given in How to copy one branch to another regardless changes? but this gave this-
Your branch and 'origin/B' have diverged,
and have 6603 and 1823 different commits each, respectively.
Is there a way i can get the code/commits of A copied to B, with no conflicts? Please help.
回答1:
If you're really trying to discard all your changes from B, which seems like a waste, then you can force update the branch pointer.
git branch -f A B
回答2:
Your command to reset your local branch B
to A
was all right.
The message you get just informs you that your local branch B
is different from the remote branch B
.
If this is really what you want to do, just now do a force push of B
to origin/B
with git push -f origin B
if the remote allows force-pushes.
But be aware that you are rewriting published history and you will upset any other developers that based work on B
as they have to recover from this manually by rebases of their work.
回答3:
git checkout A -B B
Checkout the working tree of A
, while giving it the branch name B
. (Since B
already exists, -B
(big B) is required to override.)
This is effectively the same as this:
# Checkout the branch 'A' and make the working tree reflect this
git checkout A
# Create/override a branch 'B' at this current position
git checkout -B B
The end result is that the branches (which are practically just pointers) 'A' and 'B' point the the exact same commit.
N.B. The end result of this is pretty much the same as what you had with git checkout B && git reset --hard A
.
Now, since you seem to be concerned with the origin/B
has diversed message, there're two things you can do at this point:
You can update
origin/B
to reflect your newB
by force pushing: (This is provided you aren't worried that commits that were originally inorigin/B
will cease to exist.)push origin B --force
For some reason, you're unable to force push (Fast-forward only server?) Or you want to preserve the commit history that was under
B
--just not their changes. In this case, it's a little more involved: (This use case is quite rare so I'm not entirely certain this is what you're after):git checkout A --detach git merge -s ours B git checkout -B B
This makes a merging commit that proceeds
A
, that mergesB
intoA
without actually changing any files. This newB
can be pushed normally as it's not a divergence fromorigin/B
.
来源:https://stackoverflow.com/questions/38202717/git-overwriting-code-of-one-branch-to-another