LP13
LP13

Reputation: 34079

How do I move the .git folder to a parent folder without losing history?

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

Upvotes: 7

Views: 3220

Answers (2)

aercolino
aercolino

Reputation: 2346

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"

Upvotes: 0

Etienne Laurin
Etienne Laurin

Reputation: 7184

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?

Upvotes: 6

Related Questions