This tutorial will teach you how to create Branches and push them on GitHub. You need to know the basic Commands of Git, which you can learn from Part I of my tutorial: An Intro to Git and GitHub for Beginners Part I (Tutorial).

Git Branching?

branch is essentially another copy of your repo that will allow you to isolate changes and leave the original copy untouched.

Git by default creates a branch called master. It’s not special in any way other than it’s the one created initially. We use branches to experiment and make edits before committing them to themaster branch.

Git is very flexible: you can have an indefinite number of branches active at the same time, and they can be developed independently until you want to merge one of them into another.

Branches are good for features!

What’s are features?

Can be something as big as adding a new section to a site or an app, to a small functionality.

For instance, if you want to add a new page to your website you can create a new branch just for that page without affecting the main part of the project. Once you’re done with the page, you can merge your changes from your branch into the master branch. When you create a new branch, Git keeps track of which commit your branch ‘branched’ off of, so it knows the history behind all the files.

Step 1: Create a new branch

Lets first see the branches that our repository has and our current branch by running git branch command

$ git branch
as the asterisk indicates, we are on the master branch

git branch: tells you what branches you have, and *indicates which branch you are currently on.

Create a new branch called gh-pages usinggit checkout -b <branch_name>command

$ git checkout -b gh-pages
see the message it is saying “switched to new branch gh-pages”

git checkout -b branch-name: the -b creates a new branch, and checkoutwill move you to that branch off of the master branch.

After running the above command, you can use the git branch command to confirm that your branch was created:

as the asterisk indicates, we are now on the gh-pages branch

When creating the new branch, that branch points to the latest commit made on the current branch. Run git log you’ll see the same log as the branch that you branched off.

When you create a branch off the master branch, you’re making a copy, or snapshot, of master as it was at that point in time

According to Andela best practices, it is better to keep your master branch clean. What I mean is you always branch off from the gh-pages branch and not from the master branch.

Let’s say you want to add a login page to your website. You have to create a new branch for that page and you have to branch off from the gh-pages branch and not the master branch. Once you are done with that branch, you can mergeyour changes from the branch you were working into the gh-pages branch.

Step 2: Create a branch for our login page

Create a new branch calledour login usinggit checkout -b <branch_name>command

$ git checkout -b ft-user-login-162561717
Make sure you are on the gh-pages branch

Don’t forget to run git branch to check your current branch.

Use git checkout branch_name command: switch to the branch you want.

According to Andela best practices, this is how you name your branches:

{story type}-{2-3 word summary}-{pivotal tracker id}

story-type – Indicates the context of the branch and should be one of:

  • ft == Feature
  • bg == Bug
  • ch == Chore

story-summary – Short 2-3 words summary about what the branch contains



This is where I got my `Story_ID` and `Story_Type~

Step 3: Create a new repository on GitHub

If you only want to keep track of your code locally, you don’t need to use GitHub. But if you want to work with a team, you can use GitHub to collaboratively modify the project’s code.

To create a new repo on GitHub, log in and go to the GitHub home page. You should see a+ buttonat the top right corner of the screen near you user photo.Click the + button and also click the New reposity.

After clicking the button, GitHub will ask you to name your repo and provide a brief description:

When you’re done filling out the information, press the Create repositorybutton to make your new repo.

GitHub is asking us if we want to create a new repo from scratch or if we want to add a repo we have created locally. In this case, since we’ve already created a new repo locally, we want to push that onto GitHub so lets follow the ‘….or push an existing repository from the command line’ section: copy the command from Github.

$ git checkout master
$ git remote add origin
$ git push -u origin master
Git will ask you your Github username and email after you run those commands
This is our repo on Github, after running those commands

The command git remote add <remote_repository's_URLadds a new remoterepository to the current repository.

remote is a copy(clone) of your repository, positioned on another machine.

The command git pushsends your local changes to your remote changes.

When you copy(clone) a remote repository to your local machine, git creates an alias for you. In nearly all cases this alias is called “origin.” It’s essentially shorthand for the remote repository’s URL. So, to push your local changes to the remote repository, you could’ve used either the command:

git push <alials> <remote_remote'URL> <branchname>


git push origin <branchname>

Step 4: Push a branch to GitHub

Now let’s push our gh-pages branch on Github

$ git checkout gh-pages
$ git push origin gh-pages
You don’t have to run git remote add command again since it is already set and you can it is saying the remote origin after running git remote

The command git remote-v list all the remote repositories and their urls associated with the current repository

Refresh your Github and click the branch button to see the new branch

Next, let’s add a login.html file to our ft-user-login-162561717 branch

$ git checkout ft-user-login-162561717
$ touch login.html
$ git status

Note: You can add the login.html file using your text editor.

Make sure you are in the ft-user-login branch

Next, let’s tell Git to track our login file by using the commandgit addand it to the Staging area

$ git add login.html
$ git commit -m '[starts #162561717] adds the login file'
Notice how I wrote my commit message this time differently
$ git commit -m '[starts #162561717] adds the login file'

[starts #162561717] what this does is it starts the story that is associated with the ID #162561717 and adds the commit message in the activity of the story. It also add the branch ft-user-login in the story. We will see this after we push the commit to Github.

Make sure your pivotal tracker is integrated with your Github repository.

If you don’t know know how to use the pivotal tracker, see this youtube link.

If you don’t know how to use pivotal tracker, use this tutorial Stuck with Pivotal Tracker? Get sorted here in 5 minutes.

See my story in the pivotal tracker is not started. because I didn’t push that commit to Github

Next, let’s push our commit and our new ft-user-login-162561717 branch to our Github repo

$  git push origin ft-user-login-162561717
I pushed the changes to ft-user-login branch on Github

To push changes onto a new branch on GitHub, you’ll want to run git push origin branchnameGitHub will automatically create the branch for you on the remote repository:

Notice when I clicked the ft-user-login branch my commits are now 2 and the branches are 3.
Notice our story moved from the icebox to the current iteration box and is now waiting to be finished.
Open the story to see the branch added and the commit message in the story activity,

Finally, add some HTML to your login file and commit the changes. Also, push them to Github.

$ git status
$ git add login.html
$ git commit -m '[finishes #162561717] adds the login form'
$ git push origin ft-user-login-162561717
Notice my commit message uses finishes this time instead of start.

[finishes #162561717] finishes the story and this should be added in the commit message only when you are done with that story. Otherwise, write your commit messages the normal way.

After pushing the commit to the Github repo, our story is now in waiting to be delivered and is finished.
see the green button at the right top telling us to compare and create a Pull Request.

Note: our ft-user-login branch is 2 commits ahead of our gh-pages-branchand we need to Merge those 2 commits to the gh-pages branches.

If you switch back to the gh-pages branch, you won’t see the login file you created in the ft-user-login branch, unless you merge the changes to the gh-pages.

as you can see, our gh-pages branch only has the hello.txt file.

View my next article in order to learn how to create a pull request and merge it.



Please enter your comment!
Please enter your name here