Harness the Combinatoric Power of Command-Line Tools and Utilities

Git Basics

Lesson 1 - Creating a Repository

Git

Git is a program you run on your computer that manages repositories of code. A repository holds your files along with the full history of changes you’ve made to them. In this module you’ll configure Git, create a repository, and commit your first file.

Configuring Git

Before you use Git, tell it who you are. Git attaches your name and email address to every commit you make. Set them with the git config command:

git config --global user.name "Jane Doe"
git config --global user.email jane@example.com
git config --global init.defaultBranch main

The --global flag applies these settings to every repository on your machine. The third line tells Git to name the first branch main in each new repository. You’ll learn about branches later. For now, think of main as the primary timeline of your project.

You can confirm the settings with git config --list:

git config --list

Creating a Repository

You can turn any folder into a Git repository. Create a folder for the project and move into it:

mkdir /tmp/awesomeco_site
cd /tmp/awesomeco_site
pwd

Now initialize a repository in that folder with git init:

git init

This turns the current folder into a Git repository. Git stores everything it knows about the repository in a hidden folder called .git. Take a look:

ls -la

If you delete the .git folder, you lose the entire history of the project. Only the current versions of your files remain. Leave that folder alone and let Git manage it.

Your First File

Create an index.html file with a basic HTML template. The cat command with a << block writes the lines that follow into the file:

cat > index.html <<'HTML'
<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <title>AwesomeCo</title>
  </head>
  <body>
    <h1>AwesomeCo</h1>
  </body>
</html>
HTML
cat index.html

Now that you have a file, tell Git to track changes to it.

Checking the Status

The git status command shows you where things stand in your repository. It lists the files that changed and tells you what to do next. Run it now:

git status

Git reports that it isn’t tracking index.html. Git ignores changes to files you haven’t explicitly told it to track. The output even tells you which command to use next.

Adding the File

Use git add to tell Git to track index.html:

git add index.html

You don’t see any confirmation. Use git status again to see what changed:

git status

The file now appears as a new file. Git calls this area the staging area. Files in the staging area are ready for your next snapshot.

Committing

Files in your workspace can be in several states. Git might not track a file at all, or the file might be new, modified, staged, or committed.

You used git add to stage the file. But Git hasn’t stored a version of the file in its history yet. That’s what a commit does. A commit also needs a message that describes the change, which you provide with the -m flag:

git commit -m "Add index.html to repository"

Look at the result. The phrase 10 insertions means that Git recorded 10 new lines. Git tracks lines of content rather than whole files, and your file has 10 lines.

Run git status one more time:

git status

Git reports “nothing to commit, working tree clean.” Git tracks every file and you’ve committed every change. This is a good place to be.

How You Got Here

Here’s what you did:

  1. You created the repository with git init.
  2. You looked at the status of the repository with git status.
  3. You staged the file with git add index.html.
  4. You ran git status again to see where things stood.
  5. You made a commit with git commit -m.
  6. You used git status one last time to make sure everything was committed.

The key to using Git well is to run git status often.

Your Turn

Create a new repository in /tmp/practice, add a file called notes.txt, and commit it. Then run git status to confirm the working tree is clean:

mkdir /tmp/practice
cd /tmp/practice
git init
echo "My first note" > notes.txt
git add notes.txt
git commit -m "Add notes"
git status

What You Learned

  • git config tells Git who you are
  • git init turns a folder into a repository
  • git status shows what changed and what to do next
  • git add stages a file for the next commit
  • git commit -m takes a snapshot with a message

In the next module you’ll change the file and track those changes.