Jonwd
Jonwd

Reputation: 655

How to move a local git repository to the root folder and preserve history?

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.

Upvotes: 2

Views: 565

Answers (2)

Nick Volynkin
Nick Volynkin

Reputation: 15079

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'

Upvotes: 5

letz
letz

Reputation: 1792

  • Move the content of the project1 to a subfolder of project1
  • Commit changes
  • Move project2 to the project1 folder
  • Commit

Upvotes: 1

Related Questions