Finding myself in a position where I was decommissioning an old project and starting afresh, there was a file that I wanted to copy to my new project and, ideally, retain all of the valuable version history.
After searching, I found reams of git commands piped together that may have solved my problem. They tended to be working backwards, making me start with the full repository and then using git filter-branch to filter down to the file or directory that I needed, it just didn’t seem right. The tutorials were also lengthy and used multiple git commands in sequence that I couldn’t follow so I wasn’t comfortable using.
What I actually wanted was very simple. I wanted all patches for a single file and to apply them to a new repository. I found a post explaining how to use git format patch.
I couldn’t quite get this line working for me. It was not including a patch for the root commit:
In the end, I found that this was sufficient:
This left me with a directory of patches, which, after initialising a new git repository, I could then apply in bulk with:
Now, all being well that would have been problem solved. However, somehow the git history on the file was messy. Without going into details, it meant that the patches produced didn’t make sense and it needed manual resolution.
After recovering from that fear you get when something goes wrong in git, I retaliated and was able to apply the patches manually with the help of this post.
And there we are. One new repository with a brand new old file.