Essential Source Control with Git

Hands-On Lab

 

Photo of Stosh Oldham

Stosh Oldham

Course Development Director in Content

Length

00:30:00

Difficulty

Beginner

With the prevalence of modern DevOps culture, it is essential for almost any IT professional to have a basic understanding of source control. This activity walks through the basics of using the popular tool, Git, for source control.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Essential Source Control with Git

Introduction

In this lab, we're going to install and configure Git on a workstation, clone a repository so that we have a local copy, make some changes to files, and then commit those changes that local repository.

Get logged in

Use the credentials and server IP in the hands-on lab overview page to log into our lab server.

Install and Configure Git on a Workstation

First we'll need to install Git:

[cloud_user@host]$ sudo yum install git

Accept the prompt to download and install packages.

Now we'll configure Git, providing our own name and email address (we're substituting john in here, so if your name isn't John, your command will look a wee bit different):

[cloud_user@host]$ git config --global user.email john@email.com
[cloud_user@host]$ git config --global user.name John

Clone the Source Unchained Repository from http://github.com/linuxacademy/content-git-quickstart to /home/cloud_user/play and Add a New Branch for Your Story

This will clone the repository locally:

[cloud_user@host]$ git clone http://github.com/linuxacademy/content-git-quickstart /home/cloud_user/play

Now we can get into the directory to start work:

[cloud_user@host]$ cd /home/cloud_user/play

And we'll create a branch:

[cloud_user@host]$ git branch john-story

Add a New Story File to the Play Files and Track It in Source Control

Switch to the new branch

[cloud_user@host]$ git checkout john-story

Now lets create a new file (for example, story.txt) with some sample content:

[cloud_user@host]$ vim /home/cloud_user/play/johns-story.txt

Put whatever text you like in there, then save it and quit.

We can add the file to the repository by running this:

[cloud_user@host]$ cd /home/cloud_user/play
[cloud_user@host]$ git add johns-story.txt

Note: We should make sure we're in the repository directory (assuming the filename johns-story.txt).

Add an Entry to the File contributers.txt

We need to edit /home/cloud_user/play/contributers.txt and add an entry. Just edit the file and add the email address we used earlier, during git config, to the end of it.

We'll need to let Git know to track the change we made by running the following:

[cloud_user@host]$ git add contributers.txt

Commit Your Changes Using the Message "airplane story"

We're almost done. Now we've got to commit our changes to the Git repository. From the directory /home/cloud_user/play, run this:

[cloud_user@host]$ git commit -m "airplane story"

Conclusion

That's it. We installed Git, pulled a repository down, made changes to it, and then committed those changes. Congratulations!