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?

1
  • 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. Commented Apr 7, 2012 at 14:04

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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.
See also stackoverflow.com/a/29559362/1741542. The current branch must not be master (or whatever branch you want to move)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.