Bo A
Bo A

Reputation: 3154

Change the root directory of a Git repository

I've got the following source structure:

/dir1
    file1
    file2
    file3

dir1 is unneeded as the repository itself can be like a folder, so I want my git repository to look like this:

file1
file2
file3

What should I do to achieve this?

Upvotes: 5

Views: 13956

Answers (2)

stockersky
stockersky

Reputation: 1571

You can move your .git in the subdirectory (don't forget your .gitignore). Then run git init :

Important : Do a last commit before moving (i'll explain it...)

mv .git ./dir1/.
git init .
git add .
git commit

It's important to do a last commit before moving : git uses the hash of files to recognize them. unchanged files are seen as 'renamed'. If you have uncomitted files before moving, it will log them a couple 'deleted' 'added' for each modified file and you'll loose history on those.

Upvotes: 7

knittl
knittl

Reputation: 265151

DISCLAIMER: this will rewrite history and be a PITA for anyone who has already cloned your repository. You shouldn't do it on published history.

That said, you should be able to rewrite all your trees by using the filter-branch command of Git. Be sure to understand all implications before using it (please, read the manpage; have backups).

git filter-branch \
  --subdirectory-filter dir1 \
  --tag-name-filter cat \
  -- --all

NB. This command will also perist your grafts and replace refs.

Upvotes: 6

Related Questions