Branches: Create, Switch, Push, Merge, & Delete

Free Git Tutorial

Learn to efficiently use Git branches to enhance collaborative workflows, manage multiple versions of code, and seamlessly integrate new features into the main code base.

This exercise is excerpted from Noble Desktop’s Git & GitHub training materials and is compatible with updates through 2022. To continue learning web development with hands-on training, check out our coding bootcamps in NYC and live online.

Git Branches

Git lets you branch out from the original code base. This lets you more easily work with other developers, and gives you a lot of flexibility in your workflow.

git branches merge

Here’s an example of how Git branches are useful. Let’s say you need to work on a new feature for a website. You create a new branch and start working. You haven’t finished your new feature, but you get a request to make a rush change that needs to go live on the site today. You switch back to the master branch, make the change, and push it live. Then you can switch back to your new feature branch and finish your work. When you’re done, you merge the new feature branch into the master branch and both the new feature and rush change are kept!

Working on a Branch: Commit, Push, Pull, etc.

Keep in mind that whenever you commit, push, pull, etc. you’re doing so on a branch. So make sure you’re on the correct branch you doing any Git commands.

Full-Stack Web Development Certificate: Live & Hands-on, In NYC or Online, 0% Financing, 1-on-1 Mentoring, Free Retake, Job Prep. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

See What Branch You’re On

The current branch is shown at the bottom left of the Visual Studio Code window (master is the default branch name).

Create a New Branch

  1. At the bottom left of the Visual Studio Code window click the current branch name (master is the default branch name).
  2. In panel that appears at the top of the window, choose + Create new branch.
  3. Type in a name for your new branch and hit Return (Mac) or Enter (Windows)

    You’re now ready to commit to this branch.

Switch to a Branch In Your Local Repo

  1. At the bottom left of the Visual Studio Code window click the current branch name (master is the default branch name).
  2. A panel will appear at the top of the window listing the branches. Choose the branch that you want to switch to.

Switch to a Branch In a Remote Repo

  1. To get the list of branches from GitHub, at the top right of Source Control panel vscode soure control icon click the More Actions button vscode more actions icon and go into Pull, Push and choose Fetch.
  2. At the bottom left of the window click the current branch name (master is the default branch name).
  3. A panel will appear at the top of the window listing the branches. The ones that start with origin/ are remote branches. Choose the branch that you want to switch to.

Fetch versus Pull

Fetch downloads new data from the remote repo, but does not merge that into your files.

Pull downloads new data from the remote repo (using fetch), and merges it into your files.

Merge a Branch

  1. Switch to the branch that you want to merge another branch into (changes will be merged into this branch).

    Most commonly you’ll merge changes from another branch into the master branch, so you’ll typically switch to the master branch (at the bottom left of the window click the current branch name and choose the desired branch).

  2. In the Source Control panel vscode soure control icon make sure you don’t have any changes that need to be committed.
  3. Now you can merge another branch into the current branch. At the top right of Source Control panel vscode soure control icon click the More Actions button vscode more actions icon and from the menu choose Branch > Merge Branch.

  4. Select the branch you want to merge (into the current branch) and you’re done.

    NOTE: When you merge, there may be a conflict. Refer to How to Handle Merge Conflicts to learn what to do.

Delete a Local Branch

If you’re done with a branch and you don’t plan on using it again, you can delete it as follows:

  1. You can’t delete the current branch, so switch to different branch than you want to delete.
  2. At the top right of Source Control panel vscode soure control icon click the More Actions button vscode more actions icon and from the menu choose Branch > Delete Branch.

  3. Select the branch you want to delete.

    Confirm that you want to delete it, and you’re done.

photo of Dan Rodney

Dan Rodney

Dan Rodney has been a designer and web developer for over 20 years. He creates coursework for Noble Desktop and teaches classes. In his spare time Dan also writes scripts for InDesign (Make Book JacketProper Fraction Pro, and more). Dan teaches just about anything web, video, or print related: HTML, CSS, JavaScript, Figma, Adobe XD, After Effects, Premiere Pro, Photoshop, Illustrator, InDesign, and more.

More articles by Dan Rodney

How to Learn Git

Master Git with hands-on training. Git is a free, open-source version control system that allows developers to track the changes they make to code.

Yelp Facebook LinkedIn YouTube Twitter Instagram