Lesson 1 - Creating a Repository
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 mainThe --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 --listCreating 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
pwdNow initialize a repository in that folder with git init:
git initThis 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 -laIf 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.htmlNow 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 statusGit 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.htmlYou don’t see any confirmation. Use git status again to see what changed:
git statusThe 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 statusGit 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:
- You created the repository with
git init. - You looked at the status of the repository with
git status. - You staged the file with
git add index.html. - You ran
git statusagain to see where things stood. - You made a commit with
git commit -m. - You used
git statusone 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 statusWhat You Learned
git configtells Git who you aregit initturns a folder into a repositorygit statusshows what changed and what to do nextgit addstages a file for the next commitgit commit -mtakes a snapshot with a message
In the next module you’ll change the file and track those changes.