How to transplant branch from one repository to a new one without history?

I have an existing repository that has reached a certain storage cap.

However, now I want to transplant several branches from the old to a new repository, without the baggage of old history (since the git history is the culprit of that storage issue)

  • Delete all tags from a Git repository
  • How do I partially git cherry-pick without ruining a future merge?
  • Remove remote commit in git
  • git format-patch X..Y for a specific author
  • git: a fast web-interface for huge repos
  • White space issues with Automated DB configuration script
  • Is there any git command or sequence of commands that could help me do that?

    e.g

    cd old_repo/
    git checkout old_repo_old_branch
    
    git remote add origin new_repo
    git push origin new_repo_new_branch ?? // Does this preserve the history
    

    Edit: Thanks in advance, pardon me for my rudeness.

  • How can I undo a git rebase that's already been pulled/merged?
  • git hunk edit mode - how to remove a '-' line?
  • Time Tracking+Git: measuring effort per commit
  • Eclipse git and ssh-agent
  • How to use Git push/pull with source(text) files only?
  • p4merge displays error 'nul is (or points to) an invalid file' when I am using git difftool --cached
  • 3 Solutions collect form web for “How to transplant branch from one repository to a new one without history?”

    git push origin new_repo_new_branch ?? // Does this preserve the history

    Yes, new_repo_new_branch preserves the history.

    You can checkout a new branch with --orphan flag. This will erase all previous commit histories.

    $ cd old_repo/
    $ git checkout old_repo_old_branch
    
    $ git checkout --orphan new-branch       # checkout a new branch (say, new-branch) cleaning all previous commit histories
    $ git log                                # see commit history is empty
    
    $ git remote add newrepo <new-repo-url>  # add a new remote (say, newrepo) with new-repo-url
    $ git push -u newrepo HEAD               # push to newrepo/new-branch 
    

    You can do that with good old cp rather easy:

    # create a new repo
    git init new_repo
    
    # fetch the content from the old repo
    cd old_repo
    git archive --format=tar master > ../new_repo/data.tar
    
    # unpack the old content and...
    cd ../new_repo
    tar xf data.tar
    
    # add it to the new repo
    git add .
    git commit -m 'clean slate'
    

    Repeating for other branches, start in new_repo:

    git checkout --orphan branch-foo
    cd ../old-repo
    git archive --format=tar branch-foo > ../new_repo/data.tar
    # rinse and repeat like above
    

    I guess it depends exactly what you have in mind when you talk about leaving the history baggage behind, but I think the simplest solution might be a shallow clone.

    If you want, say, the most recent 5 commits on every branch, you could do

    git clone --depth=5 --no-single-branch url-for-old-repo
    

    (Note that the depth option will not work if you just give a local file path; you have to use a URL starting with file: instead if the origin is local.)

    This has some advantages over the approaches others have suggested. Your new repo contains the same commits (with their original SHA1 id) that were in the original repo, and your continued development therefore has a traceable connection to the old history even though it’s not in the new repo. (Of course, to actually track to the older history you have to look at the old repo, but at least it can be done.)

    If you need to pick and choose branches, or set different depth for different branches, or make sure that the depth is enough to capture branch points…. this all can be done, albeit with more work.

    Git Baby is a git and github fan, let's start git clone.