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

  1. What Git is: Git tracks every version of a project. GitHub is a website that stores Git projects online.
  2. Setting up Git: set a name, email, default branch and editor once, with git config --global.
  3. Your first repository: git init turns a folder into a repository by adding a hidden .git folder.
  4. The three areas: changes move from the working directory, to the staging area, to the repository.
  5. Making commits: each commit should do one thing and have a clear message.
  6. Looking at history: git log lists commits, git diff shows changes, git show shows one commit.
  7. Undoing things: restore edits, reset local commits, revert shared ones.
  8. Ignoring files: .gitignore keeps junk and secrets out of Git.

Key terms

TermMeaning
GitA tool that saves the history of a project
GitHubA website that stores Git projects online, for backup and teamwork
Version controlKeeping track of every version of a project
Repository (repo)A project folder that Git tracks
.gitThe hidden folder that holds the whole history
CommitA saved snapshot of the project, with a message
HashA commit's unique ID, like 2c12ea6
Commit messageA short note that says why a change was made
ParentThe commit just before another one; parents link commits into a history
Working directoryThe project files as they're being edited
Staging areaThe changes picked for the next commit (also called the index)
UntrackedA new file Git isn't tracking yet
ModifiedA tracked file that changed but isn't staged
StagedA change that's ready for the next commit
Working tree cleanNothing has changed since the last commit
BranchA separate line of commits; every project starts on main
HEADThe commit the project is on right now
HEAD~1One commit before HEAD; HEAD~2 is two before
DiffThe lines that changed: + added, − removed
Atomic commitA commit that does exactly one thing
ReflogGit's log of every commit HEAD has been on; used to find lost commits
Merge conflictTwo changes to the same lines that Git can't combine on its own
.gitignoreA list of files and folders Git should ignore

Essential commands

CommandWhat it does
git config --globalSets a name, email and other settings once
git initTurns a folder into a repository
git statusShows what's changed and what's staged
git add fileStages a change
git commit -m "…"Saves the staged changes as a commit
git commit --amendFixes the last commit
git log --onelineLists the commits, one per line
git diffShows changes that aren't staged
git diff --stagedShows changes about to be committed
git show hashShows one commit in full
git restore fileThrows away edits to a file
git restore --staged fileUnstages a file, keeping the edits
git reset HEAD~1Undoes the last commit, keeping its changes
git revert hashUndoes a commit with a new commit
git reflogFinds a lost commit
git rm --cached fileStops tracking a file, keeping it on disk

Rules to remember

  • Run git status whenever unsure. It always says what's going on.
  • Create .gitignore first, before the first git 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 -f and git reset --hard.
  • A committed secret is a leaked secret. Replace the key straight away.