Git Fundamentals – Getting started with Git and GitHub

Hello everyone. It has been about two weeks since my last post and I want to firstly take the opportunity to apologize for not posting last week. But, I am gonna rectify that by posting more regularly from now onwards.

So let us get started with this week’s topic. I was wondering what I could talk about that could be informative for all aspiring programmers out there, and fortunately one of our regular blog viewer and my good friend, Aditi, suggested a post explaining the basics of git and how to get started with git and start using git to manage their projects effectively.

I personally believe that git is is one of the most important skills to master for all software programmers out there. And obviously there is so much information out there on git which at times may feel too overwhelming and this is exactly what discourages beginners from exploring and keeps them from building their skills.

Now in this post my main goal is not to teach you everything there is to know about git, but mostly to teach you what git is, why is it necessary and how to get started with it. So let us get right into it.

What is Git ?

So the definition of git on the internet is that it is a version control system. Now obviously to someone who is new, this definition may not seem useful to understand, and there is no point to learning something if that process does not involve truly understanding what you are learning or trying to learn. So let us break down the definition to understand what it is. Git’s function is to efficiently manage your project by keeping track of your progress and in doing so it helps you save versions of your project. The reason why this is done is so that in the case of you losing your progress due to any unforeseen circumstances, you can still access specific versions of your project and retrieve them, so that you can continue working without losing your progress.

Why is it necessary to know Git ?

The idea behind git is to keep backing up your progress as you proceed with your program. The reason why it is important to know git is precisely what the function of git is. In the unlikely and unfortunate event of you losing out all your data because of any given reason, if your project wasn’t backed up using some version control system, you could lose all the progress you might have had. This is not particularly encouraged in the industry and it is highly recommended to keep backing up your progress using git or some other version control system. The way git works is also essential to why to use git. This is because the way git stores your progress is not by storing the difference in your code or your project, but by storing snapshots of what your code looks like at that point in time. As a student one might try and find a way around using git by using other ways to keep track of their progress, but git is extremely essential to learn as you proceed with your software engineering career. The earlier you get acquainted and get some hands on experience with it, the faster you will be able to efficiently develop more programs and applications

Why use Git, and not any other version control system ?

Well almost any answer I provide for this question, I cannot promise that it won’t be biased. This is because I haven’t had any experience with any other version control systems like SVN or mercurial. I think this post does a way better job of explaining about how git performs compared to other version control systems like SVN.

How to get started with git ?

So finally after all the theoretical explanation, let us get to using git and Github to create our first repo and making a commit. Github is a cloud based hosting platform for helping us manage our projects efficiently using the git version control. There are other such platforms such as GitLab, Bitbucket etc. But for now, we are going to go with GitHub as it is one of the easiest and most used platforms to get started with and there is a ton of material and resources to help expand on what we are going to discuss here.

Okay let us get started with making our first git repository.

  • Step 1: Navigate to your project directory to initialize git in your project:
    First we need to initialize git in your project directory. So let us use the command line to navigate to your project directory. Once we are in it, all we need to do is type the following command:
    • git init
  • Step 2: If you do not have any code already in the directory, then add some code:
    So after the previous step, there is now a .git file in your directory. This is the file that is there is on your local machine which tracks all the changes in your directory. This is the file which handles the whole backing up and tracking progress part of git. So once you have the .git file, all you need to do is write some code. This is primarily done to make changes to the repo so that you can now add these changes to the .git file, so that it can start tracking your progress from here.
  • Step 3: Add these changes to save them in your local .git file:
    Once you are done adding some code, you now need to add them to the .git file so that it can start tracking your progress. This needs to be done initially by adding and then committing the changes. SO in this step we will add these changes by using the following command:
    • git add . : The important thing to understand here is that the ” . ” after git add command tells git to add all changes made in the directory. This means all changes made to any and all of the files in the directory. This dot can also be replaced with the file name to add the changes of a specific file.
  • Step 4: Now we need to commit these changes:
    One we are done staging the changes, we need to now commit our changes. This will be done by using the following command:
    • git commit -m "Initial commit" : The important thing to note here is that ” -m” part reflects that everything after that is a message explaining what the commit is. So “Initial commit” is a message or the name of the commit we just made. As you proceed you may commit more than one time, so you should name your commit based on what changes were made.
  • Step 5: Making your Github account (if you don’t have one) and creating a new repository:
    Once you are done making an account on Github, you need to navigate to creating the “Repositories” tab. After this, you will click one “New”, and this will take you to a page where you will fill out details such as what you want to name your repository. This could be anything you want to name it, but preferably it would be better if you name it what the project name is. Select if you want the project to be private or public. Private repositories can only be accessed by people who you authorize, public repositories are available to everyone on the internet to view. After this click on click on “Create Repository”
  • Step 6: Now copy the .git link from Github and add it to your project.
    Once you create your repository, navigate to the “Clone or download” button and as soon as you click it, it will show a link which will end in “.git”. Copy that link and navigate back to the local directory of your project using command line. Then use the following command:
    • git remote add origin link : Important think is to replace the “link” part of the command with the copied link from Github.
  • Step 7: push your committed changes to Github:
    Now all you need to do is the git push command, which is as follows:
    • git push origin master : This command will push all your changes from the .git file to your repository on Github and clear all the changes on your .git file.
  • Step 8: Repeat steps 3 to 7 as you make new additions and changes to your project:
    So this is it. This is all you need to get started with git in your existing or new project. As you make new changes, add them to Github accordingly using steps 3 to 7. Every time you push your changes, Github will update them and you can see it on Github.

I hope I covered enough information for everyone to get started with git. By no means is this the only way to do git. There are a lot of other things such as making branches, forking and pulling from your versions which are a few advanced topics when it comes to learning git. But in my opinion this is the best way to get started with git. I believe that everyone is constantly learning and developing their skills with git as they practice and use it more over time. The only way to make it a good skill is by constantly using it and learning new things. So as soon as you are done reading this, I want you to go ahead and make a new project and integrate it with git using above steps. Try it out and let me know in the comments if you run into any issues. Cheers, and see you next week!