Module 1 · Git & GitHub: From First Commit to Pull Request
Your First Repository: init, add and commit
Turn a folder into a Git repository, then use the core loop every developer repeats all day: edit, check the status, stage, commit and read the history. Practise each command in the terminal built into the page.
About 30 minutes
The core loop
Almost everything you do in Git is one loop:
- Edit your files as normal.
- Check what changed:
git status. - Stage the changes you want to save:
git add. - Commit them with a message:
git commit -m "...". - Read the history:
git log.
This lesson builds that loop into your fingers. You will work in the practice terminal. On the left are the project's files, and on the right is the terminal. When you see a command, type it and press Enter.
Our project is a small recipe notebook. Start by looking around. Type ls to list the files, and cat recipes.md to read one.
Files in your project folder
Type a command below and press Enter. Type help for the commands you can use. Press the up arrow to bring back an earlier command.
Step 1: git init
A folder becomes a repository when you run git init inside it. Try it in the terminal above:
Shell (bash)
git initGit replies Initialized empty Git repository. It created a hidden folder called .git (try ls -a to see it) that holds the whole history. Never edit or delete .git. Delete it, and you delete the history, and the folder is a normal folder again.
You run git init once per project. Everything else happens inside that same folder.
Step 2: git status, your best friend
git status tells you what is going on. Use it constantly, before and after every other command.
Shell (bash)
git statusRight now it says:
Output
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
recipes.md
shopping.txtUntracked means Git can see the files but is not tracking them yet. Notice in the file list on the left, too: a ? badge appears beside untracked files. (A means staged, M means changed, and U means a conflict, which you meet in lesson 6.)
Step 3: git add, staging a change
git add moves changes into the staging area: the waiting room for the next commit.
Shell (bash)
git add recipes.md # stage one file
git add . # stage everything in this folderStage only recipes.md first, then run git status again. It now shows recipes.md under Changes to be committed (staged) and shopping.txt still untracked. You can see the A badge appear beside recipes.md.
Step 4: git commit, saving a snapshot
Shell (bash)
git commit -m "Add jollof rice recipe"The -m flag gives the commit message, in quotes. Git replies with the short ID of your new commit. Run git status once more: it now says nothing added to commit and lists shopping.txt as still untracked, because you never staged it.
That is the staging area at work. You chose what went into the commit. Now add and commit the shopping list as a separate commit, with its own message:
Shell (bash)
git add shopping.txt
git commit -m "Add shopping list"Step 5: git log, reading the history
Shell (bash)
git log
git log --onelinegit log shows each commit with its ID, author and message, newest first. git log --oneline squeezes each to one line, which is easier to scan. Try both. You should see your two commits, with HEAD -> main marking where you are now.
Editing a tracked file
Most of the time you change a file that Git already knows. Click shopping.txt in the file list and add a line, such as Garri, then Save file. (You can also use the terminal: echo "Garri" >> shopping.txt adds a line.) Now run:
Shell (bash)
git status
git diffgit status now shows shopping.txt as modified, and git diff shows exactly what changed: lines starting with - were removed and lines with + were added.
Then stage and commit it, with a message that says what you did.
A shortcut, and a warning
For files Git already tracks, git commit -am "message" stages and commits all changed tracked files in one step. It does not include new, untracked files. It is handy, but for beginners, staging explicitly with git add keeps you in control and teaches the model. Be careful with git add .: it stages everything, including files you did not mean to include. Check git status first.
Good commit messages
A commit message says what changed and why it matters. Future you, and your team, will read these.
| Weak | Strong |
|---|---|
| update | Add revenue by region chart |
| stuff | Fix date format in orders data |
| final | Write README with project summary |
| fixed it | Fix crash when the cart is empty |
Start with a verb (Add, Fix, Update, Remove) and keep it under about 60 characters. Lesson 3 goes deeper.
The browser way (for later)
You can also create a repository on the GitHub website: +, New repository, name it, tick Add a README file, and Create repository. You can then add and edit files in the browser and commit from there. It is the same idea with buttons instead of commands, and it is how you start a repository that lives on GitHub. In lesson 7 you will connect your own computer to a GitHub repository.
Try it
First, create your first repository. The folder has two files and no Git yet.
Task
8 minTurn this folder into a repository and make a commit that includes both files, with a clear message. Finish with git status showing a clean working tree.
Your repository is checked for
- The folder is a Git repository (git init)
- Both files are tracked in a commit
- shopping.txt is committed too
- Your commit has a clear message of at least 8 characters
- Nothing is left uncommitted (git status is clean)
Files in your project folder
Type a command below and press Enter. Type help for the commands you can use. Press the up arrow to bring back an earlier command.
Now make a second commit. The repository already has one commit. Add a new item to the shopping list and save the change.
Task
8 minAdd Garri as a new line in shopping.txt, look at the change with git diff, and commit it with a message that starts with a verb.
Your repository is checked for
- There are now at least two commits
- shopping.txt includes Garri
- The change is committed (working tree clean)
- You wrote a commit message that starts with a verb (Add, Update, Fix ...)
- You checked what changed with git diff or git status before committing
Files in your project folder
Type a command below and press Enter. Type help for the commands you can use. Press the up arrow to bring back an earlier command.
Practice
Which command shows you what has changed, what is staged and what is untracked? Type the full command.
Practice
You edited index.html and style.css, but you only want index.html in your next commit. Which command stages just that one file? Type the full command.
Earn your badge
Take the module check
Questions on this module. Pass and you earn the Git Basics badge, free.
Create a free account to earn the badge