Power of GitHub, Branches and Tracking

Fanzhong Zeng
6 min readDec 13, 2019

--

What is GitHub?

GitHub is a company that offers distributed version control (which is a form of version control in which the complete codebase, including its full history, is mirrored on every developer’s computer) and a lot of additional features like having a cloud to upload your codes, branches, and even a basic wiki page for every project. The basic GitHub you can think of it like Skyrim’s save files. You can do git add --all (which is basically telling the project that you want to save all the new files you added/changed), and then git commit -m “commit_msg"(which is saving all the files that you added)

git add --all
git commit -m “commit_msg”

is the same as if you are doing a new local save file(save as, for Microsoft word users) on your computer. If you saved enough, now you can go back and forth between all your save files anytime you want.

Having multiple save files, so let’s say the next time you mess up the code and end up breaking everything (like in Skyrim dragon/vampire killing everyone in town), you would just load an earlier save file and pretend nothing ever happened.

However, the main feature of this isn’t just the saving and loading. You can upload the file by doing git push origin branch_name . You can think of this as uploading your Skyrim save files to the google drive. Where now, anyone can download it and start coding/playing with your current files. Let’s say you downloaded this project (by cloning it git clone project_url) you can always update it by doing git pull --all. Which basically download the newest data that’s on the server.

This by itself is already powerful enough, but that’s not all GitHub is capable of. Today I will be showing GitHub branches, and tracking.

GitHub Branches

When someone does git checkout -b new_branch_name they are making a new branch based on the current data of the current branch they are on. I can do whatever I want on this new branch, and the old/main branch (aka master branch) will not know about it and he can continue doing his own things.

The -b option is used for creating a new branch, if I just want to switch over to another already existing branch I can just do git checkout existing_branch_name to switch over to that branch. Now an important thing to make sure of is that you don’t have any changes that you didn’t save. If you did something and didn’t save, when you switch over to the new branch, that code will follow you along. Make sure before you switch, you do git status and check to make sure that you get the following message

nothing to commit, working tree clean

Having multiple branches is like having multiple timelines, a parallel world where a what if things went differently in another. Like inside Skyrim file, you might rise the one handed weapon tree completely different from someone else.

What if we have the power to combine these worlds, like merging them together to get the best (sometimes the worse) of both worlds? Here comes in GitHub pull request (not to be confused with git pull --all which is just downloading the latest data) and merge requests.

GitHub Pull and Merges

Merging is simply anything that I did in my branch, that I edited and saved, now I want another branch to have everything that I did. To merge we must first create a pull request. This can be done in your cmd/terminal or on the website, you can check out how to do that by clicking here. Pull request is simply telling the owner of this repository (project) that you want to merge (combine) your files so their branch have everything that you do in your branch.

Then the owner of the project/branch can be merge your branches together so now you both have your own changes.

Now master save 6 have the new changes from new_branch

So let’s say in Skyrim I cloned the owner’s (master) save file and created a new branch. He went to level up his One Handed Sword skill, while I leveled up Archery skill tree. When I merge my branch with his, now the new character has both the One Handed Sword and the Archery Skill tree leveled up.

This is really powerful, but what if we both decided to level up the One Handed Sword skill tree? However we raised it differently. Then comes in merge conflict, and resolving that can be a pain. As you both have to come into a conclusion of what to keep and what to remove. More details regarding that can be found here.

However as you notice, the new branches once created it has no way of knowing what happens to the old branch and the old branch has no way of knowing what happened to the new branch until they got merged. There is a way that allows the new branch to keep up with all the updates from the old branch, and this is called tracking.

GitHub Branch Tracking

Tracking is almost like a parasite. Think of it like this, you took the owner’s (master) save file, and created your own (checkout into a new branch). And every time the owner levels up and raises a skill or gains a new time, you get everything that the owner just gained. Yet anything that you did, the owner doesn’t know about it, until you decide that you want to merge with the owner.

git checkout -b branch_name --track origin/tracking_branch_name

The above code is how to setup tracking of a branch. Now unfortunately things don’t get updated by themself. To get the latest data you will have to pull the latest data by doing git pull --all every time. But it’ll automatically add the files and changes that the master did into your own branch.

As you can see with this the pro being you can get everything that the master is working on while keep doing your own things. But the downside is that if the master touches a file or changes a part of the code that you are using or working on, it can completely break your feature.

But knowing how branches work, and how you can use it will make any project look cleaner and easier to keep track of what’s going on.

Conclusion

Now you should know GitHub add/commits, push/pull from the cloud, branches and how to set up tracking should you need it. With all this I hope you get some understanding and know how to starting using GitHub properly in your own projects.

--

--

Fanzhong Zeng
Fanzhong Zeng

No responses yet