Merge histories of two files from same git repository

Viewed 62

I have two files, ./asm.py and dir/asm.py.

A -> B -> C -> D -> master

I worked on ./asm.py for a couple of commits, let's say up to commit C. For reasons I don't know, at commit C I added a new file - dir/asm.py, which is exactly the same as ./asm.pyat that point. Further commits C->D->master are all upon dir/asm.py.

Obviously, histories of these two files are not connected. ./asm.py is developed for A->B and dir/asm.py is developed in C->D->master. I'm wondering if I can somehow combine those two so I get A->B->C->D->master when I look at the history of dir/asm.py, and then remove ./asm.py

1 Answers

One way to accomplish this is to insert a new commit between B and C that does a move of the file. Then cherry-pick the rest of the commits in order from C through master.

It may be easier to try it out on a new temporary branch:

git switch -c temp-branch B # where B is the commit ID
# now move the file manually into the dir folder, and commit it
# When done, cherry-pick the remaining commit range:
git cherry-pick C..master
# you'll likely get a conflict here saying the file was deleted- accept the delete
# To continue the cherry-pick:
git cherry-pick --continue
# it should finish without additional conflicts
# Look at the history of the file and make sure you're happy with it
# Rename your branch to master
git branch -M master

Note, the reason you can do this is because you didn't modify the file after you created a copy of it.

Related