Git and GitHub · Part 1 · Git basics
Part 1 summary: Git basics
The big idea
Git saves snapshots of a project, like save points in a game. Every snapshot records what changed, who changed it, when and why, so it's always possible to see the history and go back to any earlier version.
Each lesson in one line
- What Git is: Git tracks every version of a project. GitHub is a website that stores Git projects online.
- Setting up Git: set a name, email, default branch and editor once, with
git config --global. - Your first repository:
git initturns a folder into a repository by adding a hidden.gitfolder. - The three areas: changes move from the working directory, to the staging area, to the repository.
- Making commits: each commit should do one thing and have a clear message.
- Looking at history:
git loglists commits,git diffshows changes,git showshows one commit. - Undoing things: restore edits, reset local commits, revert shared ones.
- Ignoring files:
.gitignorekeeps junk and secrets out of Git.
Key terms
| Term | Meaning |
|---|---|
| Git | A tool that saves the history of a project |
| GitHub | A website that stores Git projects online, for backup and teamwork |
| Version control | Keeping track of every version of a project |
| Repository (repo) | A project folder that Git tracks |
.git | The hidden folder that holds the whole history |
| Commit | A saved snapshot of the project, with a message |
| Hash | A commit's unique ID, like 2c12ea6 |
| Commit message | A short note that says why a change was made |
| Parent | The commit just before another one; parents link commits into a history |
| Working directory | The project files as they're being edited |
| Staging area | The changes picked for the next commit (also called the index) |
| Untracked | A new file Git isn't tracking yet |
| Modified | A tracked file that changed but isn't staged |
| Staged | A change that's ready for the next commit |
| Working tree clean | Nothing has changed since the last commit |
| Branch | A separate line of commits; every project starts on main |
HEAD | The commit the project is on right now |
HEAD~1 | One commit before HEAD; HEAD~2 is two before |
| Diff | The lines that changed: + added, − removed |
| Atomic commit | A commit that does exactly one thing |
| Reflog | Git's log of every commit HEAD has been on; used to find lost commits |
| Merge conflict | Two changes to the same lines that Git can't combine on its own |
.gitignore | A list of files and folders Git should ignore |
Essential commands
| Command | What it does |
|---|---|
git config --global | Sets a name, email and other settings once |
git init | Turns a folder into a repository |
git status | Shows what's changed and what's staged |
git add file | Stages a change |
git commit -m "…" | Saves the staged changes as a commit |
git commit --amend | Fixes the last commit |
git log --oneline | Lists the commits, one per line |
git diff | Shows changes that aren't staged |
git diff --staged | Shows changes about to be committed |
git show hash | Shows one commit in full |
git restore file | Throws away edits to a file |
git restore --staged file | Unstages a file, keeping the edits |
git reset HEAD~1 | Undoes the last commit, keeping its changes |
git revert hash | Undoes a commit with a new commit |
git reflog | Finds a lost commit |
git rm --cached file | Stops tracking a file, keeping it on disk |
Rules to remember
- Run
git statuswhenever unsure. It always says what's going on. - Create
.gitignorefirst, before the firstgit add, and never commit.env. - One commit, one purpose, with a message written as a command, like “Add search bar”.
- Check before committing with
git diff --staged. - Not pushed yet, reset is fine. Pushed and shared, use revert.
- Be careful with commands that delete work:
git restore,git clean -fandgit reset --hard. - A committed secret is a leaked secret. Replace the key straight away.