Dustin Silk
Dustin Silk

Reputation: 4620

SourceTree - make branch the new master

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 :/

Upvotes: 10

Views: 8133

Answers (4)

Gideon
Gideon

Reputation: 39

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)

Upvotes: 0

VonC
VonC

Reputation: 1327384

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)

Upvotes: 5

Яois
Яois

Reputation: 3858

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.

Upvotes: 8

Félix Cantournet
Félix Cantournet

Reputation: 2001

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.

Upvotes: -1

Related Questions