Git in a plain language

Femi Oni
8 min readAug 26, 2017
Code factory

Imagine you work in a the factory pictured above and the factory have several product lines; lets say milkshakes and chocolate bars(💛, right?). Each product line have its own production area and storage. Each product produced are carefully placed in a well labeled pack with information like expiry date, manufacture date, serial number, size and any other useful information clearly written on it. Each pack is then placed is safe boxes of different sizes. These boxes are just to move the products from the product area to the storage. Once in the storage products are moved the the boxes into one big container. The container with products in it is then moved to the retail store where the products are displayed and sold.

Hold on to the picture i just painted. Factory, Product line, Products, Packs, Boxes, Container, Retail outlet, Display….keep them in mind as we see what Git is about.

Git is a Distributed Version Control System for managing how code is added to software. Software development usually involve more than one developer, Git help you track, among many other things, what is being added, by who, and why.

It is also usual when we develop software to have a central place where we leave what we have worked on so that others working with it can see what we have done and also leave their work there for us to see and use. This central place is called a Repository(Repo for short). This is the container in our factory illustration. What then is the factory, you ask?

People have worked to build services that you can use as your repos so you don’t have to worry about managing and safeguarding your code, they do for you(for free, most of the time). GitHub , GitLab are the most popular examples of such services. These are the factories! And there, you can have as many repos as you need, milkshake repo, chocolate bar repo, any repo you need. the rest of the examples in this article will be about GitHub but its the same flow no matter what service you choose to use. And YES we are building a milkshake software!!

Installing Git

First you need to install Git on your computer

Windows OS: download the installer from https://git-for-windows.github.io/, follow the installation wizard

Mac OS: click here to download the installer and follow the installation wizard

Linux(Ubuntu): you will need to enter commands

  1. sudo apt-get update
  2. sudo apt-get install git

the in you command prompt/terminal run git --version to make sure it is installed successfully.

Creating a Repository

So you have been charged with building a new product in the factory, you need to prepare your storage container and your development area(henceforth, we will call this ‘development environment’ as it is popularly call). Go to GitHub and register a profile. Once registered, you need to setup a new repo.

  1. Click on “New” button shown below
button for creating a new repository on GitHub

2. Fill out the form

The ‘Public’, ‘Private’ options means, do you want anybody who comes to this factory to be able to see what you have stored in this container? If yes, select ‘Public’. if you would prefer that only the people you have invited are able to see the content of the container, select ‘Private’. There, you have an empty product container.

Supposing there are people already working to make the product and the store already exists, you won’t need to bother with the above step, just prepare the corner where you will work from, Clone the repo.

Cloning

Now that you have your store, you need to prepare a development environment to do your work. on GitHub, go to the repo, it should look like the image below on GitHub

Click on the ‘Clone or Download’ button and copy the address shown. Then open your command prompt, move the pointer to a folder where you ll like your project folder to be, then run git clone https://github.com/freefony/milkshake.git . Replace the address with the one you copied before running the command.

when cloning is done, you will have a new folder ‘milkshake’ created for you by Git, your code will go into this new folder. congrats! You are now ready for work.

Commits

Commits are labels to the changes you have made, it tells you and others working on the project what you changes are supposed to fix or add to the source code. Lets say you found a bug that was preventing users from login, you wrote a code to fix it, then you need to label what you have done i.e commit your code. To do this enter the command git add login.html then git commit-m “fixed a bug that was preventing users from login" . When you make a commit, Git adds some information to it too, they include Author of the commit, date and time the commit was made and a unique hash of 40 characters as this id of the commit. Each commit is a step in the progress you have made building you app, so and git carefully arrange your work with this steps so you can go back as see what was added at each step, by who and when. As such it is very important that these steps, our commits, are identified uniquely. Do worry Git does that for you internally and to see the steps so far, a list of all the commits previously made to the project, use git log and press ‘q’ button on your keyboard when you are done.

Branch

So you have made 50 bottles of milkshake, can you move them safely in your hands without dropping them? NOOOOOO! You need a box and a cart you can arrange them in and move them safely. Is it wise to place them on top of another already filled box to go and find an empty box to arrange them? Also NO! So the first thing you want to do before writing codes is make a branch(i.e get am empty box close by and arrange the bottles into it as we work) with this command git branch fix-login-bug . That create the branch but won’t open it for you. To open the branch do git checkout fix-login-bug . You can use a shortcut instead git checkout -b fix-login-bug this will create the branch and open it for you. Start filling with commits.

Push

Now you have arranged you bottles into the box, you carry the box, or push the cart to the store. With git you use the command git push origin fix-login-bug . If your push was successful, you can got to the remote repository on GitHub or GitLab or wherever the main source code is hosted and you will see ‘fix-login-bug’ among the list of branches, and you will a button to make a ‘pull request’ on GitHub or ‘merge request’ on GitLab, click this button, the service will compare the changes in your branch to what is already existing and see if the changes you have made and see if your commit id(serial number) is up-to-date, then it will allow you submit your request. Otherwise, if someone else have made changes to the source code after you started working and their change request have been accepted and they did something similar to what you have done, Git needs to know if it should accept some changes and reject others, or if it should accept all changes. In that case you will be asked to update your branch to reflect the new changes before making the request. This is called conflict resolution.

Fetch/Pull

Others have pushed changes to the repo and these changes have been merged, you need to update your local copy of the repo to have it up-to-date and avoid conflict the next time you send in your work. Use git fetch origin to bring copy down the changes and git merge origin/develop to update your branch. You can do git pull origin as a shortcut instead of fetch and merge like we did before.

Fork

Lastly, lets say you are not a staff member of this factory, but a contractor with your own factory where you make products and supply them to this factory. in Git, your own factory will be called a ‘Fork’ . You push your branches to this Fork (it is also a repo like any other, copied from the main repo). and when your are satisfied with what you have done, you make a change request to the main repo.

The image above is of my repo for ‘free-programming-books’ that i forked from another repo ‘EbookFoundation/free-programming-books’ and you can see, 23,100 others forked it too.

Now think about what could happen to your contract with the factory depending on what clauses where inserted:

  1. They may terminate your contract stop receiving supplies from you
  2. They may not like a batch of product you supplied and reject it
  3. You may decide to look for other buyers for some batch(es) you have made
  4. Or, you could terminate the contract with them, open your own store and become a competing brand to them.

Same could happen with Git; Your change request may be rejected if they feel what you have done is not needed on the project or is not of high quality. Or you may choose not to send your changes to the main repo anymore and just develop your own project. Your Forked repo now becomes the main repo and changes are merged there henceforth. That’s why it’s called a fork, heads goes different ways.

This article is not to make you become a Git pro, but to help you catch the concepts. The next article will put some of the concepts to real live work. see you there 🤓

If you have questions, suggestions or queries, please leave them in the comments.

Thank you Patricia Garcia for the sanitation work on this and other articles i have written. Gracias!

--

--

Femi Oni

Former FrontEnd @Engineer Field Intelligence(@vonfield), Currently @Andela. Special interest in PWAs