An Intro to Git

Mylesmatthews
5 min readJan 12, 2022

--

Git is an open-source project developed in 2005 by Linus Torvalds that aids in maintaining and keeping version control across both small and large projects. This software tracks changes in your project allowing you to easily fall back to the recent version and see when recent changes have been made. This means if you happen to mess up and realize a part of your new programming is conflicting with past work then you can easily fall back to an old version. Git runs locally on your computer but allows you to upload your changes and versions to online hosting websites such as Github or GitLab. This is useful if you’re working on a project with people who will need your saves so you can compare versions to make sure you are not conflicting with each other’s work.

Useful Git Commands that will be referenced:

  • Git clone — Clones a repository down to your local computer
  • Git add — Adds your selected changes made to the staging area
  • Git commit — This commits your changes found in the staging area and readies them to be pushed into your remote repo on GitHub or a repo hosting website of your choice
  • Git push — Pushes your changes up to your remote repo

How is Git accessed?

Git is used through the Command line on whatever computer you will be using to store and use your files. These files can be stored in a repository (alternatively named a repo). This repository will be where the files you select to be staged and committed (essentially saving your work but more on that later), will be saved and changes tracked. These git repositories contain the date and time you save your work by committing and can potentially tell you what was changed.

These git repositories can be hosted on a website such as GitHub which will create a remote repo that others will be able to copy or “clone” down to their local device. You can move between the files on your computer by using the cd command, which is used to move between different folders or “directories” on your computer shown below. I cd into my “Tests” folder and then proceed to clone down a repository using a GitHub link which will now give me access to a copy of said repo on my local computer. The ls command used first is to list the files that are available in the folder (directory), you are currently in.

Here is the way I was able to find the link to clone down this GitHub repo, provided you’re using SSH (tutorial for setting up your SSH key for GitHub on your local command line located here)

Saving your work

Now we’re on to the main reason that you use Git, version control. Let us say you edit the files on your computer and you want to save them so that, just in case you happen to break your code it can be easily gone back on. This is where committing comes into play. In git, there is something called a ‘staging area’. If you are ready for your work to be saved, you can add these changes to the staging with the command, git add, and you can check what changes are added to

staging area with the command, git status.

Great! You’ve added your changes to the staging area and now you want to commit them so that you have a version of your work that you can fall back on. For this, we need to utilize the git commit command, which will allow us to commit our changes and see the date and time they were committed. We also want to add a message that will explain what’s contained in this commit and what it potentially does.

–To do this, the command goes: git commit -m ‘<Your message>’

You can see what commits were done, and when with the help of the git log command.

As you can see, since this is an old repo, I can see git commits dating back to even September, down to the exact second these commits were done. This is why git is so powerful, to be able to see exactly when you made certain changes to your project can be very useful for maintaining continuity in your work. Now that our changes are made and committed, we want to push them up to our GitHub repository. Well to do that, all we need is the git push command. This will send our changes up to GitHub.

Now, our changes are present on our Github repo, as shown here.

And there you go! You’ve successfully committed and pushed changes up to GitHub. These are the basics of using git that you will ALWAYS be using, no matter the scope of the project whether it be big or small.

Conclusion

Well, there you have it, you’ve successfully edited a document on your main computer and sent it up to a local repo. This WILL be an integral part of your life as a programmer seeing that you’ll be wanting to commit changes frequently to keep your work in order so that you know what you’ve been doing recently. This also helps for when you push, your teammates will also be able to see the steps you’ve taken to get to the point you’re at. Next, I will discuss Git branch and the importance of learning how to use them if you plan on programming in a social setting with multiple people (which you probably will). For an early start, you can follow this link, for information on the topic. Until the next time I write for you, I wish you luck on your programming endeavors, happy coding!

--

--