5

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?

Bo A
  • 3,144
  • 2
  • 33
  • 49
  • 1
    Can you not just move the files and do a git add. Git will know if the files are the same. You will lose the history, but can get it using git log --follow /path/to/file. Take a look http://stackoverflow.com/questions/2314652/is-it-possible-to-move-rename-files-in-git-and-maintain-their-history – James Paolantonio Aug 01 '12 at 17:05

2 Answers2

6

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.

knittl
  • 246,190
  • 53
  • 318
  • 364
  • 1
    @BoA: hum. I thought I have written a disclaimer at the top of my answer. And advised you to read the manpage and understand the implications of this command. Have I been unclear about that? – knittl Aug 01 '12 at 17:15
  • I did read it. I just forgot to add the `-f` flag for `git push`. – Bo A Aug 01 '12 at 19:51
6

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.

stockersky
  • 1,531
  • 2
  • 20
  • 36