Pushing All Branches - More Git Trickery

Here's a little problem I ran into using Git and how I got around it.

Pushing All Branches - More Git Trickery

I was getting ready for my Refactoring Workshop when I ran into a little problem. I thought it would be worthwhile to document the solution here (mostly for me and maybe some of you will benefit as well)

Background

I have a bunch of LabVIEW Katas with associated repositories. These are exercises that I have copied from Emily Bache and converted into LabVIEW. These all reside at gitlab.com/sas-lv-katas. Each kata has its own repository there.

As I'm prepping for my refactoring workshop, I want to use some of the katas in my workshop. I'd like to have a single landing page for my workshop gitlab.com/sas-workshops/refactoring I'd like to have students just go there and clone all the repos there and also have a single .vipc file there that they install. I don't want to have them go to the sas-lv-katas group and figure out which ones they need. There are a lot of katas there and they don't need all of them. Also, I'd like to be able to edit these katas a little bit for this specific workshop.

First Attempt

Things started out well. I cloned the existing repository, added a new remote, and pushed to the remote.

git clone git@gitlab.com:sas-lv-katas/simple-serial
git remote add refactoring git@gitlab.com:sas-workshops/refactoring/simple-serial
git push refactoring

At this point, I visited the Gitlab repository and noticed it only pushed my main branch. I have several branches in that repository for various scenarios and I wanted to push them all to the refactoring repository. Google revealed that git push refactoring --all should do the trick. Unfortunately, it didn't. It just returned Everything up-to-date. When I went to the GitLab repository it still only showed the main branch.

Solution

I could have just checked out each individual branch and pushed it one at a time. There were 5 of them and that seemed like a lot of work. It seemed like there ought to be an easier answer.

It took a lot of Googling ( I tried ChatGPT and it was no help), and eventually I came up with this:

for remote in `git branch -r | grep -v main `; do git checkout --track $remote ; done

git push refactoring --all

This answer came from https://stackoverflow.com/questions/6865302/push-local-git-repo-to-new-remote-including-all-branches-and-tags

Analysis

The problem was that git push refactoring --all only pushes all branches that you have checked out locally. When I cloned the repository, git only checked out the main branch locally. The first line is a for loop that makes sure I locally check out each branch first and then after that the git push refactoring --all worked fine.

Need Help

If you need help with your git-fu, check out our "Using Git Effectively Course" using the button below.