Getting Started with Git

Aug 14 2023 · git 2.28, console

Part 1: Getting Started with Git

04. Create a Branch

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 03. Clone a Repository Next episode: 05. Merge Changes

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 04. Create a Branch

Now you know how to create a repository, clone a repository, and commit changes to that repository. If you remember from the Google Docs example, we eventually ran into a scenario where one user restored to a previous version of the document and ended up clobbering the changes the other user was working on. This would also happen if, on our Recipes Book project, both users end up committing to the same main branch over and over into the same files.

Let’s take a quick step back and discuss what a branch is. If you go to the repo, this is the view of our project so far. We are on the main branch, and there are three commits. This is often drawn like this in Git, with circles representing each commit, and the commit hash underneath. The hash is unique to the commit inside of the repo and acts as a key or reference to a specific commit.

The arrow represents the diff, or changes between the two commits, like when we added two new recipes to our list. Because there is just one branch so far, which is main, the timeline is linear. This branch, main, is just a label for this commit. As we make commits over time, this label for the branch moves with it.

The default name that Git applies to the original branch you create when you create a new repo is named main.

Branches are very lightweight to create in Git and are one of the reasons why Git is so powerful compared to other version control systems. You can use them for feature development, bug fixes, and sometimes you might just want to experiment with an idea. If you don’t end up liking the idea or it doesn’t work out, no problem. You can just delete the branch.

So now that you know why you’d create a branch, let’s see how to do that with Git.

So far you and your friend have started creating a list of the recipes you want to put in your recipe book, but no actual recipes yet. You’ll start that by creating a new branch.

In the GitHub app, in the menu bar, go to Branch > New Branch. Here you can enter the name of your branch. Enter “Carnitas-Tacos-Recipe”. Click Create Branch.

In the UI, you can see now here, the current branch is our new branch. The GitHub app automatically switched us to the new branch we created.

GitHub gives you this nice handy notice that tells you your branch hasn’t been published to the remote repository. The branch you created is just on your local repo.

Open up the Recipes-Book repo. You’ll create a new file for the Carnitas Tacos recipe. Add a new file “carnitas-tacos.txt”. I’m just going to copy and paste some make-believe recipe.

Now go back to the GitHub app. Git detects that changes were made in our repo and that this new recipe file was added. So just like we did previously, add a commit message: “Created Carnitas Tacos recipe”.

Select the “Commit to Carnitas-Tacos-Recipe Branch” button.

At this point, you want to publish these changes to the remote so there’s a remote backup in case something happens to your local machine. And more importantly, so you can collaborate with others on this project.

Click the “Publish Branch” button.

Go to the History tab. You can see the history of your new commit.

Open a web browser and go to the Recipes-Book repo. Git detects that the “Carnitas-Tacos-Recipe” branch was pushed to the repo. Click the “Branch” link. This will list the branches you have in this repo. You can see the main branch and the new branch you just created. Click on the new branch, you can see here the “carnitas-tacos.txt” file you added.

Go back and click on “Commits”. This is the same view you saw in the GitHub app once you published your new branch to GitHub. Your local repo and remote repo are currently in sync with each other.

Switch the branch to main.

You can see that the new commit you added for the Carnitas Tacos recipe isn’t on the main branch. You’ll see how to fix that in the next video.