How do I move my git project to a subdirectory for all commits, except one file?

Viewed 142

I'm trying to move my entire repo root into a sub-directory, for everything except one file.

For example, I have something like:

root
-Folder1
-Folder2
-.gitattributes

And for all commits and history, I want

root
-ProjectA
--Folder1
--Folder2
-.gitattributes

i.e., I want to move everything except .gitattributes to a subdirectory. If I use

 git filter-repo --to-subdirectory-filter ProjectA/

everything gets moved. I can't find the strategy for moving everything except a single file.

How do I keep the file where it is? Or how do I move just that file back up to the root with another command?

2 Answers

I know this is late but there is another option which achieves the same goal and feels a little better IMO:

git filter-repo --path Folder1 --path Folder2 --to-subdirectory-filter ProjectA

You can specify many paths which you want to be moved into the subdirectory in a single move. This is particularly beneficial if you are migrating to a monorepo - the --to-subdirectory-filter should match the final destination you want in your monorepo (ex. packages/project-a). Then, in your monorepo, you would do the following:

git remote add old-project ../path/to/old/project
git fetch old-project master
git merge remotes/old-project/master --allow-unrelated-histories
git remote remove old-project

You should now see all of your files located at packages/service-a inside the monorepo, with git history preserved. SEE: https://stackoverflow.com/a/61917589/264794

If you can, you would list all elements except .gitattributes, and rename them in one command, using path renaming:

git filter-repo --path-rename Folder1:ProjectA/Folder1 \
                --path-rename Folder2:ProjectA/Folder2 \
                ...

The OP House suggests in the comments the more sensible approach:

  • moving everything to a subdir,
  • then moving the .gitattributes back with --path-rename ProjectA/.gitattributes:.gitattributes

I would do each step in a separately cloned repository, to make sure I can go back to the original one if it fails.
The second step would be done in a clone of the repository from the first step.

Related