BrianH
BrianH

Reputation: 347

How to rebase entire master branch of git repo

I am pretty new to git, so I apologize if this is a simple solution.

I need to rebase the entire master branch of a repo to a feature branch. The repo was made, then someone used master as the first feature branch. So, what I have now is:

A -- B -- C -- D (branch: master)

and what I would like to do is

  A--B--C--D (branch: new-feature)
 /
Z (branch: master)

where Z represents a commit containing just a .gitignore file (or some other "repo-initializing commit" ... I'd even take a blank commit containing no files, but I don't think git likes/allows that).

I understand how to rebase commits within a branch, so I understand how to do this:

     C--D (branch: new-feature)
    /
A--B (branch: master)

but I have not been able to figure out how to rebase all of master onto a feature branch. I believe the crux of the issue I keep facing is that I don't have any commit to point master at, since there is no Z commit (commit A has partial feature implementation, so I can't use A in place of Z).

Thank you in advance!

Upvotes: 1

Views: 1161

Answers (1)

jthill
jthill

Reputation: 60275

Why bother rebasing? Just merge the new master base at the new feature tip. With master checked out (to provide a decent starting point)

git checkout -B feature
git branch -f master   $(git commit-tree -m 'Initial commit.' `:|git mktree`)
git merge -s ours --allow-unrelated-histories   master

producing

A ... D---E     feature
         /
        Z       master

and now history reflects exactly what happened, everything works.

If you really want to do the rebase, as the doc points out there's a --root option:

git checkout -B feature
git branch -f master   $(git commit-tree -m 'Initial commit.' `:|git mktree`)
git rebase --root --onto master

which will cause heartache if any branches off of the old master (the new feature) exist anywhere.

Upvotes: 3

Related Questions