2

I have a folder that's been under git for a while, and I need to move the git 'repo' one folder higher, for example, my folders are:

/project/data/
/project/src/.git

and I need to move it to:

/project/data/
/project/src/
/project/.git

Edit:
I've had the repo there for a while and it has a pre-existing history that I want to keep and I also need to move the repo on the server (same exact structure as local machine)

dan2k3k4
  • 1,388
  • 2
  • 23
  • 48
  • 3
    Dupicate of http://stackoverflow.com/questions/1918111/my-git-repository-is-in-the-wrong-root-directory-can-i-move-it-intead-of – exussum Nov 18 '13 at 16:18
  • Are you moving it one level higher for aesthetic reasons or because you want to include the `data` directory in the repo? – Roman Nov 18 '13 at 16:22
  • @user1281385 - Thanks, didn't find that earlier (and it didn't pop-up in the suggestions for me) – dan2k3k4 Nov 19 '13 at 10:00
  • @R0MANARMY - aesthetic and to include data, all the other projects follow the same pattern of project/.git rather than project/src/.git as there's some scripts in data that we keep under version-control – dan2k3k4 Nov 19 '13 at 10:00

2 Answers2

2

If you have no pre-existing history and just want to restart in the right place without moving too many files around, the duplicate question mentioned in the comments has the right answer for you.

If you want to keep existing history, but pretending it all happened a directory higher, here's a way to do it:

cd /project/src
git filter-branch --tree-filter 'mkdir src; git mv -k * .gitignore src'
cd ..
mv src junk
mv junk/src src
mv junk/.git .git

The filter-branch command rewrites your history line so that all action happens beneath the src/ hierarchy. The rest moves things back in place. If there are non-tracked files lying around, they'll be left in the junk/ namespace in the end.

Warning: I used git mv -k .gitignore src as a filter because it's the quick and easy way, but it will fail to move any dot-files you might have. (In which case, add them to the command.) The Right way would be to use git ls-files as an index list, but it's remarkably tricky to get right as far as the move goes, so if you can avoid it, by all means do.

JB.
  • 40,344
  • 12
  • 79
  • 106
  • Already have a pre-existing history and want to keep it. I'll have to read up on filter-branch, thanks. – dan2k3k4 Nov 19 '13 at 10:03
  • Quick question, would I need to run both commands on my machine and on the server? – dan2k3k4 Nov 20 '13 at 10:05
  • 2
    `filter-branch` is a history-rewriting command, so wherever you run it, you'll need to synchronize it everywhere else. You never mentioned you had a server; the commands here assumed they're running local (technically: on a checked-out repository). Easiest would be to perform on local and force-push on server, then have any other client re-clone from there. – JB. Nov 20 '13 at 13:43
1

This probably should work:

mv /project/src/.git /project/
cd /project
git add .
git commit

It essentially creates a new commit which moves the all files to the src directory and adds data next to it.

michas
  • 25,361
  • 15
  • 76
  • 121