Creating Local Repositories with Git and Adding/Checking in FIles

Hands-On Lab

 

Photo of Terrence Cox

Terrence Cox

Senior Vice President of Content

Length

00:30:00

Difficulty

Beginner

When working with source code or text documents on any local system, using Git allows you to track changes and revert at any time. In this scenario, the student will create an empty local repository using 'git' commands, create files within that repository, add and check them in and verify the repository shows up-to-date. After this exercise, the student will understand how to work with files and directories within a local repository to manage and track files and changes to them.

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.

Creating Local Repositories with Git and Adding/Checking in Files

Introduction

Welcome to the lab. Sit down, grab a coffee, and get comfortable. We've been handed a server with "half-running" Git setup that we need to finish configuring so we can give it back to the dev team.

What we essentially need to do is initialize a new Git repository on the server, using credentials the dev team has provided. They want us to create a README.md file containing the text Web-API v1. We'll need to get that file into the repository and commit the changes. Once we can confirm the branch is up to date, we can give the server back.

Create Directory for the Repository

The dev team would like the repository to be created in a directory called web-api. Once we're logged in and sitting in our home directory (make sure with a quick cd), let's make it with this:

mkdir web-api

Initialize the New Git Repository

Now we've got to get the new repository initialized. Get into the new directory:

cd web-api

Then, initialize it with this command:

git init

We can verify a repository was created/initialized with this:

ls -al

We should see a .git directory in the listing.

Create a Repository README File

The dev team asked us to create a README for the repository, with some text in it. This is pretty simple. Just run echo and redirect output:

echo "Web-API v1" >> README.md

Add and Commit the README File to the Repository

Now that README.md exists in /home/cloud_user/web-api/, we need to add it to the staging area and commit it to the repository. We can do this with:

git add README.md  
git commit -m "Added README.md to the project"

Then, we can verify everything is checked in and nothing else is left to do:

git status

We should get some output like this:

RESPONSE IN TERMINAL:
On branch master
nothing to commit, working directory clean

Conclusion

We're all finished. This server now has a working Git repository on it, and there's a README.md sitting in it. We can hand this back to the dev team.