Git and Github: A Simple Guide to Keeping Your Master Branch Safe by Using a Separate Servant Branch

Yakov Kiffel
Aug 25, 2017 · 5 min read

So you’ve heard of Github, and you know that you can use it to collaborate on code with other people and store the code online. But what are branches and how can you use them to keep a master version of your code safe while you write new code in the working version?

Step 1: Make new repository and clone it within a directory on your computer

First go to github.com and make a new repository. Then click on the Clone or download button and copy the text inside. Go to your terminal, navigate to the directory of your choice, and enter git clone <copied text> . In my case, it was git clone git@github.com:yakovkiff/github-games.git.

At this point, you’ve got a local copy of the repository on your computer, stored as the directory github-games, which is located within whatever directory you ran git clone in. You’re still in that directory, so now cd into github-games.

Step 2: Understanding Local and Remote

Enter git status in your terminal and you’ll see:

On branch masterYour branch is up-to-date with 'origin/master'.nothing to commit, working tree clean

Any new Github repository has one version, or branch: master. The cloned copy of the repository stored on your computer also has one branch, and that branch is also master. How do we differentiate between two the two branches named master? We call the branch stored on your computer the local branch, and we call the branch stored on Github the remote branch.

There’s a good reason that we use the same name to refer to both branches. We will be pushing code (git push) from the local master to the remote master, essentially updating the remote with changes to the code that have been made locally. We will be pulling code (git pull)from the remote master to the local master, essentially updating the local branch with changes made by someone else that have been stored on the remote. So the pair of local and remote master branches are like a team, sending stuff back and forth to each other.

We are not limited to using the remote master branch as the remote for our local master branch. We could theoretically change the remote for our local master branch to another branch or repository that exists on Github — say other-remote-repository. In that case, if we pushed from our local master branch, the code would be pushed to other-remote-repository, not to the remote master branch.

What about origin? Origin is a shortcut name that Github creates for the repository we cloned from. So to recap, in terms of accessing stuff from our terminal, origin is the remote repository; master is the name of the branch of origin, which is a remote branch; and master is also the name of the branch of our local copy.

Step 3: Pushing some new code from local to remote

Now create a new file inside your local github-games directory, write some code in the file, and save. Then, in your terminal in the github-games directory, enter git add ., which stages new and modified files, then git commit -m “first commit” , which tracks those changes. Then enter git push, updating the remote master with your local tracked changes. You’ll see master -> master , which indicates that you’ve pushed from a (local) branch named master to a (remote) branch named master.

If you now check the repository on Github, you will see the file you created along with whatever code you wrote in it.

Step 4: Creating a second local branch

Try entering git branch in terminal. You should see * master , indicating that your local git repository has one branch, master, and that you are in that branch.

We said before that a branch is like a version of the repository. In fact, we can create a new branch so as to have a second version of the repository. We can keep our master branch as our safe version of the code which we know works, and we can use the second branch (which I’ll call servant ) to write new, experimental, buggy code.

We’re going to want a remote servant branch as well. In total we’ll have four branches: two remotes and two locals. Master and master will send changes back and forth, and servant and servant will send changes back and forth. In addition, when we want to save finished, non-buggy changes, we will send the changes from local servant to local master and then from local master to remote master.

First, we need to create our servant branches and update them with what we’ve already got in master.

Enter git branch servant to create the servant branch. Then enter git branch to see:

* masterservant

The star indicates that we are currently in master and not in servant. To switch branches, enter git checkout servant. If you enter git branch again, you’ll now see:

master* servant

Our local servant branch currently has whatever code was committed to master when we created servant. Now enter git push, creating a remote servant branch and pushing the code from local servant to remote servant. On Github, you should be able to see the remote servant branch.

A Word on Commits

When you git commit, you are committing changes in whatever branch you’re currently in. So make sure you’re in the branch you want! In our case, we’re going to want to commit all new changes in servant, because we’ll be first updating servant and then

Step 5: Updating master from servant

Now we will (1) commit new code to local servant, (2) push it to remote servant, (3) merge local master with local servant, and (4) push the code from local master to remote master.

First write some new code in your file and save it. In terminal, make sure you are in the servant branch and enter git add . and git commit -m “first commit to servant” . Push to remote servant.

Switch to master with git checkout master. Enter git merge servant , updating master with the commit from servant. Then enter git push, pushing the code from local master to remote master.

Now you should be able to see the new code on Github from both the master and servant branches. You’re done!

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade