Your first repository¶
You'll create a repo on your computer, make some commits, and look at the history.
Create the repo¶
git init turns the current folder into a Git repository by adding a hidden .git folder, where Git keeps its history. Don't edit anything inside .git.
The basic loop¶
Using Git is mostly the same three steps, over and over:
flowchart LR
A[Edit files] -- git add --> B[Staged]
B -- git commit --> C[Committed]
C --> A
- Edit files as usual.
- Stage the changes you want in the next snapshot with
git add. - Commit the snapshot with a message describing it, using
git commit.
Make your first commit¶
Create a README:
Write something like:
Save and exit, then ask Git what it sees:
README.md is listed as untracked: Git sees it but isn't recording it yet. Stage it and check again:
It's now under Changes to be committed. Commit it:
Writing good commit messages
Describe what the change does, in a short command: Add motor wiring notes, Fix LED pin number. Avoid stuff or update. Six months from now you'll need to find this commit.
Make more commits¶
Add a parts list:
Then edit README.md and add a line at the bottom. Before staging, see exactly what changed:
Lines starting with + were added and lines with - were removed. Stage and commit both files:
git add . stages every change in the current folder.
Look at the history¶
Each commit has an ID (b7e21c4). Use git show <id> to see what a commit changed.
Undoing things¶
| Situation | Command |
|---|---|
| Throw away unstaged edits to a file | git restore parts.md |
| Unstage a file (keep your edits) | git restore --staged parts.md |
| Fix the message of your last commit | git commit --amend -m "Better message" |
Warning
git restore <file> permanently discards your unsaved edits to that file.
Ignoring files¶
Some files shouldn't go in Git: build output, logs, passwords and API keys. List them in a file called .gitignore:
Never commit passwords or keys
Once something is pushed to GitHub, assume it's public forever, even if you delete it later.
Exercise¶
Make two more commits on your own. For example, add a notes/ folder with a file in it. Then run git log --oneline and check you see five commits.