Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT change Base/Root Folder

Tags:

git

rebase

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

Some examples to clarify the concept.

I have my repository inside a folder:

c:\workspace\test\src\

so in my repo has all the changes in files/subfolder of the above one.

I would like to move the repository to:

c:\workspace\test

being able from now on to add all the changes in the test folder mantaining the old repository history..

so all the old commits that are marked in folder "\", now should be checked in folder "\src"

for the old commits I can or can not have (it really does not matter) the actual content of the folder..

is that possible?

I hope what I explained is comprehensible..

like image 940
Marcx Avatar asked Oct 22 '22 06:10

Marcx


1 Answers

I think you mean that the top-level directory of your repo is c:\workspace\test\ and you want c:\workspace\test\src\ to 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 that is really your top-level directory.

If it is, you can use the git filter-branch rebasing command to make the 'src' directory the new top-level. Please be careful that this is what you really want to do as it will destructively modify your old history! Any commits affecting this branch will be rebased to contain the src folder as the new top-level. Back up your current branch first.

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

Warning!!! Note that:

  1. The new history won't apply cleanly, if you have your changes backed up to a remote you will need to either:
    1. Start using a new remote branch (safer), or:
    2. Do a force push to overwrite the remote history.
  2. If anyone else was collaborating with you on this repo, they may be in for a heap of trouble. Make sure they get their changes merged in first, as it will be difficult for them to merge them in after you do the rebase.

For more info, read the "Making a Subdirectory the New Root" and "The Perils of Rebasing" sections in the Git Book.

like image 64
Eliot Avatar answered Oct 24 '22 03:10

Eliot