Skip to main content

Version control

What is Git?

Definition

Git is a distributed version control system that records snapshots of a project over time, giving every clone the full history and the ability to work offline.

Git in more detail

Every clone is a complete repository, not a checkout. History, branches and tags are all local, so committing, branching, diffing, searching the log and rewriting recent work need no server and no connection; only fetch, pull and push involve the network. That is the distributed part, and it is why Git branches are cheap enough to create for a ten-minute experiment.

Internally Git stores snapshots, not diffs. A commit records a complete tree of the project plus a pointer to its parent, and every object is addressed by the hash of its content, so identical files are stored once and any corruption is detectable. Because a commit's hash covers its parent, changing anything in history necessarily produces new commits with new hashes for everything after it — which is exactly why rebasing published branches causes trouble for everyone who already has the old ones.

The mental model that saves the most time is the three areas: the working tree you edit, the staging area (index) where you assemble the next commit, and the repository of committed history. Nearly every confusing Git command is moving something between two of those. It is also worth being clear that Git is not GitHub: Git is the tool, while GitHub, GitLab and Bitbucket are hosting platforms that add pull requests, issues, permissions and CI around it.

Git example

working tree  --git add-->  staging area  --git commit-->  repository
      ^                                                        |
      +---------------- git checkout / git restore ------------+

$ git switch -c feature/search      # branch and switch
$ git add -p                        # stage selected hunks
$ git commit -m "Add search index"
$ git push -u origin feature/search

Undoing, in order of destructiveness
  git restore <file>        discard unstaged edits to a file
  git restore --staged <f>  unstage, keep the edit
  git commit --amend        rewrite the last commit
  git revert <sha>          NEW commit undoing an old one (safe
                            on shared branches)
  git reset --hard <sha>    move the branch, discard work
  git reflog                the safety net: find lost commits

Git — common questions

What is the difference between Git and GitHub?
Git is the version control software that runs on your machine. GitHub is a hosting service for Git repositories that adds pull requests, issue tracking, permissions and CI. Git works perfectly well with no GitHub account.
What is the difference between git merge and git rebase?
Merge creates a commit joining two histories and preserves what actually happened. Rebase replays your commits on top of another branch, producing a linear history but new commit hashes — which is why you should not rebase commits others have already pulled.
How do I undo a commit that is already pushed?
Use git revert, which adds a new commit reversing the change and leaves history intact for everyone else. Force-pushing a rewritten history to a shared branch breaks every other clone and should be avoided.

Keep reading

This page explains what Git means. For the syntax itself, the Git cheat sheet is a full reference of real commands and examples. Every other definition is in the A-Z glossary.

Browse the A-Z glossary