Jordon Bedwell
Jordon Bedwell

Reputation: 3247

Git: merge master to new branch and clear out master

I was wondering if somebody could explain to me how to GIT merge master into a new branch and then clear out master as to have master clean working directory.

In other words: master -> PHP since master will now have Python.

Upvotes: 3

Views: 1257

Answers (2)

Dan Ray
Dan Ray

Reputation: 21903

"master" is just a name. It's the default, but there's nothing magical about it.

You could rename your current "master" branch to "PHP" with git branch -m master PHP

Then checkout whatever historic commit you're calling "clean" (use git log to figure that out), and then git branch master to create a master branch at that point.

Upvotes: 1

VonC
VonC

Reputation: 1328712

One solution would be, if nobody has cloned your repo yet, to:

  • make a PHP branch where master currently is
  • reset master to the Python tip

See:

git checkout master
git branch PHP
git checkout Python
git checkout -B master Python

From the checkout man page:

git checkout -b|-B <new_branch> [<start point>]

This form switches branches by updating the index, working tree, and HEAD to reflect the specified branch or commit.

If -B is given, <new_branch> is created if it doesn’t exist; otherwise, it is reset.

This is the transactional equivalent of

$ git branch -f <branch> [<start point>]
$ git checkout <branch>

But if others have cloned your repo, you need to reset the content of master, keeping all the previous commits.

That would be a good case for the non-existent merge -s theirs (where your discard your current content and replace it with the one of another branch, like Python in tour case):

See "git command for making one branch like another" for more.
(and start first by making the PHP branch where master currently is, like above: that bit doesn't change and will mark where PHP updates need to go from now on)

Upvotes: 3

Related Questions