neutrofoton

Science, Technology and Life

Import Existing Git Repository to Another

| Comments

While working with git, we may need to import source code from an existing git repository to our working copy. Merging it and pushing to origin master. The scenario that I had was :


  • I have a project template that I store on a git repository. Let say the url is http://server/git/template.git
  • I have another git repository with url http://server/git/project1.git

What I need from the two repositories are importing all contents (libraries, sources, etc) from template repository into my project1 working copy. Since I don’t want to coding from zero. To get into what I need, here are the steps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# git clone project1
$ git clone http://server/git/project1.git
$ cd project1

# add remote url named REMOTE.TEMPLATE
$ git remote add REMOTE.TEMPLATE http://server/git/template.git

# fetch from REMOTE.TEMPLATE remote url
$ git fetch REMOTE.TEMPLATE

# checkout REMOTE.TEMPLATE/master and create a new branch called TEMPLATE
$ git checkout -b TEMPLATE REMOTE.TEMPLATE/master

#switch back to master branch
$ git checkout master

# merge TEMPLATE brach to master branch
$ git merge TEMPLATE

# commit changes
$ git commit

The next step is checking the merge result on our working copy of master branch. If what we have been imported already there, now we can remove the remote URL of REMOTE.TEMPLATE and TEMPLATE branch to get rid of the extra branch before pushing.

1
2
3
4
5
6
7
8
# remove REMOTE.TEMPLATE remote address
$ git remote rm REMOTE.TEMPLATE

# remove template branch. It is useful to get rid of the extra branch before pushing
$ git branch -d TEMPLATE

# push to remote origin/master
$ git push

References

  1. http://stackoverflow.com/questions/1683531/how-to-import-existing-git-repository-into-another

Comments