i used git to push project from existing repo to new repo

2023.04.26

Summary

 

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

List current remotes:

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

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

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)

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 mainbranch 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

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