What is Version Control ?

Lorenzo Piombini
7 min readOct 4, 2020

When I started this journey I had no clue of what Version Control was . what is It for ? and why everyone is so serious about that ?

some days ago I was working on my Calc App in Xcode 12, and after hours of creating and working with the view controller, I did something weird, perhaps I hit a button, and everything was gone, I deleted everything by mistake. two seconds, maybe less to lose hours, at least four hours of work. probably If I used a Version Control I would have been able to recover my project. The purpose of this system is make your life easier. It keeps record of changes to your code over time, in this way, you can go back to a previous version of your project that maybe worked better, you can update your App without impairing your program, when you use this software tool debugging your code it will be easier, for example you are building your own project and it works so far, tomorrow you will make some change and your App will crash! OMG what happened !!?? why ?? If you have been using Version control you would know where the bad code might start, what if you have thousands line of code ??? this is a lifesaving tool, and it comes into play, also when you are not the only programmer, but you are working in a team. You are in charge of some features of the program and you will make some changes here and there, and so will your team, and it will happen at the same time, Version control will help you to organize the work better and in a clear way avoiding tons of headaches. At the beginning of the article I told you about a mistake I made, I wasn’t using Version Control because I though it wasn’t necessary, I would have saved lots of hours if I worked with it.

How does Version control work ?

There is system, an open source software able to track changes in source code when a software is in a developing stage, called Git, which was was developed by Linus Torvalds, who created the Linux operating system, back in 2005. You can use it in on a Mac with the terminal shell, if you are using Window operating system, I used the GitHub desktop app which is really easy to use. Using a mac I prefer to use Git with the Terminal, I think it is just cool!! Other than that what really matters is to have Version Control in place.

Git is not just about programming, you can have your Version control on a document .txt or word doc. Let’s say you are a writer, you will have a benefit from using Git, detecting changes in your file will help you shaping your book and your story easily. Basically you don’t have to be a programmer to use Git, let’s see some example.

we are going to create a txt file, first thing to do open up your terminal, once you are in the command shell, you are in your main folder, I personally don’t like to work there and I usually change directory with the command cd, which means change directory, and in this case we want to go into our desktop folder, which contains the file you see on your desktop like so:

now we are going to create a folder call example with the command mkdir :

now we have to move into the example folder we just created and we will create a file with the command touch, as you can see in the pic below we change folder with cd and we went ahead creating a file called someText :

now if you list the item in this folder (example) with the command ls you will see your file someText.txt listed in the item:

as you noticed already I typed in another line : vi someText.txt I did so because i do not have a text editor software on my laptop and I do not want one for now, so if you type in the same line you will enter the file from the terminal shell which is really cool according to me and you can type your text from the shell, type the line and hit return, you should see this :

now hit the letter I on your keyboard to enable the insert mode, type right in the file something like :

Ciao My name is ( your name ) and I like Pizza.

to save this line you have to press ESC on your keyboard, and then type a : and w and q like so ( this is the left hand side of your shell) :

if it looks like that, hit return and you will have returned to the main terminal shell: now that we have our file with a line of text, we still have to put version control in place, be sure to be in your example folder and type git status :

you should see also the fatal message, do not panic, it is pretty easy to fix, type git init :

ok so now we are on the right path, let`s see our git status now, which message will the shell throw ?

uor computer it is saying that we are on branch master and there are no commits yet, we do have a untracked file, which is our someText.txt, the terminal suggest to use git add to track the file, I will follow the :

you could have typed also git add someText.txt, git add -A will add any files in your folder, if you specify just the name of the file you add only that file, which is fine for our example there is also this option : git add . it`ll add all the untracked files in the folder. now you should get this message :

now git is tracking someText! the software it will recognize any changes to your file. the git repository that we created with the command git init is still empty, we have to commit our file to the repository with git commit -m and a message in double quotes :

after committing the file i checked the git status, and you see that we do not have anything to commit. and now retype vi someText.text to make some changes to our file, again press I on the keyboard yo enable the insert mode and write something like : I forgot ! I love gelato too ..

once you are back in the terminal window type the git status command to see what message you’ll get

this is real magic, now git is detecting changes on our file, and this we will help us to work easily and there will be a lot of time saved! what’s going to happen now ? if we like the new changes we can commit them to the repository as we did before, git add -A , git commit -m “new approved changes “ or whatever message you want to put there. I want to recap that to track a file you use git add if you create new files you will have to use git add to track changes in them, and git status will check wether change occurred or if it has to be committed . how about checking ours commits over time ? command git log:

what if you do not like gelato anymore ? and you know love just pizza ??? you can go back at that time using the commit number and a new git command,

if you want to go back and like just pizza, you can say git checkout 2d5d8b2 ( the first 7 characters of your commit number ID):

see the last line ? type it in your terminal and I want you to see that the line stating that you love also gelato is GONE!! this is really powerful, when you will have thousands of line of code( even hundreds are enough to go crazy ) and your app is crashing out of the blue. you will know how to go back when everything worked and start over with the new code.

all of this is just the basic to work with git, and there is a lot more to learn, but this is still a lot to process at the beginning, so take it easy and I hope it is helpful to move the first steps in git.

Thanks for reading, I’ll see you soon…

--

--