Practical Git: Isolate feature development with git branch

Trevor Miller
InstructorTrevor Miller

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 3 years ago

When working on a project, it is much easier to work on features and bugs in isolation of the rest of the project. We can do this with git branches; a branch is a copy of the working directory, staging area, and project history; we create a branch, then check it out, then add commits. After our feature has been built, we can then merge it back into the main stable branch - which is master by default. In this lesson we go over how to create a branch with git branch {branch-name}, viewing all branches with git branch, switching branches with git checkout plus a few helper commands.

[00:00] We're inside of a directory called utilityFunctions, which is a git repo. Let's run git branch to create a new branch on our repo. We need to give this a name so we'll say newFeature. Now we can review all of our branches in our repo by running git branch command by itself.

[00:16] This shows that we have two branches in our project. We have the master branch and we have the newFeature branch that we just created. The asterisk here next to master shows us that the master branch is what we have currently checked out.

[00:29] What that means is if we make commits right now, they're going to get added to the master branch and not any of the other branches in our repo. Because of this, git branches are useful for developing features on their own. When we ran the git branch command with a name, it created a new branch by copying the currently checked out branch.

[00:48] We can switch to a different branch by running git checkout and then the branch name. Let's switch to the newFeature branch. If we run git branch again, we can see that newFeature is the checked out branch. Since we haven't added any commits to the new feature branch, it's currently identical to the master branch.

[01:08] Let's create a new file called newFeature.js. Now when we stage and commit, the commit gets added to this branch instead of the master branch. If we look at the contents of this directory, we see our newFeature file, but when we check out the master branch or any other branch, that file is not in the directory.

[01:30] Git branches are extremely useful for developing features, bug fixes, or anything in isolation. Once that work has been finished and is stable, we can merge them back into the central branch.

[01:43] Since we frequently check out branches immediately after they're created there's a shortcut command to do both at the same time. We run git checkout -b, and then the name of the branch. If we run git branch we can see that this worked as expected. It created a new branch called newFeature2 and checked out that branch.

[02:02] Another useful shortcut command is git checkout with a single dash. This will switch to the branch that we had checked out last. We're currently on newFeature2, and if we run this command we will be on master. If we run it again we'll switch back to newFeature2.

Gabriel Garcia
Gabriel Garcia
~ 3 years ago

Thanks four this tutorial. I like your color theme on the terminal, can you share it?