1

There are similar questions but not quite what I need.

I have this structure in several projects:

/var/www/proj-a/
  app/
    .git/
    script-a.js
    script-.js
    ... more files and directories
  data/
    ... data and config files go here

I want to have the .git folder directly in the project folder, not as a sub-directory of app. But the repo will still only manage files in the app directory. In other words, I will use .gitignore to exclude everything inside the project directory except for the app directory.

The new structure should look like this:

/var/www/proj-a/
  .git/
  app/
    script-a.js
    script-.js
    ... more files and directories
  data/
    ... data and config files go here
  .gitignore

Now, in git are paths saved as absolute paths allowing me to just move the .git directory to the project directory without any issues; or are paths relative resulting in source files not being found? If the paths are relative, how can I achieve this change without loosing my git branches and history?

Majid Fouladpour
  • 29,356
  • 21
  • 76
  • 127
  • 3
    Why do you care where `.git` is? That said, you can put `.git` anywhere you like; see the `--git-dir` and `--work-path` options in `man git`. – chepner Jan 19 '22 at 21:40
  • @chepner it is more convenient to have `app` only contain source files. One example is when I want to deploy. I use `rsync` and if I did not have `.git` in `app` I could simply `rsync` the `app` directory and be done with it. Now I am `rsync`ing subdirectories of `app` one at a time. – Majid Fouladpour Jan 19 '22 at 21:48
  • 2
    `rsync --exclude .git/`? (Not that I condone deploying anything by simply dumping a Git checkout somewhere else; write a proper installation script.) – chepner Jan 19 '22 at 21:51
  • 1
    @MajidFouladpour In that particular situation, you can use the `--exclude` flag on `rsync`: https://stackoverflow.com/a/11497654/1440565. – Code-Apprentice Jan 19 '22 at 21:52
  • 2
    "in git are paths saved as absolute paths...or are paths relative...?" Git tracks paths relative to the project root, which is the folder where `.git` is located by default. – Code-Apprentice Jan 19 '22 at 21:54
  • deployment is but one issue. There are other reasons I prefer the new structure. Sounds like it is not a simple mater of moving the `.git` one level up though ... Is there no way? – Majid Fouladpour Jan 19 '22 at 21:58

1 Answers1

1

There are other reasons I prefer the new structure.
Sounds like it is not a simple mater of moving the .git one level up though ... Is there no way?

You don't exactly move the .git/ folder one level up.

What you can do is move the elements in the current Git repository one level down, under a new app/ folder, using git filter-repo.

Once you have a Git repository with the right structure, you can add the data/ folder in it, and clone the end result as proj-a.


As noted by knittl in the comments:

This modifies all existing commit ids.
If you have your history shared, all others need to re-clone.

This is true for any rebase or filter operations (git filter-branch, git filter-repo, BFG, ...): a git push --force or git push --mirror is often implied at the end.
And that does mean communicating to any contributor the new state of that repository, for them to clone it again.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Note that this modifies all existing commit ids. If you have your history shared, all others need to re-clone. – knittl Jan 20 '22 at 18:26
  • 1
    @knittl Good point. I have included your comment in the answer for more visibility. – VonC Jan 20 '22 at 18:47