Why version control
Five copies of one file. Which one is live, and who changed it?
Version control records every saved state of a project as a commit: who, when and why. Git is the system most teams use.
Before Git, see what it replaces. A script gets edited, so someone saves a copy. Then another. Soon there are five, and nobody can say which one is running, what differs between them, or who made the change that broke things. Version control keeps one file and remembers every saved state of it. Here is the same script in Git: three saved states, called commits, each with an author, a date and a message that says why. Any version can be compared or brought back, and two people can work at the same time.
Commits
Every commit is a complete snapshot of the project
A commit is a snapshot, a parent and a hash that names it.
Most people picture Git as a list of edits. It is not. The first commit records the whole project as it stood: two files. The second commit is again the whole project. One file changed, so a new version is stored. The other did not, so the snapshot simply points at the copy it already has. And the commit points back at its parent. The third adds a test file. Three complete snapshots, cheaply stored.
The three areas
A change passes two gates: add, then commit
Working directory, staging area, repository. You choose what moves on.
A file can be in three places. You edit in the working directory. Here two files have been changed. Git add copies one of them to the staging area, which is the draft of your next commit. Git commit turns that draft into a commit in the repository. The other edited file was never added, so it is not part of this commit. It is still waiting in the working directory.
git status · Going deeper · 1 of 2
git status sorts every file into staged, not staged or untracked
git status shows which gate each file has passed, and the command for the next one.
.gitignore · Going deeper · 2 of 2
.gitignore hides new files; it does not untrack or erase old ones
A secret that was ever committed stays in history. Change the key.
History
History is what you can reach from a label
git log walks backwards from where you are, parent by parent.
The arrows in these pictures point backwards, from each commit to its parent. That direction explains what Git shows you. Ask for the log of main, and Git starts at E and walks back: E, B, A. It never sees C or D. From feature it walks D, C, B, A. So what happens if you delete the label feature? Nothing points at D any more. The commits still exist for a while, but no log will show them.
History · Going deeper · 1 of 2
Detached HEAD
HEAD on a commit, not a branch. Name the work before you leave.
Searching history · Going deeper · 2 of 2
Ask history a question: which commits, which file, which line, who
Filter git log by path, text or author; git blame names the commit behind each line.
Lesson complete
You finished “Version control with Git”
Nicely done.