32

I have a branch with actual sources and I did not make any commits for a long time to master, and at the moment it's completely out of date. I want to just replace master's content with the content of my branch. One way to do it is to checkout both branch and master, delete master's content and copy content from branch to master, and after that push result to master.

It works, but I believe there has to be some git command to do it in a simpler way.

Does anybody know how to do it?

michaelmichael
  • 13,755
  • 7
  • 54
  • 60
Rostyslav Druzhchenko
  • 3,673
  • 3
  • 33
  • 38
  • How do you know there's nothing of value left on master? You could check with 'git log branch..master' to see if there are any commits on master not on branch. If there are any, then 'git checkout master; git merge branch' would include the changes in those commits. – Kent Apr 07 '12 at 14:04

1 Answers1

59

You can use the following command to have master point to a new location:
git branch -f master branchToMoveMasterTo

What this is actually doing is creating a new branch called master that points to branchToMoveMasterTo. Since we already have a branch called master, we need the -f flag to say we want to delete the original master

Andy
  • 44,610
  • 13
  • 70
  • 69
  • This works fine. But is there any way to preserve the history of the old master? I mean, how can it look like an ordinary commit, but changes everything? – Kun Wu May 06 '15 at 07:54
  • If you want to preserve the old master you'll need to create a branch that points to the same SHA before you move the master to its new location. Git branches are just pointers; there is no history attached to them. – Matt May 12 '15 at 16:38
  • 1
    See also http://stackoverflow.com/a/29559362/1741542. The current branch must not be `master` (or whatever branch you want to move) – Olaf Dietsche Jan 28 '16 at 04:46