Skip to content

Instantly share code, notes, and snippets.

@trepichio
Last active October 14, 2023 08:07
Show Gist options
  • Save trepichio/00fb7fa59b62cc393c19c5b226961ca1 to your computer and use it in GitHub Desktop.
Save trepichio/00fb7fa59b62cc393c19c5b226961ca1 to your computer and use it in GitHub Desktop.
Transfer a partial history from a git branch to a new repository

Transfer a partial history from a Git Branch to a new Repository

Steps to reproduce:

  1. In your current repository, create a new orphan branch, containing the contents of the first commit you want to copy:

      $ git checkout --orphan -b tmpbranch <hash_of_first_commit>
      $ git commit -a -m 'first commit'
  2. Cherry-pick the rest of the commits you want:

        $ git cherry-pick <hash_of_first_commit>..mybranch
  3. Push this new orphaned branch to your new repository, as the new master branch:

      $ git push <URL_of_new_repo> refs/heads/tmpbranch:refs/heads/master
  4. Delete the tmpbranch so garbage collection will eventually clean up for you:

      $ git checkout mybranch
      $ git branch -D tmpbranch
@konradtoenz
Copy link

Must be

$ git checkout --orphan tmpbranch <hash_of_first_commit>

(the -b isn't needed and doesn't work).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment