An Intro: Git and GitHub for Starters

Yankee Maharjan
Leapfrog
Published in
7 min readJan 14, 2019

--

Image: blog.github.com

If you are working your way in the field of programming, then the most indispensable tool you will come across is Git. And if you want to collaborate with people in a project, or want to create a ‘developer’ portfolio online, it is most likely that GitHub is where you will end up doing those things.

What is Git?

Git is a version control system for tracking changes in computer files and coordinating work on those files among multiple people.

Well, let’s start with this meme on the internet.

https://i.redd.it/ppai95a62qwz.jpg

The meme that you see above used to be prominent in the design world; luckily we developers have a better solution for ourselves. Instead of creating multiple files to signify different changes, a version-controlled setup provides a centralized repository where each change in files is tracked in a controlled manner. It makes it possible to go at any instance where you made a significant change in your file and continue working from there on. This is what Git is capable of doing and we are just scratching the surface.

So let’s dig into the basics first.

Note: Everything in this tutorial is based on the command line. Please make sure you have prior command line experience. Here’s a tutorial to get you started: Learn the Shell

Settings Things Up! 🔨

Installation for different OS platforms:

Once you have installed Git in your system, it’s time to verify the installation.

In your terminal type in git --version to make sure Git is installed.

Git Setup

To set up Git for the first time, you’ll need to provide an username and your email address by running these two git commands.

Git Glossary 📗

  • Git Status
    Check the status of files.
    Syntax: git status
  • Git Add
    Make Git aware of your file for tracking.
    Syntax: git add <filename>
  • Git Commit
    Commit the file to the local repository with a message.
    Syntax: git commit -m "This is my first commit!"
  • Git Log
    Logs out all the commit history.
    Syntax:git log
  • Git Push
    Push the committed code to the remote repository.
    Syntax: git push origin master

Note: You need to associate a GitHub repository to push the files which we will eventually do later on.

Git States

Before we get our hands dirty editing our files, we have to know what Git States actually is. It is one of the most crucial things that a beginner needs to understand to use Git properly.

  1. Modified — You have changed files.
  2. Staged — You have marked a modified file to go into your next commit snapshot.
  3. Committed — The data is safely stored in your local database.

Tip: All of these operations are maintained locally by Git.

Similarly, based on the states of Git we have corresponding sections in Git.

  1. Work Area
  2. Staging Area
  3. Git Repository

Don’t worry, if these words seem alien to you, you will soon understand these terms once we start using Git for real.

Git States Illustration

Keep this figure by our side at any times to know where your files are in the Git Status Lifecycle.

Let’s Practice 🏁

Create a folder where you want to try this guide and open it. Inside the folder, right click and click either Open Terminal Here (for Debian/Ubuntu) or Git Bash Here (for Windows).

Initialize Git

To begin tracking files, first, we need to initialize Git inside the folder where we want to record changes. To do that type the command below in terminal.

git init

You will get a message specifying Initialized empty Git repository in project_dir. That means we are ready to go!

Git Workflow Steps 🐾

As we move forward, we will be using the Git States analogy to know where we are in the Git State.

1. Untracked

Let’s create a text file inside of the folder.

touch readme.txt

Now to check the state of files in our Git directory, we use the command:

git status

Git status is the command that we will be using the most while using Git. It shows the state of all the files inside our working folder.

You will receive the following message:

Notice the word “Untracked”; it means that Git hasn’t taken record of the file that we just created. To make Git notice our file we have to add it.

2. Staged

git add readme.txt

Once you add the file, it is in “Staged” state, meaning, it is ready to be committed. It says you have done all the significant changes you want, and now you want to save it. Let’s check out our files status now.

Git guides you through every step of the File Status Lifecycle. If you look here, it specifies that our file readme.txt is ready to be committed, but there are No commits yet.

Similarly, if you want the file to be unstaged, then it also provides an explicit instruction to do so i.e.

git rm --cached readme.txt

Tip: Next time you do git status, make sure to read all the information provided there.

3. Committed

Time to save our files so we can get back to them later on.

git commit -m "First commit"

Here, -m flag is to represent your commit message.

Congratulations, you have made your very first commit! 🌟

4. Modified

Now if we check our status, then it says that our working directory is clean. That means no new changes were made to any files in that folder.

Open the readme.txt file and add some text into it.

If you check the git statusnow, then you will see the something like this:

Git has tracked that you have modified the file. It does so by comparing the recent state of your file with the file that you previously committed. Now you are at the very spot where you began. Meaning, you have to add the file and commit it as we did earlier. (Try it yourself 😉)

5. Git Log

At this point, you already have done two commits. To see all of your commits, you have to use the git log command.

This command will list all of your commits along with other information like the timestamp of the commit, your username, and email that you set up earlier. The long gibberish alpha-numeric value that you see is a hash that Git automatically creates to represent your commits. It is unique for every commit.

6. Pushing your code to GitHub

Till now the Git was running locally in your computer. If you want to collaborate with your friends, then you have to host your code in the cloud.

Now, let’s set up a GitHub account. Go through this link create your GitHub account.

Here we have opted for GitHub simply because it is one of the most popular and feature-rich hosting service for Git. You can go with any other service providers like GitLab, BitBucket and many more.

Whichever the service provider, the underlying technology is Git and it is the same across all the platforms. Plus all the commands will work the same.

Let’s begin!

Click the plus icon on the top right of the windows and select ‘Create Repository.’

Create a New Repository

Give your repository a name and a description. Then you can choose to make your repository either Public or Private. Once you have done that select ‘Create Repository’.

Setting up Git Repository

GitHub will provide you with information to initialize Git on your computer and push to GitHub. The only line of code that concerns right now is the Git Remote.

Copy the remote that is provided by GitHub and paste it in your terminal. Once you have done that, it’s time to push your code to GitHub.

git push origin master

Enter your username and password on prompt, and now you have successfully pushed your code to GitHub. Go to your repository and find your files there. 🔥

Wohoo! We are done.

Check out our repository for this tutorial here.

Further Reading

If you want to dive deeper into Git, then there are some intermediate concepts that you should know of. All of them are listed below.

--

--