I have two git branches, branch_a and branch_b, both diverged from branch master.
I have pushed branch_a. However, I want to reset all changes on branch_a and swap them for the changes on branch_b.
I can revert branch_a to the diverging point with reversion commits, and then merge branch_b, or rebase branch_a in branch_b. Is there a simpler way? Is there any way to say: "Create a commit that make the files in branch_a become exactly equal to branch_b"? I don't want to fix conflicts. branch_a is wrong, branch_b is right. But I cannot reset to branch_b, since I have already pushed branch_a.
If you don't want any changes from branch_a, then I don't think there's any other way than reverting everything in branch_a up to the diverging point and then merging branch_b into branch_a. Here's an easy way to do it, taken from this answer:
git checkout branch_b
git reset <the common ancestor commit>
# Move the branch pointer back to the previous HEAD.
git reset --soft HEAD@{1}
git commit -m "Revert to <the common ancestor commit>"
# Update working copy to reflect the new commit.
git reset --hard
It's still possible to reset branch_a to be the same as branch_b even when it's already pushed, but there are caveats. Basically if the branch is only used by you, it's pretty safe to do it, but if others are working on that branch too, you'd need to coordinate with them and the steps depend on your specific situation, so I can't really put a general answer here.
If you want to go that route, here's how to do it:
git checkout branch_a
git reset --hard branch_b
git push -f origin branch_a
Re: "Is there a simpler Way"
This doesn't reset as such. but you should be able to use the "ours" merge strategy to overwrite branch_a with branch_b like this:
git checkout branch_b
git merge -s ours branch_a
git checkout branch_a
git merge branch_b
The result should be your branch_a is now essentially branch_b.
I am a git novice, so caveat emptor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With