How to add a parent commit before all other commits in Git

git

I am in a situation where I am trying to convert an open source project to Git, and I have recently gained access to historical data for the project. I have already made modifications to the repo, though, so I want to add these early changes as git commits to the beginning of the tree of commits in Git. (Yes, I am aware that this will change the SHAs for later commits; this is acceptable.) The data is provided as successive snapshots of the working directory. I want to set it up so that the state of the working directory for the later commits is not affected (I don't want to merge the changes in).

For example, if initial commit B adds files a and b to the working directory, and my historical commit A adds file a, I want to make a new commit B' parented from A that adds file b only. In both B and B', the working directory looks the same, and any subsequent commits on top of B can be safely rebased onto B'.

Is it possible to do this in Git? If so, how?

Edit: Note that I need to modify the initial commit. The standard usage of git commit appends a new commit as a child of the HEAD commit, and so does not work for the initial commit, which has no parent.

Best Answer

Something like this should work.

# Create a new branch with the old history
$ git checkout --orphan old-history    
$ git add <old-files>
$ git commit

# Rebase master on top of the branch with old-history
$ git checkout master
$ git pull --rebase . old-history
Related Question