joedawson's avatar

Moving a git repo, into a new branch in an existing repo?

Hey all,

I've tried doing some Googling for this, but I think that I've been searching is too lengthly and isn't returning the results I'm after so I'm going to give here a go.

I currently have two separate projects (locally) that I want to merge into one, but separate the two into their own branches.

For now, I'm going to say my repos are both called project-black and project-gold

What I'd like to do is;

  1. Take project-gold's master branch
  2. Add this to project-black as a NEW branch
  3. Allowing me to dispose of project-gold as it's now on it's on branch in project-black

Any ideas how I can tackle this?

0 likes
4 replies
petehouston's avatar
$ mkdir awesome-project
$ cd awesome-project
$ git init
$ git checkout -b project-gold master
...add all source code for project-gold here, do whatever you like in this branch "project-gold"
$ git checkout master
$ git checkout -b project-black master
...add all source code for project-black here, do whatever you like in this branch "project-black"
$ git checkout master

Branch master is basically the holder for the other two projects; so it's best to be empty anyway. So just switch to your project branch to work by issuing $ git checkout [project-branch] is enough.

usman's avatar
usman
Best Answer
Level 27

Use orphan branches instead they don't inherit the source history, for instance:

$ mkdir mega_repository
$ cd mega_repository/
$ git init
$ git checkout --orphan project1
$ touch project1.txt
$ echo "first project" > project1.txt 
$ git add .
$ git commit -m 'initial commit in first project'
    [project1 (root-commit) 4d4cb2b] initial commit in first project
    1 file changed, 1 insertion(+)
    create mode 100644 project1.txt

$ git checkout --orphan project2
    Switched to a new branch 'project2'


$ rm .git/index             #reset the index for this new project

$ rm project1.txt           #clean the directory for the second project source


$ touch project2.txt
$ echo "second project" > project2.txt
$ git add .
$ git commit -m 'initial commit in second project'
    [project2 (root-commit) abeae16] initial commit in secont project
     1 file changed, 1 insertion(+)
     create mode 100644 project2.txt

$ git log
    commit abeae168a6bed156a134461ae8a025bfdf619778
    Author: Usman <usman@***.com>
    Date:   Sat Feb 21 00:45:17 2015 +0500

        initial commit in second project

$ git checkout project1
$ git log
    commit 4d4cb2b8a315bb7da55ba67fd2de42008b177fbd
    Author: Usman <usman@***.com>
    Date:   Sat Feb 21 00:43:52 2015 +0500

    initial commit in first project

For further information check this page :http://git-scm.com/docs/git-checkout/1.7.3.1

Usman.

joedawson's avatar

Thanks @usman and @petehouston, I went with your method in the end @usman

$ cd project-black
$ (master) git checkout -b --orphan development
$ (development) git rm -rf .

At this point, I simply drag and dropped the files from project-gold into the project-black folder on my machine

$ (development) git add -A
$ (development) git commit -m "Moving project-gold into project-black"
$ (development) git push origin development

Done. :)

Please or to participate in this conversation.