41

I need to export a git repository content and import it in a target repository. I know of two alternatives to do it, but they do not solve my problem

a) Add the source repository as a remote and do a merge to the target. I cannot use this as both repositories are not on the same network

b) Use git archive and copy the contents to the target. In this git archive the revision info is lost.

I need something that does an archive along with the version history and enables me to merge in the target. How can this be done?

4 Answers 4

79

git bundle is provided for this exact purpose.

Example:

  1. Bundle your source repository in its entirety

    git bundle create my_repo.bundle --all

  2. Take ``my_repo.bundle'' to where your target is

  3. From your target, load up your source commit objects

    git bundle unbundle my_repo.bundle

    ...from which point you can merge/rebase/cherry-pick your desired source commits into your target repository to your heart's content.

    Otherwise, pulling works just the same:

    git pull my_repo.bundle

Compared to cloning, bundles are easier to transfer because they're smaller as your files aren't carried in their checked out state.

Other reasons for using bundle:

  • Unlike using clone --bare (which doesn't checkout your files), bundles are created and packed as a single file so you don't need additional compressing/tarballing.

  • You could also simply tarball your source repository (or just the .git directory), but you'll be carrying over all the excess cache and garbage that builds up under .git/.

4
  • 23
    You can also git clone my_repo.bundle. More info at git-scm.com/docs/git-bundle
    – jpbochi
    Mar 6, 2014 at 17:39
  • 2
    before unbundle, you need to have an existing target repository, or create a new repo by issue git init command, as otherwise you would get an error. Oct 14, 2016 at 7:23
  • 1
    @LinSongYang The new repo can also be created by cloning from the bundle the way the first comment describes.
    – antak
    Oct 14, 2016 at 7:33
  • I think I will try this for migrating from In house TFS 2013 to VSTS. Thanks!
    – JDPeckham
    Jul 31, 2017 at 14:31
2

Git clone the repository onto a USB stick, laptop, or some other data transfer medium. Hook that up to the other machine, add add the medium as a remote and pull/merge the changes from there...

Any clone from the repository actually contains all the history, so the question is really how can you get a clone to the other machine.

Also see: this question

2
  • So we need to clone and then use our own archiving mechanism because git archive skips the .git folder right?
    – codejammer
    Aug 9, 2012 at 15:53
  • Yes, basically git-archive can create a zip/tarball of a specific revision and/or part of the tree, but does not retain history. Aug 9, 2012 at 18:03
1

You can just zip the complete repository folder, then copy it to the destination workstation (via usb stick, etc.), and add that local repository as remote repository. Then you can fetch and merge.

1
  • 2
    FYI: This is often OK, but has one major drawback: you are transferring not only the data and history, but also all the settings like i.e. remotes urls or hooks or working directory username&email, which is something you don't want to do. Jun 6, 2017 at 10:54
0

You want to create a fork of your current repository it would seem. Let me know if I got this right: You wish to clone a particular git repository. Work on the clone and then merge the changes into the original repository?

This is done my creating a fork of your repository and then pulling the changes from the original source for commit changes.

3
  • The source repository is not directly accessible. (So cannot merge/clone) Is there any other delivery mechanism to move changes across two disconnected repositories?
    – codejammer
    Aug 9, 2012 at 9:41
  • If the source directory is not accessible how do you plan to copy any file from it?
    – darnir
    Aug 9, 2012 at 9:56
  • It has to be distributed without direct access
    – codejammer
    Aug 10, 2012 at 1:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.