Lesson 2.5.1: Committing versions
Git saves versions of a folder. Each version you commit to the history is a commit, with its message.
The idea
Without Git, versions end up like this: game.js, game-final.js, game-final-really.js.
Git saves versions of a whole folder. Each version is a commit: a snapshot of the project at that moment, with a message saying what changed. A folder with Git is called a repository.
Installing it and introducing yourself
- Windows: download it from git-scm.com and install it with the default options.
- macOS: type
git --versionin the terminal; if it's missing, the system offers to install it. - Linux (Ubuntu, Debian):
sudo apt install git.
Then, just once, tell it who you are and what the main branch is called:
git config --global user.name "Ana García"
git config --global user.email "ana@example.com"
git config --global init.defaultBranch main
Creating the repository
In my-game:
git init
Initialized empty Git repository in /Users/ana/projects/my-game/.git/
Git keeps everything in a hidden folder, .git. Don't touch it.
Committing a version
Two steps:
git addchooses what goes into the snapshot.git committakes it, with its message.
git add hello.js
git commit -m "Add the greeting"
[main (root-commit) 3f2a1c9] Add the greeting
1 file changed, 1 insertion(+)
create mode 100644 hello.js
git add . adds everything that changed in the folder.
Asking Git
git status says how everything stands. Right after creating the repository, before any add:
git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.js
table.js
nothing added to commit but untracked files present (use "git add" to track)
Untracked means Git sees them but isn't saving them yet.
Use it often: before add, before commit and whenever you're unsure.
The message
For now, a short sentence saying what the change does: "Add the 3 times table", not "changes". How to write a good message is the next lesson, 2.5.2.
Try it
Make a second commit that saves table.js. Look at git status before and after.
Show the solution
git status
git add table.js
git commit -m "Add the 3 times table"
git status
After the commit, git status says nothing to commit, working tree clean: everything is saved.
Summary
- Git saves versions of a folder; each one is a commit.
git initcreates the repository, once.git addchooses;git commit -m "…"commits the version to the history.git statussays how everything stands.