How to move multiple repositories into one

Hello 

I am new here

I have multiple repositories on my profile page
Here’s what i want to do
I wanna create a master folder(name it Core Java Programs) with multiple subfolders and move these repositories into that sub folder.
when i am on my page, the operation should be like this
Core Java Programs->
Hello Java
AverageCalculator
so on and so forth

Please help

2 Likes

Hello @meetdineshsharma,

Welcome to Github Community Forum!

Is not so difficult, I try to explain some solutions

Solution 1: clone, delete and init

  1. Create the directory of the main project. If you use linux:

    mkdir <name_project>

  2. Move inside that directory. If you use linux:

    cd <name_project>

  3. Clone a project inside the directory. Here you can use even a command line or git GUI like github desktop if you are in Windows and clone:

    git clone https://github.com/MeetDineshSharma/<repository_name>.git

  4. This step is important. You need to delete the .git directory inside the project <repository_name> , this is necessary if you want to create a monolit project with lot of git repository. First move inside the directory and then delete the .git. If you use linux:

    cd <repository_name>
    rm -rf .git

The -rf is necessary because there are some protected files inside it and many folder.

  1. Repeat from point 3 until you put every project you want.

  2. Return in the root folder and use the init command of git to create the repository. In a command line or with the git GUI run the init command:

    git init

This command will create the .git folder with all the infos to use git inside that folder.

  1. Add the remote origin with the link to the github repository:

    git remote add origin https://github.com/MeetDineshSharma/<main_project_name>.git

This assumes that the main project repository already exists in github.

  1. Done…  Now you can use this as a git repository.

Solution 2: elegant submodules

This is the most elegant solution (maybe).

  1. Create the directory of the main project. If you use linux:

    mkdir <name_project>

  2. Move inside that directory. If you use linux:

    cd <name_project>

  3. Initialize the git repository using the init command with a console or a git GUI:

    git init

  4. Instead of clone the repository you want, you can create multiple submodules. You can find the man page here: Git-Tools-Submodules. Some git GUI doesn’t have this functionality, if not use the command line and run this command:

    git submodule add https://github.com/MeetDineshSharma/<repository_name>

Run this command for every repository you want to add.

  1. To use a module you need “clone” it. First of all you need to init:

    git submodule init

Than update:

git submodule update
  1. Add the remote origin with the link to the github repository:

    git remote add origin https://github.com/MeetDineshSharma/<main_project_name>.git

This assumes that the main project repository already exists in github.

  1. Done…  Now you can use this as a git repository.

WARNING: Is being a long time since I didn’t use the submodules so is probably that the related solution may work and may not :slight_smile:

Hope this help, ask if you have more questions.

-Gabriele-

Mark helpfull posts with Accept as Solution to help other users locate important info. Don’t forget to give Kudos for great contents!

3 Likes

Please add some push command to push that repo to github same with branches and history

I came across this when looking around to see if it’s possible to consolidate repos in a way that transfers stars and watchers (doesn’t look like that’s possible). Wabri’s answer works if you want to use submodules or if you don’t mind losing the commit history of the original separate repos, but this is what I do to preserve commit history and truly consolidate repos (no submodules):

  • In the repo you want to move, create a directory and move all files except for the .git directory into it. In OP’s case this directory might be called AverageCalculator for example. Commit that change and push it up to Github (see note at the bottom if you want to skip pushing).
  • In the repo you want to move into, add the other repo as a remote. Example if you use ssh auth: git remote add averagecalculator git@github.com:your-username/averagecalculator.git. Then git fetch --all and git merge averagecalculator/master --allow-unrelated-histories (assuming master is the branch you want).
  • Resolve any merge conflicts (allow-unrelated-histories will usually fix it so you don’t have any), merge commit, and push that up to your main/parent project repo.

I’ve done this dozens or maybe hundreds of times when consolidating what I call “junk” repos, projects I created while in school or when just trying out different languages or technologies. After this is done, you can safely delete the sub-repo, since its entire commit history and all files now live in the parent repo.

If you’d like to skip pushing and have both repos on disk, you can also use local remotes. Assuming both repos are in the same directory, from the “parent” repo you substitute git remote add some-sub-project ../some-sub-project, then continue on with the other instructions above.