2

I have a local git repository which is inside a project. I have decided that some code needs to be in another project but I only want one git repository.

Current directory structure:

Solution->
 - Project1
   - .git
 - Project2

Desired directory structure:

Solution->
 - .git (tracks both projects)
 - Project1
 - Project2

How do I move .git to the root folder (Solution) so that it can begin to watch both Project1 and Project2 without losing track of the modifications/commits?

Note: I don't really care about preserving the history of the files that are going to be moved to the Project2 folder.

Note: This is not a duplicate to (this question) because I'm not moving a git repository and its whole contents. I want to only move the git repository to the root folder.

Community
  • 1
  • 1
Jonwd
  • 635
  • 10
  • 24
  • possible duplicate of [How to move a git repository into another directory and make that directory a git repository?](http://stackoverflow.com/questions/19097259/how-to-move-a-git-repository-into-another-directory-and-make-that-directory-a-gi) – Kyle Trauberman Jun 03 '15 at 19:43
  • @KyleTrauberman I'm not a git expert but if I do what the answer says, wouldn't I lose all the changes made in Project1? (Because of the directory change) – Jonwd Jun 03 '15 at 19:44
  • 2
    Move actual files to Solution/Project1/Project1 with git. Manually move .git folder to Solution. Manually move back files to Solution/Project1. Add Project2. Enjoy. – kiwixz Jun 03 '15 at 19:44
  • @iKiWiXz Thanks. That is actually clever. I didn't understand what was going on until I did it myself. – Jonwd Jun 03 '15 at 19:57

2 Answers2

5

The git add -u solution

Most elegant, needs 6 commands.

cd Solution/
#dot means "move it here"
mv Project1/.git .

#make git add deleted files
git add -u

#make it recognize them as moved
git add Project1/

Now git status should show "renamed..."

git commit -m'moved to a subfolder'

Now check for unwanted files and update .gitignore

#add the project2:
git add --all
git commit -m'added project2'

The subfolder solution

  1. in Solution/Project1/ create a subfolder with the same name: Solution/Project1/Project1/.
  2. Move all other contents (except .git/, .gitignore, .gitconfig and new subfolder) of the Solution/Project1/ to Solution/Project1/Project1/
  3. cd Polution/project1, git add --all, git commit -m'moved to a subfolder
  4. Move all contents of Solution/Project1 to Solution. Now project files are back in Solution/Project1/ and .git and git-files are in the Solution/
  5. cd Solution/, git status. Watch for unwanted files. Add them to .gitignore
  6. git add --all, git commit -m'added project2'

The git mv solution

cd Solution/Project1/
mkdir Project1/

#Now move all your files with git mv
git mv foo.txt Project1/
git mv bar.js Project1/
git mv fizz/ Project1/
....

git commit -m'moved files to a subfolder'
cd ../

Now move all files from Solution/Project1/ to Solution. This is probably done with mv command, but I don't catch the exact syntax.

# this should work now
git status
# fix .gitignore if necessary
git add --all
git commit -m'added project2'
Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
1
  • Move the content of the project1 to a subfolder of project1
  • Commit changes
  • Move project2 to the project1 folder
  • Commit
letz
  • 1,762
  • 1
  • 20
  • 40
  • 1
    Here's what you have after letz's solution: Project1/Project1 and Project1/Project2. Just rename Project1 so you now have Solution/Project1 and Solution/Project2. Then move Solution so that it's where you wanted it to be. – trent Jun 03 '15 at 19:56
  • Why you can't make Project1 folder of your projects and simply move the the actual project1 inside a folder? and them move project2 ? – letz Jun 03 '15 at 19:57
  • @letz Thanks, I got it now. I believe iKiWiXz's solution is cleaner though. – Jonwd Jun 03 '15 at 20:02
  • Oh, it seem like a lot of people explain the same solution. – Nick Volynkin Jun 03 '15 at 20:03