Git – copy few branches from old repository to a new one

Sometimes, then you need to transfer few Git branches from existing repository to a new one you could run the following commands:

git clone --no-checkout [New Git Repository URL] new-repo-folder

This command will create a new folder new-repo-folder with a .git folder inside, but without any files from branches.

Change to the repository folder:

cd new-repo-folder

Adding the necessary branches: Now you need to do “remote tracking” for the desired branches. You could do it using commands below:

git remote add old-origin [Original Git Repository URL]

git fetch old-origin dev:dev
git fetch old-origin stage:stage
git fetch old-origin prod:prod

This will add the dev, stage, and prod branches from the original repository to your local copy.

Check: Verify that you now have only these three branches.

git branch -l

In case if you have also have copied Git tags use the command below:

git tag -l

to list all Git tags.

To remove all local tags use the command below:

git tag -l | xargs git tag -d

To push all branches to remote use the following command:

git push origin --all