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

前端 未结 9 2068
被撕碎了的回忆
被撕碎了的回忆 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 06:51

    From Git Community Book:

    git symbolic-ref HEAD refs/heads/newbranch 
    rm .git/index 
    git clean -fdx 
    <do work> 
    git add your files 
    git commit -m 'Initial commit'
    
    0 讨论(0)
  • 2020-11-22 06:51

    The currently selected answer is correct, I would just add that coincidentally...

    This is actually exactly how github.com lets users create Github Pages for their repos, thru an orphaned branch called gh-pages. The pretty steps are given and explained here:

    https://help.github.com/articles/creating-project-pages-manually

    Basically the git commands to set this up are as follows:

    1. git checkout --orphan gh-pages (create a parentless branch called gh-pages on your repo)
    2. git rm -rf . (removes any files from branch working tree)
    3. rm '.gitignore' (even the gitignore)
    4. Now add website content (add index.html, etc) and commit and push.
    5. Profit.

    Note you can also designate a /docs folder on you repo to be the source of the "Project Site" that Github uses to build the website.

    Hope this helps!

    0 讨论(0)
  • 2020-11-22 06:56

    Sometimes I just want to create empty branch in project instantly then start doing work, I will just excute following command :

    git checkout --orphan unrelated.branch.name
    git rm --cached -r .
    echo "init unrelated branch" > README.md
    git add README.md
    git commit -m "init unrelated branch"
    
    0 讨论(0)
  • 2020-11-22 07:01

    Github has a feature called Project Pages where you can create a particular named branch in your project to provide files that will be served by Github. Their instructions are as follows:

    $ cd /path/to/fancypants
    $ git symbolic-ref HEAD refs/heads/gh-pages
    $ rm .git/index
    $ git clean -fdx
    

    From there you have an empty repository which you can then add your new content to.

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

    There is a new feature (since V1.7.2) which makes this task a little more high-level than what's in any of the other answers.

    git checkout now supports the --orphan option. From the man page:

    git checkout [-q] [-f] [-m] --orphan <new_branch> [<start_point>]

    Create a new orphan branch, named <new_branch>, started from <start_point> and switch to it. The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits.

    This doesn't do exactly what the asker wanted, because it populates the index and the working tree from <start_point> (since this is, after all, a checkout command). The only other action necessary is to remove any unwanted items from the working tree and index. Unfortunately, git reset --hard doesn't work, but git rm -rf . can be used instead (I believe this is equivalent to rm .git/index; git clean -fdx given in other answers).


    In summary:

    git checkout --orphan newbranch
    git rm -rf .
    <do work>
    git add your files
    git commit -m 'Initial commit'
    

    I left <start_point> unspecified because it defaults to HEAD, and we don't really care anyway. This sequence does essentially the same thing as the command sequence in Artem's answer, just without resorting to scary plumbing commands.

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

    Found this script at http://wingolog.org/archives/2008/10/14/merging-in-unrelated-git-branches and it works very fine !

    #!/bin/bash
    
    set -e
    
    if test -z "$2" -o -n "$3"; then
        echo "usage: $0 REPO BRANCHNAME" >&2
        exit 1
    fi
    
    repo=$1
    branch=$2
    
    git fetch "$repo" "$branch"
    
    head=$(git rev-parse HEAD)
    fetched=$(git rev-parse FETCH_HEAD)
    headref=$(git rev-parse --symbolic-full-name HEAD)
    
    git checkout $fetched .
    
    tree=$(git write-tree)
    
    newhead=$(echo "merged in branch '$branch' from $repo" | git commit-tree $tree -p $head -p $fetched)
    git update-ref $headref $newhead $head
    git reset --hard $headref
    
    0 讨论(0)
提交回复
热议问题