How to list and switch between GitLab branches |Video upload date:  · Duration: PT7M0S  · Language: EN

Quick guide to list and switch GitLab branches using git commands and the web UI plus safe workflow tips for branch switching

So you inherited a repo that looks like a jungle gym for branches and you need to move around without causing an international incident. This guide shows how to list and switch branches in a GitLab repository using local git commands and a tiny amount of common sense. Expect commands you can paste, a little sarcasm, and no useless drama.

Sync remote refs

Start by updating your local view of the server. If you skip this you will get the fun surprise where someone insists a branch exists and you spend 20 minutes proving them wrong.

git fetch --all

This downloads refs from origin so the branch list is accurate. Think of it as giving git a quick memory refresh.

List branches

Want to know what is local and what lives only on the server Use these commands to inspect both sides of the dance.

  • git branch shows local branches and marks the current one with a star.
  • git branch -r lists remote branches that origin advertises.
  • git branch -a gives you everything at once, useful when you are feeling investigative.

Switch to an existing branch

Modern git gives you clear commands that do what you expect. To move your working tree to a branch use the switch command.

git switch branch-name

If the branch lives only on origin and you want a local copy, create a tracking branch in one move.

git switch --track origin/branch-name

This creates the local branch and sets it to track origin so pushes and pulls behave.

Create and track a new branch

When you start a feature or bugfix create the branch locally and check it out immediately.

git switch -c new-branch-name

That makes the branch and switches to it. When you are ready to share with the world push it up.

Push your branch and set upstream

Share the branch with GitLab and configure the upstream so future commands are less noisy.

git push -u origin new-branch-name

The -u sets origin new-branch-name as the default upstream so git pull and git push know where to talk without extra arguments. This saves keystrokes and reduces keyboard rage.

Why set upstream

Without an upstream git will ask you where to push every time. With an upstream you get the expected two step developer workflow. It is friendlier for humans and CI systems alike.

Quick cheat sheet

  • git fetch --all update remote refs
  • git branch list local branches
  • git branch -r list remote branches
  • git branch -a show all branches
  • git switch branch-name switch to a local branch
  • git switch --track origin/branch-name make a local branch from a remote branch
  • git switch -c new-branch-name create and check out a new branch
  • git push -u origin new-branch-name push and set upstream

These commands map cleanly to GitLab web actions like creating a branch for a merge request, but on the CLI you get speed and fewer clicks. Use them, avoid accidental commits on master, and remember that git is a tool not a personality test.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.