TrueCH
TrueCH

Reputation: 501

Git. Change repository root folder

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?

Upvotes: 4

Views: 2082

Answers (2)

phd
phd

Reputation: 94406

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

Upvotes: 6

Hariprasath
Hariprasath

Reputation: 539

You can initialize a new repo under C:/projects/NameOfProject/ by

git init

and pull the remote branch under that repo and just copy and replace those two folders MyCode and OtherFiles into initialized git repo and push it to your remote.

Upvotes: -1

Related Questions