Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

How can I make a set of commits the children of another commit in git?

Writer 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

  1. 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 commit abcdefghijkl. With that, your .git/info/grafts file should look like:

    1234567890 abcdefghijkl

    Note 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.)

  2. Use git log --stat --decorate to make sure you got it right. You should see a "(grafted)" decoration next to 1234567890.

  3. Finally, run git filter-branch abcdefghijkl..HEAD to "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/grafts as well.

    Note that this will change all commit IDs up to (and including) the grafted commit.

4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy