How do I Re-root a git repo to a parent folder while preserving history?

前端 未结 8 1754
予麋鹿
予麋鹿 2020-11-29 23:12

I have a git repo in /foo/bar/baz with a large commit history and multiple branches.

I now want /foo/qux to be in the same repo as /f

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 23:44

    This adds to Walter Mundt's accepted answer. I would have rather commented on his answer, but I don't have the reputation.

    So Walter Mundt's method works great, but it works only for one branch at a time. And after the first branch, there may be warnings that require -f to force the action through. So to do this for all branches at once, simply add "-- --all" to the end:

    git filter-branch --commit-filter '
        tree="$1";
        shift;
        subtree=`echo -e 040000 tree $tree"\tsrc" | git mktree`
        git commit-tree $subtree "$@"' -- --all
    

    And to do this for specific branches, add their names to the end instead, although I can't imagine why you would change the directory structure of only some of the branches.

    Read more about this in the man page for git filter-branch. However, please notice the warning about possible difficulties pushing after using such a command. Just make sure you know what you're doing.

    I'd appreciate more input on any potential problems with this method.

提交回复
热议问题