Git

Git: A Version Control Utility - The Basics



I have talked about the concept of version control in a previous post, so if you haven't read it yet please go check it out. In this post I am going to talk about Git and go over the usage of it by showing some useful Git commands.

Git meme

But first, what is Git?

Git is free and open source version control utility that is used by most people that write code on this planet if not all. Git is an essential tool for managing your source code, it allows tracking of different versions of your source code very easily. This means that if your code changes over time (which is what most likely will happen), using Git you will be able to visit different versions of your code, therefore, you can think of it as a source code time machine with many great features!

Git is great when it comes to having multiple people work on the same source code. Git tracks collaborative work on projects very efficiently.

Git meme

Without further ado, let's get down to business.

Once upon a time you wrote a nice little code. This code was actually a video game that can be installed on windows systems. One day, you decided to show your awesome video game off to your best friend. You were very excited because you wrote the software for it, end-to-end!

Your best friend loves the game and wants to install it on his own computer so he can play it too. What you do is you just give him a copy of your software on a USB flash drive and your friend now has a copy of it. But what happens next is that other friends hear about it and they all want to install it on their computers. What will you do? Are you going to buy more USB flash drives?

Well, you can use a version control system to host your software using Git.

You can open-source your software to the public so anyone can see the code and get a copy of it but can't do changes unless approved by you, the author.

Let's see how we can share our code publicly using Git.

Sharing code using Git

To start your code-sharing journey, first you need to decide on where you want to host your code. There are few version control systems options, such as GitHub, Bitbucket, or GitLab. Those all are code collaboration tools and may differ in pricing and services they provide depending on client and project needs, but Git-specific terms like commits, push, pull, fetch, merge and so forth (which we will explore next) are the same. Please note that you can use any of these options to make an account and host your code publicly for free.

Assuming that you have a GitHub account and signed in, on the main page in the left panel, click on the New button to create new repository.

Screenshot from GitHub

Next, type in a name for the repository and select Public. If you don't have a README file yet, I suggest you Initialize this repository with a README where you can explain to users how the code (video game) works. I would also suggest including a license, you know for lawyering up. Next hit the Create repository button and stop there.

In the next steps, I am assuming that you know how to use the command line interface and have already navigated to the project directory that you want to push to the new remote branch you just created on GitHub.

After creating the new repo on GitHub, you need to initialize Git in your existing project folder. This can be done by using the following command:

$ git init

Basically, this command is converting your existing, unversioned project to a Git repository. Next, you need to run the following command (which you can copy from the UI):

$ git remote add origin git@github.com:<user_name>/<repo_name>.git

This command creates a remote connection called origin pointing to the new repository you just created on GitHub. Don't forget to replace user_name with your user name and repo_name with the new repository name in the command above.

Now, if you run:

$ git status

you will notice that all the files are untracked and color coded in red. To track the files we need to do the following:

$ git add .

The dot (.) after the add means that we want to add all the files at once. Now, try the git status command again and see how all the files have turned to green. We just need to write a commit message before we do the last step which is pushing to the remote branch. To commit use the following command:

$ git commit -m "Initial commit"

You can type whatever you want between the two double quotes, as long as it is brief and clear. Now if you run the git status command, you will notice that all the files are gone and the branch is up-to-date (nothing to commit, working tree clean). But we need to push our code to the remote branch, so the last command is the following:

$ git push -u origin master

If you go on GitHub where you left off, refresh the page and you will see your code has been uploaded.

Now you can point anyone who wants a copy of your software to your Git repository. They can clone you project on their computers and use it freely as you wish of course.

Other useful Git commands

$ git pull which is used to fetch and download content from a remote repository and immediately update the local repository to match that content. This command is mostly used when two or more people are contributing to the same source code.
$ git merge used to merge two branches together. Useful when you want to merge, for instance, a feature that has been developed on a separate branch with the your local master branch.
$ git fetch this command downloads commits, files, and refs from a remote repository into your local repository. Fetching is what you do when you want to see what everybody else has been working on.
$ git diff this command is used to track the difference between the changes made on a file. Therefore you need to pass a file name (that has been changed) with this command.



Done


Leave a comment


Post


Comments

Be the first one to leave a comment!