7 minutes
Version control
Written by: Karthik.
Hello my fellow wizards, hope you are making the most of your quarantine season, its been a boring ride just sitting at home and doing nothing, So here i am saving you from the clutches of boredom with an interesting post.
What is version control and why do you need it?
From the title, Version Control, you might have guessed the context of this post and your right(dont get too excited, cause you’re partially right).
Whenever you are developing something may that be a software or a website or just some project that you have been working on with some other people, you may have come across many problems that relate to keeping track of your changes and contributions to the project. When these changes and contributions are continuous you might as well lose track of what needs to be done and what is currently going on with the project, in other words you might get confused and loose your head, to avoid this confusion we need to keep track of the changes that we make and that is what version control does.
Version control is a component that keeps track of all the changes that you make to a specific file or to a set of files. This component is treated more as a necessity than a tool for many companies as it makes their work easier. It can be considered as a book or a database that keeps records of different changes that you make, it contains all the changes and historical edits of your project, so if you mess something up ( due to your ineptitude (▀̿Ĺ̯▀̿ ̿) ) you can always start from the previous versions instead of starting from the beginning(Thank Version Control for this).
There are many version control systems but the most popular right now is GIT, and guess what?? Tech giants like Google, Twitter, Facebook, Netflix and many more make use of GIT. The main thing that puts GIT on top of others is the ability to perform operations locally, nearly all operations are done locally and this makes git faster and better. GIT lets you share your changes and contributions with your collegues and it even lets you clone the work of other people in order to make changes to their work.
Git allows you to have multiple branches of a single repositary and these branches can be independent of each other. What if you have many repositaries and now its difficult to keep track of these repositories?? GITHUB has you covered.Github is a GIT repositary hosting service whereas GIT is a command line tool. It provides a web-based graphical interface for your repositaries and all the changes that you make using GIT can be viewed on GITHUB too. It has many other features and you can check out over here.
Some basic GIT commands.
Having discussed what git is , lets jump into how to use it. First of all you need to download the latest version of git from here or if you already have git installed you can get the latest version by cloning the git repositary
git clone https://github.com/git/git
after setting up the GIT tool you can head over to GITHUB and create a new account if you dont have one and once you have an account created you can go ahead and create a new repositary and give it a name.
After creating a new repositary, It should look something like this:
It might be a bit overwhelming at first but once you start using it you’ll get the hang of it.
The first command that we will be using is git clone and it is a pretty basic one,what it does is it saves a copy of the specified repositary on your local machine so that you can make changes locally and then once you’re satisfied with the changes you can push it to GITHUB.
So now go to the repositary that you just created and copy the specified url of the repositary.
Open up your terminal and cd to where you want the repositary to be cloned and after that all you need to do is
git clone https://github.com/orangeappleak/test.git
Cloning into 'test'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
It shows a warning saying that we have cloned an empty repositary and its true but nothing to worry about, now you can see that the repositary has been cloned or copied to the specified directory.
Now head over to the directory where the cloned repositary exists and create a new file, I am going to create a html file but you can create any type of file.
Our github repositary is still empty, so lets add the file that we just created to our GITHUB repo. Here we make use of the command git add. This command informs GIT which files are going to be added to the repo(short for repositary).
git add hello.html
hello.html is the name of my file, you need to specify the name of your file while executing the above command. So now GIT is aware of which files to add to the repo, now lets save the changes and add a short message in order to recognize what changes we have made. To do this we use the git commit command with a message flag as follows.
git commit -m "added a new line to the file"
[master (root-commit) 0ac0142] added a new line
1 file changed, 10 insertions(+)
create mode 100644 hello.html
The message can be anything relating to the changes that you make, it makes it easier to recognize what changes have been made at a glance. The final thing we need to do is to puch the file we created to our github repo. We make use of git push command in order to push the file or the changes that we made to our github repo.
git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 361 bytes | 361.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/orangeappleak/test.git
* [new branch] master -> master
After all the above steps or commands are executed you can head over to your repositaries on github and now you can see that the file that you created on your local machine is added to your github repositary along with the short message. Now the people working along with you can see what changes you have made, they can also clone the repo and make changes to your file and then push it back to your repositary. In this way you can keep track of all your changes and versions using git.
Now let us say that someone has made changes on your github repositary using the inbuilt edit mode that github provides, these changes are not reflected on your local machine. If we want to update the local files to the changes made on our github repo we make use of the git pull command. This command updates all the files on your local machine as per the changes made on your github repositary.
Another useful command is the git log command, it lists all the changes made by you as well as other people.
git log
commit 0ac014267063ae3ae2a7a7c0185e72126e7a563f (HEAD -> master, origin/master)
Author: orangeappleak <email@gmail.com>
Date: Thu Apr 16 15:55:38 2020 +0530
added a new line
So if you messed up and want to restore to the previous changes you can make use of the git reset [commit] where commit is the unique identification number (hashcode) used by github, the commit can be obtained from the log.
I have commited some new changes and then moved back to the previous version as follows:
D:\projects\test_websites\test>git add *
D:\projects\test_websites\test>git commit -m "added a new heading"
[master 4fe0c68] added a new heading
1 file changed, 1 insertion(+)
D:\projects\test_websites\test>git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 323 bytes | 323.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/orangeappleak/test.git
0ac0142..4fe0c68 master -> master
D:\projects\test_websites\test>git log
commit 4fe0c6813c75f43ec9c270036abbbd8bc0d50f1d (HEAD -> master, origin/master)
Author: orangeappleak <appannagarikarthik.123@gmail.com>
Date: Thu Apr 16 16:36:38 2020 +0530
added a new heading
commit 0ac014267063ae3ae2a7a7c0185e72126e7a563f
Author: orangeappleak <appannagarikarthik.123@gmail.com>
Date: Thu Apr 16 15:55:38 2020 +0530
added a new line
D:\projects\test_websites\test>git reset --hard 0ac014267
HEAD is now at 0ac0142 added a new line