DEV Community

Cover image for Push project from existing repo to new repo
Olawale Adepoju for AWS Community Builders

Posted on • Originally published at dev.classmethod.jp

Push project from existing repo to new repo

Summary

Image description

I needed to build a new project from an existing project which I already had in a remote repository at some branch.I could do it manually by copying and pasting it into a new repo, but I prefer using a better way with the help of git.

Adding new remote repo into existing git project

cd to the project directory

$ cd existing-project-name
Enter fullscreen mode Exit fullscreen mode

List current remotes:

$ git remote -v
origin https://github.com/existing-repo.git(fetch) 
origin https://github.com/existing-repo.git(push)
Enter fullscreen mode Exit fullscreen mode

Add new origin (origin2):
(Because Git uses origin by default, let's create a new origin as origin2).

$ git remote add origin2 https://github.com/new-repo.git
Enter fullscreen mode Exit fullscreen mode

Let's see added origin:

$ git remote -v
origin https://github.com/existing-repo.git(fetch) 
origin https://github.com/existing-repo.git(push) 
origin2 https://github.com/new-repo.git (fetch)
origin2 https://github.com/new-repo.git (push)
Enter fullscreen mode Exit fullscreen mode

Pushing specific branch of the new repository:

On the existing repo, my project is on the master branch, while I want to push it to the main branch on my new repo.

The following command pushes a specific branch (master) from the current repo to the main branch of the new repo, with the remote set to origin2.

$ git push origin2 source_branch:destination_branch

If necessary, use --force to forcefully push into the new branch.

$ git push origin2 master(source-branch):main(destination-branch) --force
Enter fullscreen mode Exit fullscreen mode

I hope this helps when trying to push from the existing repository to the new repository.

Top comments (0)