12

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

akshitBhatia
  • 1,131
  • 5
  • 12
  • 20
  • 3
    possible duplicate of [How to reset a branch to another branch with git?](http://stackoverflow.com/questions/15943434/how-to-reset-a-branch-to-another-branch-with-git) – Joe Sep 14 '15 at 09:44
  • but if your commits resolve in conflict, then why on earth would you want to blindly dump everything in? – DevDonkey Sep 14 '15 at 09:45
  • 2
    Wouldn’t resetting like `git checkout branch2` `git reset --hard branch1` be an option for you? – Melebius Sep 14 '15 at 09:45

3 Answers3

2
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 
Chuck Lu
  • 173
  • 12
1
git checkout branchA
git merge -X theirs branchB
radiantRazor
  • 477
  • 3
  • 13
  • -X is only the strategy-option. The default strategy recursive uses the strategy-option ours only for conflicts. See git-merge help. – Weidenrinde Nov 10 '16 at 12:10
  • A list of possible solutions: http://stackoverflow.com/questions/4911794/git-command-for-making-one-branch-like-another – Weidenrinde Jan 30 '17 at 13:36
1

Start on the branch you would like to clean, branch2 in your case. Then, rebase on that branch and drop all the commits that are unique to that branch.

branch2 should now be in the same state is it was when you first created it. Now to 'clone' all the commits from branch1, just rebase branch2 onto branch1