Git change base / root folder

I have a long (~ 2 years) repo with a lot of commits / branches, now for some reason I need to change the root folder of the repository one folder above.

Some examples to clarify the concept.

I have my repository inside the folder:

c:\workspace\test\src\ 

so in my repo there are all the changes in the files / subfolders above.

I would like to move the repository to:

 c:\workspace\test 

Now you can add all the changes to the test folder that support the history of the old repository.

therefore, all old commits marked in the "\" folder should now be marked in the "\src" folder

for old commits that I may or may not have (it really doesn't matter) the actual contents of the folder.

perhaps?

I hope I explained it clearly.

+4
source share
5 answers

I think you mean that the top level directory of your repo is c:\workspace\test\ and you want c:\workspace\test\src\ be the new top level directory. First check that this is the case:

 cd c:\workspace\test\src git rev-parse --show-toplevel #print repo top-level directory 

It should print something like c:\workspace\test\ if it really is your top level directory.

If so, you can use the git filter-branch rebasing command to make the src directory a new top level. Please be careful, this is what you really want to do, as it will destructively change your old story! Any commits affecting this branch will be reinstalled to contain the src folder as the new top level. First back up your current branch.

 cd c:\workspace\test\ git branch oldRoot #this backs up your current branch to a new branch called "oldRoot" git filter-branch --subdirectory-filter src HEAD #this modifies your history 

A warning!!! Note:

  • The new story will not be applied cleanly, if you have backup copies of your changes on a remote computer, you will need:
    • Start using a new remote branch (safer) or:
    • Press and hold to overwrite the deleted history.
  • If someone else collaborated with you on this repo, they could be in a heap of trouble. Make sure they change their changes to first , as it will be difficult for them to merge them after you rebase.

For more information, read “ Creating a New Root Subdirectory ” and “ The Reils of Rebasing in the Git Book.

+3
source

If I understand your question correctly, if you start with a clean git repository ( git status says "do nothing, clean working directory"), you just need to move the .git directory from c:\workspace\test\src\ to c:\workspace\test\ .

Then you need to make a “big” commit with all the files in the new layout. git will see this new commit as a big step. No new content is required as the files will be the same. git will only register a new tree layout (with src ) in the .git directory.


Re-reading the question makes me think that you might want your old commits to be available in the src subdirectory

If so, you need to completely rewrite the git repository history using something like git filter-branch --tree-filter '...'

0
source

It seems you only need to do this:

  • rename c:\workspace\test\src to c:\workspace\src
  • delete the directory c:\workspace\test , which is supposedly now empty.
  • rename c:\workspace\src to c:\workspace\test
Storage facilities

git usually does not track their parent directories, so moving storage from one place to another should be harmless. Now, any scripts / build files / everything that you have in your repository that may contain hard-coded paths can be a completely different story ...

0
source
 git filter-branch --index-filter ' git read-tree $(printf "040000 tree %s\tsrc\n" `git rev-parse HEAD:` | git mktree) ' -- --branches --tags mv .git .. cd .. 

See the documents in these commands; use is very simple. The advice in filter-branch docs on getting it to do its job on tmpfs is likely to be useful with two years of history.

You might want to grep -ri c.workspace.test .git to check the absolute paths in your configurations, etc., to make sure they are still valid.

0
source

First, clone your repo so that you can work on it in isolation, and keep your original intact if something goes wrong.

 git clone --no-hardlinks c:\workspace\test\src c:\sandbox 

write a script that makes the appropriate changes for any commit. I am not familiar with Windows scripts, but you want your script to create a new subdirectory called src (i.e. c:\sandbox\src ) and then move everything except the .git directory and any .git* files (e.g. .gitignore ) to this new subdirectory. Make sure your script will work properly with any commit in the repo, and not just in the current state. Then run:

 git filter-branch --tree-filter c:\absolute\path\to\your\script 

This will cause git to check each commit in the repo, run your script, and then replace the commit with the final result. Then, if you have ignored or unfixed files, you will want to copy them to the appropriate places in the new repo. Make sure the filter branch has the intended effect and the new repo looks the way you wanted it to. Once you are sure that you are happy, delete c:\workspace\test , and then move c:\sandbox to c:\workspace\test .

I prefer to use --tree-filter , as shown here, rather than --index-filter . It is less efficient (since it should check every commit, and not edit the index directly), but I find it more intuitive. If necessary, it will also allow you to make changes to the contents of files in the repo (possibly update absolute paths).

0
source

Source: https://habr.com/ru/post/1489162/


All Articles