How to move one repo to another repo folder and keep git history?
Solution 1:
History, in Git, is simply the commits. The commits are the history. So the literal answer to the question that you didn't quite ask—"can I change the contents of the commits without changing the commits' contents"—is of course no. More importantly, no commit can ever be changed, by anyone or anything, because the commits are numbered, and the numbers are cryptographic checksums of their contents. So if you change anything about a commit, that changes the number.
All is not, however, lost. You can copy the commits to new-and-improved commits, whose only difference from the original commits is ... well, let's take one commit as an example. The original commit says the files are named src/file1
, src/file2
, etc, and here are their contents: ... The commit was made by ____ on ____ and its parents are ... The new-and-improved commit says the files are named projects/A/src/file1
, projects/A/src/file2
, etc., and here are their contents ... The commit was made by (same person) on (same date) and its parents are (the new-and-improved copies of the original commit's parents).
This copying process is tedious but straightforward, and it can be done by git filter-branch
, git filter-repo
, and various other tools as shown in git move directory to another repository while keeping the history (among many other very similar questions). Note that if you have other additional repositories whose commits—or rather, new-and-improved copies of commits—you'd like to have in this repository, you must decide whether to convert each set of commits one at a time, then try to make combining (merge) commits to combine them, or to write a much fancier filter that attempts to combine all the other repositories' commits at once. That is, one new-and-improved commit might represent two or more "original" commits from other repositories. This is a much more difficult proposition and the existing filter programs that do the simple straightforward thing do not do this for you.