Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to duplicate a branch into another existing branch in git

I would like to push all the changes in one branch to another branch (existing branch) without merging.

For the example, consider two branches branch1 to branch2. Both branch1 and branch2 track origin/branch1 and origin/branch2 respectively.

Branch1 has commits A,B,C,D,E,F Branch2 has commits A,B,D,F

I would like to make Branch2 exactly like branch1. Cherry-picking and merging would give conflict which i dont wanna spend time resolving, because all i am trying to do is, blindly replicating branch1 into branch2.

I am able to do this by

git checkout branch1 # Moves to branch1
git push origin :branch2 # Deletes remote branch origin/branch2
git branch -d branch2 # Deletes the local copy of this branch
git pull
git push origin HEAD:branch2 # Creates new branch in remote repository from the HEAD at local branch branch1

Is there a better way of doing this through some --force options in merge commands. I dont want to delete the branch everytime just to create a new branch with the same name.

Thanks

like image 930
akshitBhatia Avatar asked Sep 14 '15 09:09

akshitBhatia


2 Answers

git switch branch1  
git pull  
git push origin branch1:branch2 --force-with-lease  #assume the remote name is origin  
git branch -f branch2 origin/branch2  #reset the local branch branch2 
like image 104
Chuck Lu Avatar answered Nov 09 '22 12:11

Chuck Lu


git checkout branchA
git merge -X theirs branchB
like image 38
radiantRazor Avatar answered Nov 09 '22 14:11

radiantRazor