In git, is there a simple way of introducing an unrelated branch to a repository?

Viewed 109125

While helping a friend with a git problem today, I had to introduce a branch that needed to be totally separate from the master branch. The contents of this branch really had a different origin from what had been developed on the master branch, but they were going to be merged into the master branch at a later time.

I remembered from reading John Wiegley's Git from the bottom up how branches are essentially a label to a commit that follows a certain convention and how a commit is tied to a tree of files and, optionally to parent commits. We went to create a parentless commit to the existing repository using git's plumbing:

So we got rid of all files in the index ...

$ git rm -rf .

... extracted directories and files from a tarball, added those to the index ...

$ git add .

... and created a tree object ...

$ git write-tree

(git-write-tree told us the sha1sum of the created tree object.)

Then, We committed the tree, without specifying parent commits...

$ echo "Imported project foo" | git commit-tree $TREE

(git-commit-tree told us the sha1sum of the created commit object.)

... and created a new branch that points to our newly created commit.

$ git update-ref refs/heads/other-branch $COMMIT

Finally, we returned to the master branch to continue work there.

$ git checkout -f master

This seems to have worked as planned. But this is clearly not the kind of procedure I would recommend to someone who is just getting started using git, to put it mildly. Is there an easier way of creating a new branch that is entirely unrelated to everything that has happened in the repository so far?

10 Answers

If your existing content was already committed, you now (Git 2.18 Q2 2018) can extract it into its own new orphan branch, since the implementation of "git rebase -i --root" has been updated to use the sequencer machinery more.

That sequencer is the one now allowing to transplant the whole topology of commit graph elsewhere.

See commit 8fa6eea, commit 9c85a1c, commit ebddf39, commit 21d0764, commit d87d48b, commit ba97aea (03 May 2018) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster -- in commit c5aa4bc, 30 May 2018)

sequencer: allow introducing new root commits

In the context of the new --rebase-merges mode, which was designed specifically to allow for changing the existing branch topology liberally, a user may want to extract commits into a completely fresh branch that starts with a newly-created root commit.

This is now possible by inserting the command reset [new root] before picking the commit that wants to become a root commit. Example:

reset [new root]
pick 012345 a commit that is about to become a root commit
pick 234567 this commit will have the previous one as parent

This does not conflict with other uses of the reset command because [new root] is not (part of) a valid ref name: both the opening bracket as well as the space are illegal in ref names.

Creating a separate repository worked well for me. Didn't have to worry about a <start_point>.

  1. Create an new empty repo somewhere else.
  2. Rename the master branch to anything other than master, ie gh-pages
  3. Start your orphan work.
  4. Commit your work to a the only branch in the new repository.
  5. Push the new branch to the same remote repo that contains the original master branch.

This method is mentioned on:

Related