Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a branch equal to another branch

Tags:

git

git-branch

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.

like image 992
Luís Guilherme Avatar asked Feb 26 '14 19:02

Luís Guilherme


2 Answers

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
like image 177
htanata Avatar answered Oct 12 '22 02:10

htanata


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

like image 45
Neil Thompson Avatar answered Oct 12 '22 04:10

Neil Thompson