45

I have a subdirectory named "www" that is a repo:

site
|-- www/
|    |-- .git/
|    |-- index.html
|-- design/
|    |-- images.jpg

I'd like to change the repo to the parent directory so that the repo structure mirrors the original file structure as follows:

site
|-- .git/
|-- www/
|    |-- index.html
|-- design/
|    |-- images.jpg

Can this be done? Are there implications with then pushing the changes up to Gitlab?

RGilkes
  • 1,012
  • 3
  • 12
  • 22
  • 2
    I think you can just move the .git/ directory up a directory (or two). It's going to be sort of expensive, and that first commit is going to be interesting, but it should work. (based on the second answer to this question http://stackoverflow.com/questions/1918111/my-git-repository-is-in-the-wrong-root-directory-can-i-move-it-instead-of ) – mrcheshire Aug 18 '15 at 20:59
  • 1
    @mrcheshire Using `git mv` gives me a fatal error that says the destination directory "is outside the repository". – RGilkes Aug 18 '15 at 21:06
  • Just use `mv`. Then `git add -A`, `git commit -a` – maackle Aug 18 '15 at 21:30
  • 1
    @maackle This loses all revision history and looks like the files were deleted – RGilkes Aug 18 '15 at 21:36
  • In the current state, is `design` under revision control (i.e., a peer of `.git`? – rholmes Aug 18 '15 at 21:47
  • @rholmes no, it is not. `www` is the root of the repo currently. – RGilkes Aug 18 '15 at 22:23
  • Roland Smith's answer **should** work. Not sure if user541051's will (I've never tried) bit it may - it appears based on the answer mrcheshire points to. Just make a backup of your repo before doing anything drastic ;-) – rholmes Aug 18 '15 at 22:27

2 Answers2

34
  1. Create a www directory in your repo.
  2. git mv the HTML files to that directory. Optionally commit this step.
  3. mv the design directory into your repo, and git add .
  4. Commit.
  5. Rename/move your whole repo.
Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • Worked perfectly. Such a simple solution too! – RGilkes Aug 18 '15 at 22:37
  • By the `git mv` in step 2. this will - temporarily / until step 5 completes - move your files to a different place in the filesystem which _may_ be undesirable when these files serve any kind of live purpose - like being a web server's content for example. [The solution given by @user5241051](https://stackoverflow.com/a/32083479/5110545) does not have this flaw. – cueedee Feb 21 '20 at 14:54
  • 2
    Repositories are for development. Almost by definition the content of the working tree will be in flux. Using that as live webserver content would be ill advised, to say the least. – Roland Smith Feb 21 '20 at 20:02
  • While that is very true, git _does_ get used for [other purposes](https://hackaday.com/2017/05/23/stupid-git-tricks/). – cueedee Feb 25 '20 at 09:21
  • To those that carried out this Git repo move, I have a question. What happens if I carry out these steps, then try to reset to (or checkout) a commit that occurred before this move? Will there be any issues? For example, will the Git repo somehow move *back* to its original directory? – cag8f Mar 20 '20 at 08:28
  • Succinct and elegant... thank you! – Jc Nolan Apr 30 '21 at 15:16
20

The following instructions will work.

cd www
mv .git ../
git add www
git commit -a -m "Change root directory of project"
git add design/*
git commit -m "Start tracking design folder"

This will preserve your history.

user5241051
  • 211
  • 1
  • 3
  • 2
    You lose all hisotry on your files when doing this. – Youssef NAIT Nov 22 '18 at 13:49
  • 1
    @NAIT If I do `git log` the history is preserved but on GitHub the history is removed. – viery365 Oct 21 '19 at 15:25
  • This should be the accepted answer. – cueedee Feb 21 '20 at 14:59
  • Having said that, another method that rewrites history as if the parent directory has always been part of the repo can be found [here](https://stackoverflow.com/a/60394780/5110545). Pushing changes is likely to need `--force` though. – cueedee Feb 25 '20 at 20:13
  • Can you please explain how this works? It seems counter intuitive. Doing this I realized that git does a rename, but what is happening behind the scenes? – Sahil Singh Apr 06 '20 at 04:44
  • 1
    Should there be an extra step after 2 `cd ..`, or was that implicitly expected, seeing not everyone is an idiot like me... – Sjors Hijgenaar Jul 01 '21 at 19:20