10

Im using GIT and Sourcetree as the gui.

I've got two branches - master and feature-01. I now want to overwrite everything on master with whats on feature-01.

I had to do this after having to roll back a few changes on master. however i rolled back the changes on the new branch, got the branch to be 100% where i want master to be but now when i merge the two - master is keeping all the rolled back changes that it should have discarded like in the branch :/

4 Answers 4

8

You can do the following:

  1. From your feature-01 branch: git merge --strategy=ours master
  2. Then from your master branch: git merge feature-01

The first command will create a new (merge) commit from the two branches, but ignoring all what's in master by using the --strategy=ours option. Then, the second command will "advance" master to this new commit.

At this point you will have your feature contents in master. With this option you won't need to "reset" the remote or anything similar; it will just put into master all the work you did in feature.

2
  • 6
    How to do this in SourceTree is the question.
    – Yeats
    Commented Jul 19, 2016 at 16:51
  • Wouldn't git merge --strategy=theirs feature-01 from the master branch do the same exact thing as those two commands?
    – tumelo
    Commented Nov 26, 2018 at 22:54
5
+50

Is this possible in SourceTree?

No, it is not directly possible, because a merge --ours is not supported in Sourcetree.
(I have tested it with the latest 2.1.11.0, with Git 2.14.1 embedded, Sept. 2017).

See SRCTREEWIN-1237 (... from 2013!)

I need to use the ours strategy i.e. merge myBranch into master, discarding any changes on myBranch

git merge -s ours myBranch

This is still not available when merging two branches:

merge options


As mentioned in this article, you would need to define a custom action:

custom action

That will allow you to emulate the merge --ours step of the following sequence

git checkout feature-01
git merge --strategy=ours master
git checkout master
git merge feature-01

The checkout and simple merge are supported in SourceTree. The custom action allows you to make the merge --ours step.
And then, master content will be replaced by feature-01 content.
Entirely from SourceTree alone.


For mercurial, following "Replace the content of a hg branch", you would need to create a custom action (as illustrated above), for the command hg branch -f (the -f option is not available in the normal branch dialog)

3
  • Thank you. @VonC . I just realized that I am using Mecurial for source control. Is it possible with that as well?
    – Kirsten
    Commented Sep 11, 2017 at 3:04
  • Also I am confused about discarding changes to mybranch. Why would I want to do that. I want to replace master with mybranch like the question asks.
    – Kirsten
    Commented Sep 11, 2017 at 3:22
  • 1
    @kirsteng I have updated the answer for mercurial (even though the all page was about Git ;) ). Don't worry about the discard part: the end result is replacing one branch by another. as requested.
    – VonC
    Commented Sep 11, 2017 at 12:07
0

You can do this in SourceTree 3.0

Select feature-01 branch then pop up a terminal window

SourceTree Terminal Window

Type in the command git merge --strategy=ours master and hit return. Terminal will prompt for a comment to go with the commit. It is a Unix Vim editor so to save changes need to Press Escape key then :x and Enter to save the changes

Close the terminal

Select master branch

Open another terminal window and type git merge feature-01

(There is probably a way to swap branches from within the terminal window but this worked for me)

-1

You can delete the master branch (it's just a name tagged to a commit), then recreate it where you want it :

git branch -d master   // this will delete the branch
git checkout feature-01
git branch master   // this will recreate it where you are checked out.

Be careful if you have shared this repository, you have to clean the remote, AND you should probably talk to whoever uses the repository / is in charge of the the repository, because you will be re-writing history.

1
  • This might be OK if feature-01 and master otherwise share the same history.
    – chepner
    Commented Apr 13, 2015 at 21:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.