Learning How to Git: Create Git Branch

Haydar Ali Ismail
3 min readJan 27, 2017

--

One of the killer features in Git is the existence of the branch system. Branch allowing us to create a different state of the files and folders. If you still wondering how branching works you can check my previous story here.

Create Git Branch

Firstly, let’s see the branches we have using git branch.

Listing all branches

Okay, so currently we only have a branch called master. Let’s create another branch called dev. To create a new branch we can use the git checkout -b <name-of-new-branch>. The checkout command is actually pretty tricky, we will talk about it in future stories. So, because I want to create a branch called dev then my command would be git checkout -b dev.

Creating a new branch called `dev`

Hooray, now if you check using git branch, we will see our new branch.

The new branch already shows up

The asterisk (*) symbol represents the current branch you currently at or usually, it is also represented at the right of the current working directory path (notice the (dev) at the upper right corner).

Working on Your Branch

Now let’s say we are working on this branch and creating commit(s). After creating all of those commit(s), we can push the branch to the remote repository by using git push origin <name-of-branch>. So, because we are currently on the dev branch then we need to run git push origin dev.

Now let’s check what happened on our remote repository.

Git Branch Network

We can see that the dev branch is now one step ahead of the master branch. This means that anything happened at to dev branch doesn’t affect the master branch.

Move to Other Branch

Let’s say we have finished with our dev branch, how we can move to other branches? It’s similar to the previous command, but we omit the -b parameter. So we would have git checkout <name-of-branch>. In my case, I want to go back to master branch, so I would use git checkout master.

Switching back to `master` branch

If you check the file or the log you will found that the commit from the other branch will not exist here.

Wrap Up

I think this already covers the introduction to git branch system. On the next story, we are going to talk about merging those branches. I hope it’s helpful and thanks for reading.

References

--

--