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

前端 未结 9 2079
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 06:11

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 bra

相关标签:
9条回答
  • 2020-11-22 07:09

    Although the solution with git symbolic-ref and removing index works, it might be conceptually cleaner to create new repository

    $ cd /path/to/unrelated
    $ git init
    [edit and add files]
    $ git add .
    $ git commit -m "Initial commit of unrelated"
    [master (root-commit) 2a665f6] Initial commit of unrelated
     1 files changed, 1 insertions(+), 0 deletions(-)
     create mode 100644 foo
    

    then fetch from it

    $ cd /path/to/repo
    $ git fetch /path/to/unrelated master:unrelated-branch
    warning: no common commits
    remote: Counting objects: 3, done.
    Unpacking objects: 100% (3/3), done.
    remote: Total 3 (delta 0), reused 0 (delta 0)
    From /path/to/unrelated
     * [new branch]      master     -> unrelated-branch
    

    Now you can delete /path/to/unrelated

    0 讨论(0)
  • 2020-11-22 07:11

    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.

    0 讨论(0)
  • 2020-11-22 07:16

    In recent Git versions, 2.27 at least, this can be cleanly achieved with the switchcommand:

    git switch --orphan <new-branch>
    

    Official documentation: https://www.git-scm.com/docs/git-switch

    0 讨论(0)
提交回复
热议问题