DEV Community

Cover image for Git – What is it and Why is Version Control Important – A Comprehensive Guide
Amo InvAnalysis
Amo InvAnalysis

Posted on

Git – What is it and Why is Version Control Important – A Comprehensive Guide

A Brief Overview on Git and GitHub
At some point, you may have come across or heard of Git. Everyone mentions it, and it seems to be a staple for software engineers, data engineers you name it. However, to understand what Git is, consider the following.
Any piece of code is bound to run into issues at some point, and in the midst of you trying to debug the underlying issue(s) you may introduce a breaking change into your code. What do I mean by this? Have you ever tried to fix your code but it only ended up worse? That is but one case. If it were a project in production, the consequences could be far worse.
Now consider this other case. You are part of a large team working on a single codebase. With everyone working on their part, locally, or put simply in their own isolated personal computers (PCs), some conflicts are bound to occur. One person or more may introduce breaking changes, or even overwrite what someone else has already done.
The answer to this, and more other issues, Git.
Think of Git as this open-source distributed version control system. With Git, a large team can easily collaborate on a project, and should there be any conflicts, Git can help pinpoint where exactly the conflict originated. In addition to this, with Git, each individual can access a copy of the entire codebase, work on their part, and upon completion or when necessary, merge or rather consolidate with the rest of the team’s work.
That is just a brief overview of what Git is. It helps with tracking changes in code no matter how insignificant, tracking who made what changes to the code and enabling collaboration in a codebase.
In addition to Git, you may also have heard of GitHub. So what is the difference? First off, Git is an open-source distributed version control software, and it is usually in the form of a command line based tool. Furthermore, Linus Torvalds developed it in 2005 as an answer to the numerous issues he was running into with the version control system used at the time when working on the operating system Linux.
GitHub on the other hand is a server based or web-based version of Git, with a better, more user-friendly graphic user interface. GitHub is a service owned and maintained by Microsoft, whereas Linux owns and maintains Git.
Another major difference between the two is that Git is free to use while GitHub is paid service. Users get to use GitHub at no cost for certain tiers, but advanced features or extra usage like with larger teams come at a cost.
There are many other web-based versions of Git like BitBucket, but GitHub is the most commonly used.
How to Push Code to GitHub
So how do you push code to GitHub? It sounds or seems hard, but it is easier than that really. Here are three major ways to push code to GitHub:

  1. Using terminal or a command line interface (CLI) like Git
  2. Via a graphical user interface (GUI) like GitHub desktop.
  3. Using the web-based GitHub For brevity’s sake, this article discusses how to push code to GitHub using terminal or a CLI tool like Git. Using Terminal or a CLI like Git Before you proceed, ensure you have met the following prerequisites: I. Git is installed on personal computer (PC) (Mac and Linux users have this installed by default, but if not, or if you’re a windows user you can quickly install by visiting https://git-scm.com/) II. You have a project on your PC (You can create something simple for this like an HTML file, nothing too overboard) III. Have a GitHub account (You can create this for free if you don’t have one at GitHub.com) That said, here is how to push code to GitHub using Git or terminal. Step One: Create a Repository on GitHub Navigate to your GitHub.com account and log in if you have not done so already. Look for + / plus sign on the top right corner of your browser, click on it and select “New Repository”


Give your new repository any name you would like but in practice this would be something along the lines of what your project is actually about. Once you are done, click on “Create repository”


Next, copy the repository uniform resource locator (URL) as so. The link should end with .git and https:// is recommended.


Step Two: Launch Git and Navigate to Your Local Project Folder
Launch Git on your device and navigate to the relevant project directory. In this case you would use the cd or change directory command.

cd path/to/your/project
Enter fullscreen mode Exit fullscreen mode

Check if you are in the correct directory by typing in this command. If you are still not there you can reference this handy resource to figure out how to navigate to the project directory.

pwd
Enter fullscreen mode Exit fullscreen mode

Step Three: Stage Your Local Project
Once you are in the project folder or directory key in this command and hit enter.

git init
Enter fullscreen mode Exit fullscreen mode

This is meant to tell Git, hey, this is a new local Git repository. Therefore, it can start taking snapshots of whatever you do in the folder.
Next, type this in and hit enter

git add .
Enter fullscreen mode Exit fullscreen mode

This command stages or in simpler terms readies your files for movement from your local computer to GitHub. More specifically, the command readies all files in the current folder or directory you are working on. Git already knows you have a certain file in your working directory, but by using this command, you’re now telling Git to start actively take not of the file(s) you have specified.
In case you want to move an individual file you can use the following command with the “filename.txt” being a placeholder for the actual name of the file you wish to upload.

git add filename.txt
Enter fullscreen mode Exit fullscreen mode

Finally, it’s time to commit what you’ve staged to your local device’s repository. Think of this as some sort of timestamp with a description. Type the following command and hit enter.

git commit -m "My first commit"
Enter fullscreen mode Exit fullscreen mode

Step Four: Connect the Local to the Remote Repository, then Push to Remote
Now to the final bit.
Type the following into your terminal, and hit enter after each command.

git remote add origin https://github.com/USERNAME/myfirstrepo.git
Enter fullscreen mode Exit fullscreen mode

Then,

git push -u origin main
Enter fullscreen mode Exit fullscreen mode

The first command tells Git on your local computer where you want to push the code to, and the last one, well, it tells Git to now push the code to where you pointed out in the first command.
With the last command, you may have to complete authentication but this is usually a breeze. Once you’re done, you should be able to see the code from your local repository on your GitHub repository.
How to Pull Code from GitHub
First, navigate to your GitHub account and open the repository that you want to pull code from.
Next, look for a green “< > Code” button as shown below and click on it.


Copy the URL, either option would do but HTTPS is much easier if you’re doing it for the first time.


Next, launch your terminal or Git and navigate to the directory or folder you’re working with for the project.
Lastly key in the following command, paste the URL you copied in the placeholder for the URL and hit enter. (Do not include the “ ” in your command)

git clone "pasted-url"
Enter fullscreen mode Exit fullscreen mode

For subsequent pulling, you will only need to navigate to the directory you used above and run this command.

git pull
Enter fullscreen mode Exit fullscreen mode

That is how easy it is to pull code from GitHub to your local computer.
How to Track Changes using Git
If there is one thing Git is exceptionally good at, it’s tracking changes. Tracking changes using Git typically follows the following cycle: Modify-add-commit.
That said; let us try tracking changes using Git practically.
In this instance, let’s assume you’re trying to create a simple car driving manual and we’ll track changes using Git.
First thing let’s create a directory named “driving_tutorial” by running the following command.

mkdir driving_tutorial
Enter fullscreen mode Exit fullscreen mode

Next, navigate to the directory you have just created.

cd driving_tutorial/
Enter fullscreen mode Exit fullscreen mode

Now create a file named manual_car.md, type the following text into it “Manual car”, and in the next line, add “Key Instructions.” This example uses Vi as the editor, but you can use Nano, or any other you prefer.

vi manual_car.md
Enter fullscreen mode Exit fullscreen mode

Now time to track changes. First we’ll try to if you created the file correctly, then try to check the contents of the file and lastly, you’ll check if Git is taking note of any changes to files in your working directory.
So, start with “ls” which lists current files in the working directory
ls

Then, “cat” which reveals the contents of the subject file

cat manual_car.md
Enter fullscreen mode Exit fullscreen mode

And lastly,

git status
Enter fullscreen mode Exit fullscreen mode

The output from this last command tells us Git is not tracking the file. To resolve this, run this command, which now tells Git to start tracking changes. (“git add .” would have worked similarly, but since we know the file we need to track the command below would do)

git add manual_car.md
Enter fullscreen mode Exit fullscreen mode

Once you run the command, now check the status again with git status
This what you should see.


Now that Git knows it has to track changes to files in your working directory, the next step would be record all this in a sort of timestamp referred to as a commit. Just like how you would save your video game’s progress at a certain checkpoint, or whenever you feel like it. (Remember, the commit message can be anything but ensure it is brief and adequately descriptive of what you’re trying to do with what you’re committing).
To do this, run the following command

git commit -m "Create a handy manual for new drivers"
Enter fullscreen mode Exit fullscreen mode

Now check again on the status of your file with git status


If you get this, or something similarly, you’re well on track.
Now lastly, run the following command

git log
Enter fullscreen mode Exit fullscreen mode


With that, you’ve managed to track changes with Git. However, in practice you won’t be limited to this. Remember, it’s a constant “modify-add-commit” cycle with Git, and you just have to keep doing it to ensure Git keeps track of everything you do.

Top comments (0)