An Intro to Git and GitHub for Beginners Part I (Tutorial)

Munira Omar
5 min readDec 10, 2018

What is the Difference between Git and GitHub?

Git is a free and open distributed version control system, a tool to manage your source code history.

GitHub is a code hosting platform for version control and collaboration. It lets you and others work together on projects from anywhere.

In short, Git is the tool, and GitHub is the service for projects that use Git.

Install Git

The first thing you need to do is install Git on your Machine.

Ubuntu

$ sudo apt-get install git

Windows

Download and install Git for Windows.

Create a GitHub Account

Create a free GitHub.com account.

Step 1: Create a local git repository

A repository (or often, ‘repo’, for short) is the Git version of a project folder. Git will track any changes inside of a repository.

Start by creating a directory on your machine and cd into the directory you created.

$ mkdir myProject
$ cd myProject
Make sure you are inside the directory you created.

Set up name and email in Git config

$ git config --global user.name "Your Name Here"
$ git config --global user.email "your_email@example.com"

Check your Git config using git config --list command

$ git config --list

You can initialize a Git repository in the root of the folder by running git init command

$ git init
Git init command creates a hidden directory “.git” in the directory where you ran it.

The 3 States of Git

Files in a repository go through three stages before being under version control with git:

  • Committed means that the data is safely stored in your local database.
  • Modified means that you have changed the file but have not committed it to your database yet.
  • Staged means that you have marked a modified file in its current version to go into your next commit snapshot.
The 3 States of Git

In addition to these three states, Git files live in one of three areas: the Working directory, Staging area, and the Git directory (aka your local repository).

Git Workflow

The manipulation among all the above states occurs locally, meaning that the changes affect no one else’s current repository but yours.

The basic Git workflow goes something like this:

  1. Working directory/tree: The current version of your project where you are making changes (which is reflected in your code editor)
  2. Staging Area(aka Index): The place where you stage your files when you are preparing them to commit
  3. Repository: When you commit, Git permanently saves only the changes from your staging area to the repo’s memory

If a particular version of a file is in the Git directory, it’s considered committed. If it has been modified and was added to the staging area, it is staged. And if it was changed since it was checked out but has not been staged, it is modified.

Step 2: Add a new file to the repo

Create a new file in your myProject directory named hello.txt

$ touch hello.txt
ls command — shows a list of all files or folders in the current directory

The file is now in the directory, but Git won’t officially keep track of the changes of the new file unless you explicitly tell it to.

Check the status of your repo with git status command

$ git status
As we can see our hello.txt file is “untracked’

Step 3: Add a file to the Staging area

We can add a file to the staging area using git add command

$ git add hello.txt
Notice the “Changes to be committed” line after we run the git status command

Once a file is in the Staging area, you can unstage the file / untrack it using the git rm --cached <file_name> command

$ git rm --cached hello.txt
Our file is no longer in the staging area and that means it is not tracked by git

The file is now returned back to the Working directory. So use the command git addto add the file back to the staging area so that we can commit.

Step 4: Create a commit

Once you have one or more changes to the staging area, you can commit them with a message using

$ git commit -m 'adds the hello.txt'

saved changes are called commits. Each commit has an associated commit message, which is a description explaining why a particular change was made. Commit messages capture the history of your changes, so other contributors can understand what you’ve done and why.

Committing cleans the status of the staging area:

use git status to check the status of your repo after committing

Committing also permanently stores the edit you made into a record store, which you can see by typing git log command:

git log
Look at your progress using the git log command

Your most recent commit is called the HEAD

View my next article in order to learn how to create Branches and push them on Github.

Congratulations!
You are now using Git.

Reference

--

--

Munira Omar

I’m just here to talk to myself and hope people are listening.