How to List, Switch and Checkout Git Branches |Video upload date:  · Duration: PT5M43S  · Language: EN

Quick guide to list switch and checkout Git branches with examples for git branch git switch and git checkout

If you ever wanted to look like you know what version control is without actually panicking in front of your team this tutorial will help. We cover how to list branches switch between them create new ones and pull single files from another branch using the git CLI. No magic unicorns just practical commands and a hint of sarcasm.

List branches and find the thing you lost

Want to see what branches exist in your repo without guessing wildly The git branch family of commands is your friend. By default git branch prints local branches only which is usually all you need until someone pushes a half finished feature to origin and you regret trusting humans.

git branch                # show local branches
git branch -a             # show local and remote refs
git branch -r             # show remote branches only

Use -a when the repo is a mess and curiosity gets the better of you. The output shows which branch HEAD points to with an asterisk. That pointer is how git knows what code is checked out in your working tree.

Why remote refs matter

Seeing remote branches helps when you want to check out a branch that exists on github but not yet locally. It also prevents the classic awkward moment where you assume a branch exists and then realize it is only in someone else s fork.

Switch branches without drama

Switching branches changes your working tree and moves HEAD. Prefer the modern command git switch because it reads like plain English and makes your intent obvious. The old git checkout still works and will stare at you from legacy scripts like a forgotten dependency.

git switch feature-branch    # switch to an existing branch
git checkout feature-branch  # older command that still works

If you need to toggle back to the branch you were just on use the dash shortcut. It is shockingly convenient when you are pretending to multitask.

git switch -    # go back to previous branch
git checkout -   # older form doing the same thing

Create a branch and check it out in one step

Creating a branch and moving HEAD in a single command saves a keystroke and some dignity. Use -c with git switch or -b with git checkout depending on your git version and mood.

git switch -c new-branch   # create and switch in modern git
git checkout -b new-branch # create and switch with the classic command

Pick clear names for branches so future you does not have to invent excuses at code review. Good examples are feature/authentication fix/login-bug or chore/update-deps. Bad examples are stuff or temp or finally.

Restore a single file from another branch when things go wrong

If a recent change broke the site and only one file needs rescue you do not need to reset the whole branch. Use git checkout with a double dash and a path or use git restore on newer git versions which is less ambiguous.

git checkout main -- path/to/file                # classic way to get a file from main
git restore --source=main path/to/file           # preferred on modern git

The double dash ensures git treats the next token as a path and not a branch name which avoids nasty surprises in your working tree.

When to prefer git restore

git restore was added to separate restore intent from the multipurpose checkout command. If your git is recent prefer git restore for single file operations and git switch for branch changes. Legacy commands still exist and will keep working for a long time because of historical inertia.

Quick reference and final tips

  • List local branches use git branch
  • See remote branches use git branch -a or git branch -r
  • Switch branches use git switch branch-name or the older git checkout branch-name
  • Create and switch use git switch -c name or git checkout -b name
  • Restore a file from another branch use git restore or git checkout with a double dash

This short tutorial covered how to list existing branches switch between them create new branches and recover files across branches using the git CLI. Use these commands on your local machine or on github clones to tame branching chaos and keep version-control slightly less terrifying.

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.