DEV Community

Sameer Gurung
Sameer Gurung

Posted on • Originally published at Medium on

Git push to new repo from existing repo’s branch

Project setup to git push from my-project repository (repo) to new-project repo

While working with multiple projects, you might have encountered a situation where you need to create a new project from existing project’s (repo) some branch. We can always do it manually by copying and pasting and uploading it to new repo.

However, this is another “better way” to achieve this, with the help of git itself.

Lets start by adding new remote repository into existing git project:

 **Go to current project:** 

$ cd my-project

**List current remote:** 

$ git remote -v
originhttps://github.com/my-org/my-project (fetch)
originhttps://github.com/my-org/my-project (push)

**Add new origin (origin2): git remote add origin2 <git\_url>**

$ git remote add origin2 https://github.com/my-org/new-project

(By default, git uses origin, so lets add new origin as origin2)

**Lets see recently added origin:** 

$ git remote -v
originhttps://github.com/my-org/my-project (fetch)
originhttps://github.com/my-org/my-project (push)
origin2 [https://github.com/my-org/new-project](https://github.com/my-org/new-project) (fetch)
origin2 [https://github.com/my-org/new-project](https://github.com/my-org/new-project) (push)

Pushing ‘master’ branch to the new repository:

The following command pushes master branch of current repo to master branch of new repo with remote configured as origin2.

**$ git push <remote\_name> <remote\_repo\_branch>**

$ git push origin2 master

Pushing specific branch:

The following command pushes specific branch (say dev) of current repo to master branch of new repo with remote configured as origin2.

**$ git push origin2 <source\_branch>:<destination\_branch>**

$ git push origin2 dev:master

**Use --force (to forcefully push into that new branch if required)**

$ git push origin2 <source\_branch>:<destination\_branch> --force

Removing previous commits or tracking history of current repository:

If you want to push to the new repo without all the history of the existing repo, then you can create new orphan type branch which does not record previous history. Then push this new branch to our desired repo:

git checkout --orphan clean-branch

**Add the files and commit:**

$ git add . && git commit -m "initial commit to new repo"

Push to new repository from the new clean-branch:

**git push origin2 <source\_branch>:<destination\_branch>**

i.e;

$ git push origin2 clean-branch:master

Note: You can push to origin2 from new clean-branch created locally and don’t need to push this new clean-branch to server.

Deleting branch:

You can delete the newly created local branch if you wish. Just checkout to a different branch than the one you want to delete and then delete using commands:

**Checkout another branch:**

$ git checkout master

**Delete the newly created branch:**

$ git branch -D clean-branch

Top comments (0)