Reputation: 191
I am not certain how common this particular issue is but it took quite a bit to figure it out so I am reproducing the question here. There are similar posts, but none that addressed my particular concern.
We have a project with directory structure as follows:
ProjectA/
ProjectA.xcodeproj
ProjectA/
file1
file2
file3
file2 and file3 have code that we would like to share with another project and so I created a framework and used git mv to move the files into the new framework. The directory structure now looks like this:
ProjectA/
ProjectA.xcodeproj
ProjectA/
file1
FrameworkB/
FrameworkB.xcodeproj
FrameworkB/
file2
file3
FrameworkBTests/
I then tried to create a git submodule using the git subtree
command but it does not retain the commit history of file2 and file3 prior to being added to FrameworkB which defeats the purpose of carefully moving the files.
What are the dance steps for retaining the commit history?
Upvotes: 1
Views: 147
Reputation: 191
In order to create the new repository, these are the steps that I ended up coming up with:
(1) Clone the repo into a temp directory and remove the remote for the original repo.
git clone https://github.com/my-repo/ProjectA.git
cd ProjectA
git remote rm origin
(2) Use the git filter-branch --tree-filter
command to move all the files that are not in the FrameworkB/FrameworkB/ subdirectory up one level.
git filter-branch -f --tree-filter 'shopt -s dotglob nullglob; test -d FrameworkB && mv FrameworkB/* . || :' HEAD
(3) Use a variation on the same command to move the files in FrameworkB/FrameworkB/ up one level. (Note: this step is only necessary if you have a nested directory with the same name - which is common with Xcode projects.)
git filter-branch -f --tree-filter 'shopt -s dotglob nullglob; test -d FrameworkB/FrameworkB && mv FrameworkB/FrameworkB/* FrameworkB/. || :' HEAD
(4) In my case, I simply deleted the files I didn't want and committed the change since I was less concerned about stripping them from the commit history.
rm -r -f ProjectA/
rm -r -f ProjectA.xcodeproj
git commit -a -m "Remove the project files not associated with the shared framework."
Hopefully, others may find this useful.
References:
http://jimmy.schementi.com/splitting-up-a-git-repo/
http://will.willandorla.com/extract-to-git-submodule
Upvotes: 1