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.
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.
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
- At the bottom left of the Visual Studio Code window click the current branch name (master is the default branch name).
- In panel that appears at the top of the window, choose + Create new branch.
-
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
- At the bottom left of the Visual Studio Code window click the current branch name (master is the default branch name).
- 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
- 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.
- At the bottom left of the window click the current branch name (master is the default branch name).
- 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
-
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).
- In the Source Control panel
make sure you don’t have any changes that need to be committed.
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.
-
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:
- You can’t delete the current branch, so switch to different branch than you want to delete.
At the top right of Source Control panel
click the More Actions button
and from the menu choose Branch > Delete Branch.
-
Select the branch you want to delete.
Confirm that you want to delete it, and you’re done.