Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git. Change repository root folder

Tags:

git

I have my project in folder C:/projects/NameOfProject/MyCode/, and .git is located in folder MyCode/, so only MyCode's contents are in repository.

Recently I added some other files that are in the folder OtherFiles/ related to the project in folder NameOfProject/. I can't move these to MyCode/ due to relationship between files in MyCode/ and OtherFiles/. And now I need to change repository settings somehow, the folders MyCode/ and OtherFiles/ will be located at the root of repository. And moreover, it's highly desirable to continue commit history: next commit after changing root of repository will keep on the history of the current branch.

To summarize the above: I have repo in C:/projects/NameOfProject/MyCode/ and I want to move repo root to C:/projects/NameOfProject/ not moving files/subfolders in both of these folders.

How can I achieve that?

like image 792
TrueCH Avatar asked Oct 25 '25 12:10

TrueCH


1 Answers

You need to rewrite directory names in the entire NameOfProject/MyCode/ repository using git filter-branch. I recommend to it this way:

  1. Backup everything.
  2. Copy directory MyCode to a temporary location.
  3. (In that temp dir) rewrite directories with the command: git filter-branch --tree-filter 'mkdir MyCode && mv * MyCode || :' -- --all. The filter will rewrite all branches; for every existing commit it creates a new subdirectory MyCode and moves all files into it (ignoring errors; there will be at least one error — cannot mv MyCode into MyCode). This is the main part of the entire process; it prepares the repository to expect all files in subdirectory MyCode instead of the root of the repository but preserves the commit history.
  4. Move the new .git directory to C:/projects/NameOfProject/ (not to MyCode!) and cleanup temp dir.
  5. Verify you have a backup and remove MyCode/.git.
  6. git add OtherFiles
  7. git commit -m OtherFiles
like image 152
phd Avatar answered Oct 27 '25 02:10

phd