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

Git Tutorial

Learn about Git branches, how they enhance flexibility and collaboration in your workflow, and how they can be used in Visual Studio Code. Gain insights on creating and managing branches, merging, and handling merge conflicts.

Key Insights

  • Git allows you to create branches from the original code base, enhancing flexibility in your workflow and collaborative work with other developers.
  • When working with Git commands like commit, push, pull, ensure you are on the correct branch.
  • The Visual Studio Code window displays the current branch at the bottom left, with 'master' being the default branch name.
  • Creating a new branch in Visual Studio Code involves clicking the current branch name, choosing '+ Create new branch', and typing in a new name.
  • Switching to branches in your local or remote repository is possible via the Visual Studio Code window.
  • When merging branches, ensure you are on the branch you want to merge into, and that there are no changes that need committing in the Source Control panel.

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.

Essential Git Commands

  • git status

    What's changed, staged, and untracked — your most-used command.

  • git diff

    See exactly what's different before staging.

  • git log

    Inspect commit history with --oneline or --graph for visualization.

  • git reset

    Undo staging or commits — careful with --hard.

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.

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 click the More Actions button 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 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 click the More Actions button 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 click the More Actions button 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.

Learn more in these courses

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