Creating Local Repositories with Git and Adding/Checking in FIles
Senior Vice President of Content
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.
Creating Local Repositories with Git and Adding/Checking in Files
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:
Initialize the New Git Repository
Now we've got to get the new repository initialized. Get into the new directory:
Then, initialize it with this command:
We can verify a repository was created/initialized with this:
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
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:
We should get some output like this:
RESPONSE IN TERMINAL: On branch master nothing to commit, working directory clean
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.