bob
bob

Reputation: 75

Git bundle - import back into repo

A while ago I needed to move git managed code to a separate disconnected network. I was unaware of git bundle at the time and so just created a new git repo on the new host and copied the files. They were committed as an initial commit, and all development continued on the new repo.

Due to 'altered' requirements, I need to bring back the newer code to the old repo. Is it possible to use git bundle to do this to preserve all commit history? There has been no further commits to the old repo.

Thanks for any clues. Bob

Upvotes: 1

Views: 1254

Answers (2)

LeGEC
LeGEC

Reputation: 51988

I will separate the two following parts :

  1. bring the history of the old repo in your current repo
  2. stitch together your current history with the old history

  1. bring in the history

You can use git bundle but copying the repository (the crucial part is to copy the .git/ directory) is actually enough.
In both cases, once the bundle or the repository is copied next to your current repo, you can use the bundle/the copy as a remote :

# using git bundle :

# from the old repo :
git bundle create ../oldrepo.bundle --all
cp ../oldrepo.bundle /mnt/thumbdrive

# on the new repo :
cp /mnt/thumbdrive/oldrepo.bundle ~/
git remote add old ~/oldrepo.bundle

# copying the repo :

# from the old repo :
cp -r .git/ /mnt/thumbdrive/old.git 

# on the new repo :
mkdir ~/oldrepo
cp -r /mnt/thumbdrive/old.git ~/oldrepo/.git

git remote add old ~/oldrepo

You now have a remote named old, which points at the old content,
run git fetch old and you will have a complete copy of the old history in your new repository :

$ git branch -r
old/master
old/feature/one
old/feature/two
...

  1. stitch the histories together

If your new history has an identified commit which matches the exact content of a known old commit :

$ git log --oneline --graph old/master
* eacf32 (old/master) <that commit when we migrated>
* dbde41 other commit
...


$ git log --oneline --graph master
* eaed50 (HEAD -> master) current head commit
...
* 57a7aa <that commit when we migrated>

you can simply use git rebase :

# rebase on top of old/master all changes that came after 57a7aa :
git rebase --onto old/master 57a7aa master

Check that the resulting history is what you expect ; if you had already shared your master branch, you will need to force push your updated master to update.

Upvotes: 1

Marco Luzzara
Marco Luzzara

Reputation: 6046

You cannot use git bundle because:

As no direct connection between the repositories exists, the user must specify a basis for the bundle that is held by the destination repository: the bundle assumes that all objects in the basis are already in the destination repository.

From git bundle documentation


In your case the new repository does not share anything with the old one, except for the working directory, which is not enough. In summary the last commit of the old repo should have the same hash of the first commit in the new repo.

You have another way, git format-patch and git am:

  1. First you need to create a patch for each commit (except the first one) in the new repository:

    git format-patch -o new_commits {first_commit_hash}..HEAD
    

    This is going to create a new folder new_commits containing a bunch of .patch files. Each .patch file has some meta-data, like the author and the date of commit, and the diff from the previous commit.

  2. Make the new_commits folder accessible to the old repo and, in the old repo, run:

    git am {path_of_new_commits_folder}/*
    

Now you should see all the patches of the new commits applied to the old repo.

Upvotes: 1

Related Questions