How to push code of branchA to different repository branchB ?

Author: neptune | 22nd-Feb-2022
#Github

We are going to push the code of branch(branchA) to a new branch(branchB) in a different repository.

branchA: Which you want to Push.

branchB: Where you want to Push.

Follow these steps:

  • Clone branchA locally in your machine.

  • Remove the remote origin from above cloned branchA.

  • Create a new branchB if not created yet.

  • Add remote origin of branchB repository.

  • Checkout branchB.

  • Then Commit changes if you make any.

  • Then Push the code in branchB.


Detailed Steps:

Clone URLs: 

BranchA Repo: https://github.com/Neptune998/NeptuneWorldA.git

BranchB Repo: https://github.com/Neptune998/NeptuneWorldB.git






Step1: 

Clone the existing repository from where you want to push the code i.e NeptuneWorldA .

 git clone https://github.com/Neptune998/NeptuneWorldA.git       


Now, checkout the branchA

git  checkout -b branchA


Now check the branch.

git branch -a

Step2:

Now we will remove the existing remote origin NeptuneWorldA and add remote origin NeptuneWorldB.


Check the remote origin:

git remote -v


git remote rm origin


Now add the origin of NeptuneWorldB.


git remote add origin https://github.com/Neptune998/NeptuneWorldB.git

Step3:

Check the remote origin.

git remote -v



git checkout -b branchB




Follow the steps we always do to push code.


git status

git add .

git commit -m “Commit msg”

git push -u origin branchB


If you get warning to pull and you don’t want to add the existing code then use -f tag. 


git push -u -f origin branchB


If you face any issue write in the comment section.


Thanks for Reading !




anonymous | Dec. 8, 2021, 10:12 a.m.

Brief and Short Great !



Related Blogs
Git - Recovering Discarded Changes
Author: neptune | 13th-Jul-2023
#Github #Problem Solving
We will follow a scenario where a developer is working on a web application and needs to recover a discarded commit to reintroduce a specific feature...

GoodbyeX: A Step-by-Step Guide to Remove the "X" Branding from Twitter
Author: neptune | 26th-Jul-2023
#Github #Projects
Twitter has been known for its continuous updates and changes to its user interface. One such change was the introduction of the "X" branding, which might not be appreciated by all users...

Generating an SSH Key and Adding it to Your GitLab Account
Author: neptune | 19th-Sep-2023
#Github
SSH keys provide a secure way to authenticate yourself to GitLab without the need for a username and password...

View More