7

My local git repository looks like this

C:\MyRepo
      \MyProject
           \.git
           \ProjectFolder1
           \ProjectFolder2
           \ProjectFolder3

Now I want to add few folders which needs to be directly under "MyRepo" and needs to be part of same repository. I guess that means .git folder should also go directly under "MyRepo"

C:\MyRepo
     \.git
     \NewFolder1
     \NewFolder2
     \MyProject
           \ProjectFolder1
           \ProjectFolder2
           \ProjectFolder3

How do I move the .git folder up a level without losing the history?

I'm using Windows OS and git version is 2.6.3.windows.1

Mus
  • 7,290
  • 24
  • 86
  • 130
LP13
  • 30,567
  • 53
  • 217
  • 400
  • Possible duplicate of [My Git repository is in the wrong root directory. Can I move it? (../ instead of ./)](https://stackoverflow.com/questions/1918111/my-git-repository-is-in-the-wrong-root-directory-can-i-move-it-instead-of) – M Masood Nov 17 '19 at 23:03

2 Answers2

6

You can move all your files to an inner MyProject folder before moving the git repo. Something like this might work:

cd C:\MyRepo\MyProject
mkdir MyProject
git mv -k * MyProject
git commit
move .git ..
cd MyProject
move * ..
cd ..
rmdir MyProject
cd ..
git add NewFolder1 NewFolder2
git commit

See also How to import existing Git repository into another?

Community
  • 1
  • 1
Etienne Laurin
  • 6,731
  • 2
  • 27
  • 31
  • I like the `-k` flag. I didn't know it. – Jepessen Dec 11 '15 at 19:07
  • @AtnNn I cut and pasted .git folder from “C:\MyRepo\MyProject\.git” to “C:\MyRepo”. Then from the command prompt “C:\MyRepo” i executed “git add *” and then “git commit” to commit changes locally. Then add new folders under “MyRepo” and executed add and commit again. That seems to working. Is there anything wrong with this process – LP13 Dec 11 '15 at 19:55
  • @AtnNn Also when i run first 2 of your commands, which creates a new folder and then i check git status..it says "on branch master. nothing to commit, working directory clean" – LP13 Dec 11 '15 at 20:11
  • @user3862378 What you did might just work. If `git status` doesn't complain and your history is preserved, you should be fine. Perhaps my instructions are wrong. I tested them in bash and translated them for windows. – Etienne Laurin Dec 12 '15 at 15:33
0

The important bit to consider is that the name of the parent folder of a repo is irrelevant to the repo.

  1. rename "MyRepo/MyProject" to "MyRepo/tmp"
  2. create a new folder "MyRepo/tmp/MyProject"
  3. move all (except ".git") from "MyRepo/tmp" to "MyRepo/tmp/MyProject"
  4. commit -m "Move everything to MyProject"
  5. move all (including ".git") from "MyRepo/tmp" to "MyRepo"
  6. remove "MyRepo/tmp"
aercolino
  • 2,193
  • 1
  • 22
  • 20