How can I make a set of commits the children of another commit in git?
Emily Wong
I have a repo I inherited, it was created by downloading a zip of code from github not cloned, and almost all changes were made before the first commit.
I found which commit it was created at (I think) and after cloning the github repo I made a branch off that commit. I also added the incomplete repo as a remote to the complete repo cloned from github.
Now I'd like to take the commits from the incomplete repo and rebase? them onto the branch in the complete repo, preferably while maintaining previous ownership of the commits.
1 Answer
First temporarily "graft" two histories together via
.git/info/grafts. This adds fake parents to any given commit. The syntax is:commit-id parent-id [parent-id...]Let's say your oldest commit is
1234567890, and you think it was based on GitHub repo's commitabcdefghijkl. With that, your.git/info/graftsfile should look like:1234567890 abcdefghijklNote that you must use the full 20-character commit IDs, not the shortened ones.
(The same can also be achieved using "replaced objects" and
git replace --graft. The mechanism is different, but the end result is the same.)Use
git log --stat --decorateto make sure you got it right. You should see a "(grafted)" decoration next to1234567890.Finally, run
git filter-branch abcdefghijkl..HEADto "bake in" the graft and make it permanent. After this, the log should look identical, but "(grafted)" marker should have disappeared. You can delete.git/info/graftsas well.Note that this will change all commit IDs up to (and including) the grafted commit.