1

I used the git subtree command on an existing repository, in order to extract a subfolder into its own repository. Then, I pushed the extracted commits to its new repository, into the master-branch.

However, I should have pushed it into the "feature/FT001"-branch all along and wanted to change everything accordingly:

  1. renamed the branch: git branch -m feature/FT001
  2. delete old branch and push new: git push origin master feature/FT001
  3. reset the upstream branch: git push origin -u feature/FT001

At this state, the master branch does not exist anymore locally and at origin.

I would like to add a new (and empty) master-branch now. It should look like I have never worked on master before (no commits). The history should look like there was a master branch, I then created the feature-branch and commited everything to the feature-branch (and will eventually merge my feature-branch into the master-branch).

How can I do these last steps?

Drimer
  • 93
  • 2
  • 14
  • 1
    You can't: there is no such thing as an "empty branch" in Git. A branch name is a pointer to a commit. There must be a commit for it to point to. (In other words, you must change your problem-statement requirements before you can solve your problem.) – torek Oct 20 '21 at 03:53
  • @torek: How could I deal with it then? Would I have needed to create an "initial commit" on my master-branch in the very beginning and then branched out of it? Is it still possible to do that afterwards, without messing with the history too much? – Drimer Oct 20 '21 at 03:58
  • That (an initial commit with, e.g., just a README) is one way, but it is not possible to do that with the tool you used. A simpler method is just to pick any existing commit in the renamed branch and call that commit "master". (The obvious choice is the latest commit.) Perhaps even better, simply don't bother with a `master` at all. – torek Oct 20 '21 at 04:01
  • Possibly related: [How to create a new (and empty!) "root" branch?](https://stackoverflow.com/questions/15034390/how-to-create-a-new-and-empty-root-branch) – Richard Smith Oct 20 '21 at 08:40

1 Answers1

3

I suppose your aim is to make it look like all those extracted commits were only in the feature/FT001 branch and the master branch was only initialized.

If you have Git 2.23, You can do this by creating an orphan branch:

git switch --orphan master

This will stage all your files, so then you will have to unstage them and make an empty commit to that orphan branch:

git reset
git commit --allow-empty -m "Initial commit"

If you have Git 2.28+ you can just do the switch and the reset on a single command:

git switch --discard-changes --orphan master
git commit --allow-empty -m "Initial commit"
Daniel Campos Olivares
  • 2,262
  • 1
  • 10
  • 17