4

I know there are many similar topics to this one, but I couldn't find an answer to my situation. Using git 1.8.3.1 I have a git repo in:

/path/to/my/git/repo/.git

I want to move the repository to /path/.git

while preserving the history of the original repo + adding new files from the new root directory. I've tried simply moving the .git folder to the appropriate place, re-add all files and committing but I've lost the history of my original repo.

How can I do this properly? (filter-branch?)

Thanks

UPDATE #1 My intention is to keep the files where they are, and expand the repo to include its parent directories + keeping history of the nested files:

Old repo: /path/to/my/git/repo/

New repo: /path/

UPDATE #2 I was thinking maybe there is a simpler solution than this, without moving actual files.

Community
  • 1
  • 1
amirb
  • 387
  • 3
  • 8
  • The description makes no sense. `repo/.git` is repository, the `repo` is working copy. So, you could move `repo` folder anywhere, it doesn't affect history at all. What exactly do you want to re-add? – kan Feb 29 '16 at 15:59
  • I understood the question to be about _expanding_ the existing repository to include its parent directories. @amirb, can you edit your question to be a bit more clear? – Justin Howard Feb 29 '16 at 16:02
  • edited as per your request, thanks! – amirb Feb 29 '16 at 16:07
  • So you want to effectively move the current files from the root of the repo `/` down to `/to/my/repo/`? – Hasturkun Feb 29 '16 at 16:18
  • @Hasturkun exactly the opposite, I want to keep the file where they are and expand the repo to include its parent directories. – amirb Feb 29 '16 at 16:20
  • What I mean is, assuming repo currently exists as `/path/to/my/repo/.git` and there exists a file `/path/to/my/repo/foo`, do you want to change the repo to be `/path/.git` with the file remaining as `/path/to/my/repo/foo` or do you want the file to now be `/path/foo`? – Hasturkun Feb 29 '16 at 16:22
  • Possible duplicate of [How do I Re-root a git repo to a parent folder while preserving history?](http://stackoverflow.com/questions/3212485/how-do-i-re-root-a-git-repo-to-a-parent-folder-while-preserving-history) – Justin Howard Feb 29 '16 at 16:25
  • @JustinHoward My case is more simple I think because I don't have branches in the current repo, only master. – amirb Feb 29 '16 at 16:47

2 Answers2

3

So, basically there are two steps here:

A. Move files which are under git, so that git repo has correct root:

cd  /path/to/my/git/repo/
mkdir -p to/my/git/repo/
git mv * to/my/git/repo/
git status #check all files are moved.
git commit -m "megamove!!!"

B. Move the result into correct position:

mv * /path/
cd /path/
rm -r to/my/git/repo/ #drop empty dir

Also, another approach, more straightforward (but sometimes it could be not very nice if you have wrong CRLF):

mv /path/to/my/git/repo/.git /path/
cd /path
git add -A
git status #check all files are moved.
git commit -m "megamove!!!"

History should be preserved, check it with git log -M

kan
  • 28,279
  • 7
  • 71
  • 101
  • The second approach does not keep file history, if I execute "git log -p -- to/my/git/repo/file1" it will not show all versions of the file... :-( – amirb Mar 01 '16 at 06:52
  • Actually, both of these approaches are not giving me anything when I execute git log for a specific file. (Maybe I am doing something wrong?) – amirb Mar 01 '16 at 07:12
  • I figured it out! It turns out that I needed to execute "git log --follow master -- some.file" to see the history of that file, because it was renamed when I re-added it to the new location of the repo. Thanks! – amirb Mar 01 '16 at 08:38
0

The .git directory contains metadata for your Git repo. The actual Git repo is likely housed under repo/.

Your repo is just a directory as far as your OS is concerned. It can be moved wherever you want, and the Git repo (complete with history) will move along with it.

So, in your example, what you probably want is to move repo/ to /path:

mv /path/to/my/git/repo /path/.
criswell
  • 759
  • 6
  • 10