I am having a problem with my repository setup: I initialized my repository in the wrong directory and started using it. I already made some branches and merged them again. I would like to keep that history.
How it is now:
wrongRootProjectDir/
|
+-.git
|
+-realProjectDir
|
+-<Project files>
How it should look like:
realProjectDir
|
+-.git
|
+-<Project files>
So basically I would like to shift everything one directory up. wrongRootProjectDir
also contains only the .git
file and the realProjectDir
directory.
I found some hints at SO How can I move a directory in a Git repo for all commits? that there could be a way. I run a git clone oldrepo newrepo
to get a backup repo and ran
git filter-branch --subdirectory-filter realProjectDir -- --all
on the new repo. It seemed to work, however I have lost my merge and branch structure from the old repository. The filtered repo has the correct paths but is lacking the merge commits. Additionally the old commits are still around, so that the repo looks like this:
(Commit A)
|
(Commit B)
|
(Commit C)
|
(Commit A')
|
(Commit B')
|
(Commit C')
Where A' are the new commits. Is there a problem with my multiple branches and using filter-branches
? So right now I have each commit twice, once with the wrong old path and once with the new one.
Summary
Is there a way of moving a complete directory tree one layer upwards and keeping all branches and merges without generating double commits?
Update I still cannot manage to keep my merge commits and the right branch layout. Everything is flattened into a chain of commits.
Update
Added the history tree before:
and after running filter-branch using gitk
:
The working directory is clean when I run this command. To test things I copied the whole directory tree using simple cp -r
to another directory and tested it there. The filter-branch command works in that sense that it moves the subdirectory, but it still flattens the history. As far as I can see it, I have not done anything too complicated, just the normal branching and merging stuff.
Update
I showed the wrong picture for the before state. I tried to transform a non-rewritten repository.