Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

atlassian sourcetree - How to move one repo to another repo folder and keep git history?

I want to move my project(e.g.project-A) to monorepo and keep whole git history. now when adding remote repository to new one with

git remote add A <remote-repo-project-A>

and pull project with

git pull project-A master --allow-unrelated-histories

source files are in folder monoreposrc instead of monorepoappsproject-Asrc

Basically I want to move repo-A to new repo with following structure:

repo-A
---- src

new repo
---- projects
---- ---repo-A(rename to project-A)
---- ------src
question from:https://stackoverflow.com/questions/65886670/how-to-move-one-repo-to-another-repo-folder-and-keep-git-history

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...