How To Use Git

Learn to code, learn web development basics, start a career in tech.

How To Use Git

Last edited: Aug 1 2019

Let's get started on using Git

Now that I have introduced you to the concepts of git, it is time for us to have a look at examples of how to use git in practice, we will start by downloading and installing git

Download Git

The link below has details on how to install Git in multiple operating systems:

Git is a command line tool, I will be testing it in my terminal, an app on a mac for running commands, all example codes below are to be pasted in your terminal command line.

Verify if Git is installed by with the following command in your terminal:

 git --version 

Initialize Git

Create a project directory on your desktop called git-demo and initialize git within that directory

 # change directory into your desktop
cd ~/desktop

# make directory called git-demo
mkdir git-demo

# change into git-demo directory
cd git-demo

# initialize git
 git init 

When we run git init, it initializes a repository within the git-demo directory called .git this is where git will save all snapshots along with other information git requires

Let's Add Code To Commit

Save the code below into git-demo directory as index.html

<!DOCTYPE html>
   <title>Git Demo</title>
    <p>Learning git</p>

Git Status

   git status 

git status tells us we have a new file inde.html which we have not yet commited, now we can go ahead and commit that work.

Git Staging

Before we create a snapshot of commit we first need to stage the work that we want to commit, example below shows how we stage the index.html file

   git add index.html 

If we had multiple files to stage we could do it in two different ways, one would be to list the files we want to stage as follows:

   git add file file file 

Another option for staging multiple files would be to use a (.) notation, this will go ahead and commit every file you have changed.

   git add . 

With our work staged ready for commit, we can run the status command again to see that our work is staged

   git status 

Git Commit

Now we are ready to commit our work, this will create the snapshot, making it possible for us to revert back to this snapshot at any point in the future.

   git commit -m'added an index.html' 

The above command will create our commit, it also adds a commit message 'added an index.html' this makes it possible for us to track this commit

Git Log

With git log we can inspect a list of changes that have been commited

   git log 

Git Branches

By default git work is done on a branch called Master Branch, that is where our commit from above was recorded, to view list of branches you type:

   git branch 

Let's go ahead and create a new branch, we will then commit some changes against our new branch:

   git branch about-page 

The above command has created a new branch called about-page, we can go ahead and view the list of branches with the git branch command, notice there is a star? that indicates which branch we are currently pointing to.

   git branch 

Now what we want to do is point our currently working directory to our newly created branch, to do that we use the following command:

   git checkout about-page 

Another check on our branch list should reveal that we are now pointing to our new branch

   git branch 

From now on, what ever new code we write and commit, will be recorded against our new branch, let's see that in action, below is the code for creating an about-us.html file

<!DOCTYPE html>
   <title>Git Demo</title>
    <p>About Page</p>

Now let's go ahead and stage this work

   git add . 

The next phase is to go ahead and commit this work

   git commit -m'Added about page' 

With the work now commited, we have created a new snapshot in about-us branch, if we checkout back into our master branch, you will notice that the about-us.html file will be missing. Let's see how we checkout back into master branch:

   git checkout master 

The above command has changed out working directory to point back to our master branch, what ever files were commited to the about-us branch will not be visible here

Git Merge

Now that we have checkedout our master ( meaning that we are now pointing to master branch) we can merge in our changes from the about-us page:

   git merge about-us 

The above command will take all the commits from about-us branch and replay them on the master branch, now the about-us.html file has been added to our working directory

If we go ahead and check the branches we will notice that we still have the about-us branch

   git branch 

Deleting a Git Branch

When a branch is merged into other branch or no longer needed, you can delete it using the following command:

   git branch -d about-us 

A further check of our branch lists with the command below should now reveal that we only have our master branch

   git branch 

If we check our commit log we will notice that we now have both commits, one was created in the master branch and the other when we were working from the about-us branch.

   git log 

An added bonus, you can also view your commits as single lines, this comes in handy when you have lots of commits to inspect:

   git log --oneline 

In Conclusion

You have just gone through some of the processes of how to use git, version controlling your work, keeping a history of changes, creating branches and learnt examples of how to change from one branch to another.

Git is a fantastic tool to add to your skill set and this article has barely scratched the surface, I advice you to visit the git documention to learn even more as well as see some of the video courses I have created on the subject.