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.
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.
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.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.
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.
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.
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.
git fetch --all
update remote refsgit branch
list local branchesgit branch -r
list remote branchesgit branch -a
show all branchesgit switch branch-name
switch to a local branchgit switch --track origin/branch-name
make a local branch from a remote branchgit switch -c new-branch-name
create and check out a new branchgit push -u origin new-branch-name
push and set upstreamThese 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.