简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM