开发者

Migrating local git repository into larger remote repository while rewriting paths

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


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!


yes the next step is nice and simple

git remote add other [path/to/repo]
git fetch other
git checkout -b other-master other/master
git checkout master
git merge other-master

and you should be good to go


Ideally you should your own sub project MyProject.git under Wholething/Area2.

If your bosses want to have only one git repository for every project, that doesn't make sense, but if they want it anyway, that would mean the WholeThing tree is a new tree totaly different from your and so , you can't do anything except from pulling the whole thing, add your directory and push everything (as well as the DudesProject) (you can NOT work on a subset or subdirectory of a git tree). Anyway as that would be 2 different trees you will lose all your history and stuff.

The proper way to do stuff is to have one git tree per project and so, so no .git under WholeThing. With the proper way "WholeThing/Area2" appears just naturaly in the url when you are adding the remote branch.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜