Module 1 · Git & GitHub: From First Commit to Pull Request
Staging, Diffs, History and .gitignore
Go beyond the basics. Compare changes with git diff, commit in clean logical pieces, write messages teams will thank you for, explore history with log and show, and keep secrets and junk out of your repository with .gitignore.
About 30 minutes
Looking before you commit
Professionals never commit blindly. Before every commit they check what exactly is about to be saved. Git gives you two views, and the difference between them is the staging area.
| Command | Compares | Answers |
|---|---|---|
git diff | Working folder against the staging area | "What have I changed that I have not staged yet?" |
git diff --staged | Staging area against the last commit | "What will go into my next commit?" |
Try it. This project already has a history and an unstaged change. Run git status and then git diff, then stage the file with git add menu.txt and run git diff again. It prints nothing now, because everything is staged. Run git diff --staged and the change appears there.
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.
Reading a diff takes a minute to learn: a line starting with + was added, a line starting with - was removed, and an unchanged line starts with a space. A changed line shows as a removal followed by an addition.
One commit, one idea
The staging area exists so you can commit in logical pieces. Say you spend an hour and end up with three changes: you fixed a typo in the menu, added a contact page, and updated the opening hours. Those are three ideas, so make three commits, each with its own message.
Shell (bash)
git add menu.txt
git commit -m "Fix typo in the jollof rice price"
git add contact.txt
git commit -m "Add contact page"
git add hours.txt
git commit -m "Update opening hours for December"Why this matters:
- Easy to understand. A reviewer reads one small change at a time.
- Easy to undo. If the contact page causes a bug, you undo that one commit and keep the others (lesson 4).
- A useful history. In six months,
git logactually tells you what happened.
A commit that says "lots of changes" and touches twenty files is a warning sign.
Writing messages a team will thank you for
A commit message is a note to your future self and your teammates. A good one is short and says what the commit does.
- Start with a verb, in the imperative: "Add login page", not "Added login page" or "Adding". Read it as "This commit will... add login page."
- Keep the first line under about 60 characters.
- Say what changed and, when it is not obvious, why.
- Do not end with a full stop. It is a title, not a sentence.
| Weak | Strong |
|---|---|
| fix | Fix crash when the cart is empty |
| changes | Update delivery fee for Lekki orders |
| stuff i did today | Add monthly revenue chart |
| WIP | Draft the README project summary |
Many teams also use Conventional Commits, which add a short type at the start so that history is easy to scan and tools can read it: feat: add dark mode, fix: correct VAT rounding, docs: update README, chore: tidy file names. You do not have to use it, but you will see it everywhere.
Exploring history
Your history is a story. These commands let you read it.
Shell (bash)
git log --oneline # one line per commit
git log -3 # only the last three
git show # the last commit, with its changes
git show HEAD~1 # the commit before the last one
git show a3f9c21 # any commit, by its IDHEAD means "the commit you are on now". HEAD~1 is its parent, HEAD~2 its grandparent. You will use these to compare, undo and restore.
Try them on this project.
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.
Run git log --oneline, then git show (the newest commit), then git show HEAD~2. Notice how git show prints the message and the exact change.
.gitignore: files Git should leave alone
Not every file belongs in a repository. These should never be committed:
- Secrets: passwords, API keys,
.envfiles. Anyone who can see the repository can see them, forever. - Generated files:
__pycache__/,node_modules/, build outputs. They can be recreated. - Junk: log files, temporary files,
.DS_Store, your editor's settings. - Huge files: big datasets and videos slow everything down.
You tell Git to ignore them with a file called .gitignore, one pattern per line:
Output
# Secrets
.env
# Python
__pycache__/
*.pyc
# Logs and temporary files
*.log
*.tmp
# Operating system
.DS_Store| Pattern | Ignores |
|---|---|
.env | A file with exactly that name |
*.log | Every file ending in .log |
build/ | A whole folder called build |
# text | A comment, ignored |
Once a file is ignored, git status stops listing it, and git add . skips it. Commit the .gitignore file itself, so everyone on the team ignores the same things.
Try it
Set up .gitignore for a project that has a secret file and a log file in it.
Task
8 minThis folder has code, a README, a secret .env file and a debug.log. Make a repository whose first commit contains the code and the README plus a .gitignore, and does not contain .env or debug.log. Both files must stay in your folder, ignored.
Your repository is checked for
- app.py is committed
- README.md is committed
- A .gitignore file is committed
- The secret .env file is NOT committed
- The log file debug.log is NOT committed
- Both ignored files still exist in your folder
- git status is clean, so Git is not asking about the ignored files
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 practise one idea per commit. Two files have changed, and they are unrelated. Commit them separately.
Task
8 minThe header got a new contact link and the footer got a new year. Those are two ideas. Make two separate commits, one for each file, each with a message that starts with a verb.
Your repository is checked for
- Two new commits on top of the first (at least 3 in total), so each change is its own commit
- You staged header.html on its own
- You staged footer.html on its own
- Both changes are committed (clean)
- Messages start with a verb: Add, Update, Fix ...
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
You want to see exactly which changes are staged and will go into your next commit. Which command shows that? Type the full command.
Practice
Which line in .gitignore stops every file ending in .log from being tracked?
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