Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrating local git repository into larger remote repository while rewriting paths

Tags:

git

repository

I have been happily using git on my machine for a project "MyProject". MyProject is actually part of a larger collaborative research effort "WholeThing". There is virtually no overlap between myProject and OtherProjects in the WholeThing.

Now, Big Bosses have decided to put all development onto a central git repository (for documentation and backup). Makes sense to me so far.

Assume I have no privileged access to the central repository.

The problem is, that MyProject maps into a sub-directory of the WholeThing, i.e.

local repo:
MyProject -|
           |- .git
           |
           |- dirs


remote repo:
WholeThing -|
            |- .git
            |
            |- Area1
            |
            |- Area2
                 |------ MyProject
                 |------ DudesProject

How do I get my local repository into the proper sub-directory on the central repository while preserving history and branches?

I (think I ;) understand remote branches and stuff, but I have not yet figured out a way to prefix the path of MyProject's file with "WholeThing/Area2" short of moving the files in my local repository and creating a commit before pushing. There surely must another way?

I also do not mind to clone the WholeThing and navigate down to MyProject once all the migration is done ... so I am not looking for submodules or such (and I don't have administrative access to the central repository).

Any ideas are welcome!

Cheers, Jose

like image 836
Jose Gracia Avatar asked Oct 14 '22 20:10

Jose Gracia


1 Answers

Thanks for the replies so far ... made me formulate the question differently on Google.

The answer is actually written down in the man page, you just need to read to the very end.

The solution is to rewrite the history with git filter-branch and rewriting the path of each file while doing so. Rewriting the path can in principle be done with --tree-filter (I think), or with --index-filter, which is much faster.

The code snippet below is take allmost verbatim from the man page git help filter-branch, just edited to reflect the directories in my example. It asks for each commit for the involved file's pathnames, rewrites them prefixing "WholeThing/Area2/MyProject/" and replays the commits.

git filter-branch --index-filter \
    'git ls-files -s | sed "s-\t-&WholeThing/Area2/MyProject/-" |
     GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
     git update-index --index-info &&
     mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' HEAD

The next (hopefully simple) step is to add the remote repository and push to it.

Cheers & thanks!

like image 169
Jose Gracia Avatar answered Oct 16 '22 23:10

Jose Gracia