views:

555

answers:

3

Hi,

I have two branch in my git repo:

  1. master
  2. seotweaks (created originally from master)

I created "seotweaks" with the intention of quickly merging it back into master, however that was 3 months ago and the code in this branch is 13 versions ahead of "master", it has effectively become our working master branch as all the code in "master" is more or less obsolete now.

Very bad practice I know, lesson learnt.

Do you know how I can replace all of the contents of the "master" branch with those in "seotweaks"?

I could just delete everything in "master" and merge, but this does not feel like best practice.

+2  A: 

What about using git branch -m to rename the master branch to another one, then rename seotweaks branch to master?

ZelluX
Very nice solution!
Jason
Sorry, when I run this, the changes happen only locally. How can I get this to effect the remote branches? Thanks
Jason
@Jason: Try `git push -f origin master`
Koraktor
A: 

You can rename/remove master on remote, but this will be an issue if lots of people have based their work on the remote master branch and have pulled that branch in their local repo.
That might not be the case here since everyone seems to be working on seotweaks.

In that case you can:
(make a git remote --show to check how your remote is declared within your local repo. I will assume 'origin')

git branch -m master master-old  # rename master on local
git push origin :master          # delete master on remote
git push origin master-old       # create master-old on remote
git checkout -b master seotweaks # create a new local master on top of seotweaks
git push origin master           # create master on remote

But again:

  • if other users try to pull while master is deleted on remote, their pulls will fail ("no such ref on remote")
  • when master is recreated on remote, a pull will attempt to merge that new master on their local (now old) master: lots of conflicts. They actually need to reset --hard their local master to the remote/master branch they will fetch, and forget about their current master.
VonC
Thanks for the detailed response, when I run 'git push remote :master' I get an error - 'remote' does not appear to be a git repository.
Jason
@Jason: I changed that to 'origin' which might be the default name given to your remote repo.
VonC
+4  A: 

You should be able to use the "ours" merge strategy to overwrite master with seotweaks like this:

git checkout seotweaks
git merge -s ours master
git checkout master
git merge seotweaks

The result should be your master is now essentially seotweaks.

ergosys
This also marks a spot where you merged visually (gitk --all or git log --all --graph). Subsequent merges are going to involve commits from that point on.
adymitruk