Git: overwriting code of one branch to another
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.
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.
3 Solutions collect form web for “Git: overwriting code of one branch to another”
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
Your command to reset your local branch
A was all right.
The message you get just informs you that your local branch
B is different from the remote branch
If this is really what you want to do, just now do a force push of
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.
git checkout A -B B
Checkout the working tree of
A, while giving it the branch name
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/Bto reflect your new
Bby force pushing: (This is provided you aren’t worried that commits that were originally in
origin/Bwill 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 merges
Awithout actually changing any files. This new
Bcan be pushed normally as it’s not a divergence from