Programming Search⌘K

Toolcore

Git

Also called git, git-scm.

Git is a distributed version control system. Each clone holds the full history of a project, and Git stores that history as snapshots that are named by their hashes.

Role
version-control
Licence
gpl-2.0-only
First release
2005-04-07

What it is

Git is a distributed version control system. It records the history of a directory of files. Each clone of a repository holds the whole history, so most commands work without a network.1 Git stores each version as a snapshot of the project, not as a list of changes. Git names each stored object by a hash of its content, so a changed or damaged object gets a different name and Git notices it.1 Git is free software under version 2 of the GNU General Public License.28

Install and first use

On a Debian or Ubuntu system, run sudo apt install git-all. On Fedora, run sudo dnf install git-all.2 On macOS, run brew install git with Homebrew, or run xcode-select --install to get the Git that comes with the Xcode Command Line Tools.3 On Windows, install Git for Windows, a separate project that packages Git.2

Before your first commit, tell Git your name and email. Git writes them into each commit.4

git config set --global user.name "Ada"
git config set --global user.email ada@example.com

The examples on this page set the identity and the dates with environment variables instead, so that the hashes are the same each time they run. git status --short marks a file that Git does not track with ??, and a file that is added to the next commit with A.5

export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com GIT_AUTHOR_DATE=2026-01-05T10:00:00Z
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com GIT_COMMITTER_DATE=2026-01-05T10:00:00Z
exec 2>&1
git -c init.defaultBranch=main init -q shop && cd shop
git --version
printf 'apples\n' > list.txt
git status --short
git add list.txt
git status --short
git commit -m "Add the shopping list"
git log
Output
git version 2.56.0
?? list.txt
A  list.txt
[main (root-commit) c6fb135] Add the shopping list
 1 file changed, 1 insertion(+)
 create mode 100644 list.txt
commit c6fb135760531277211142d3556246d6d0d6c46d
Author: Ada <ada@example.com>
Date:   Mon Jan 5 10:00:00 2026 +0000

    Add the shopping list

Concepts of the tool

A Git repository has three places for your files. The working tree is the files that you edit. The index, also called the staging area, holds the snapshot that the next commit will record. The Git directory, .git, holds the objects and the refs.1 7

export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com GIT_AUTHOR_DATE=2026-01-05T10:00:00Z
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com GIT_COMMITTER_DATE=2026-01-05T10:00:00Z
exec 2>&1
git -c init.defaultBranch=main init -q shop && cd shop
printf 'apples\n' > list.txt
git add list.txt
git commit -q -m "Add the shopping list"
printf 'bread\n' >> list.txt
git add list.txt
printf 'milk\n' >> list.txt
git status --short
git diff
git diff --staged
Output
MM list.txt
diff --git a/list.txt b/list.txt
index 5be50bc..e21221b 100644
--- a/list.txt
+++ b/list.txt
@@ -1,2 +1,3 @@
 apples
 bread
+milk
diff --git a/list.txt b/list.txt
index 950a188..5be50bc 100644
--- a/list.txt
+++ b/list.txt
@@ -1 +1,2 @@
 apples
+bread

MM means that the file differs between the last commit and the index, and also between the index and the working tree. git diff shows the second difference, and git diff --staged shows the first one.5

The object database holds four types of object. A blob holds the bytes of one file. A tree lists names, modes and the objects that they point to, so it is one directory. A commit points to one tree and to its parent commits. A tag object holds a tagger, a date, a message and a pointer to another object, usually a commit.6 29 Refs are names that point to objects. A branch is a ref that moves when you commit. A tag is a ref that stays.7 The commit, branch, merge and rebase pages explain these ideas in detail.

export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com GIT_AUTHOR_DATE=2026-01-05T10:00:00Z
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com GIT_COMMITTER_DATE=2026-01-05T10:00:00Z
exec 2>&1
git -c init.defaultBranch=main init -q shop && cd shop
mkdir docs
printf 'apples\n' > list.txt
printf 'Shopping\n' > docs/README
git add .
git commit -q -m "Add the list and a README"
git cat-file -p HEAD
git cat-file -p 'HEAD^{tree}'
git cat-file -p HEAD:docs
git cat-file -t HEAD:list.txt
git cat-file -p HEAD:list.txt
printf 'apples\n' | git hash-object --stdin
Output
tree b784625c0c88e980304c33247088e1a62014436d
author Ada <ada@example.com> 1767607200 +0000
committer Ada <ada@example.com> 1767607200 +0000

Add the list and a README
040000 tree cef55b8e07c6f20567cd388db7a52944e9c596b6	docs
100644 blob 950a188f0a4224ce6a1ac710915113df972aeb2c	list.txt
100644 blob 6d1077d2c0b9d9a161d4935b6ae07dddf4d44d48	README
blob
apples
950a188f0a4224ce6a1ac710915113df972aeb2c

The blob of list.txt has the same name as the hash of the bytes apples and a newline. The name depends only on the content.

Common tasks

git restore <file> puts back the version of a file from the index and drops your edits.9 git revert <commit> makes a commit that undoes an earlier commit, and keeps the earlier commit in the history.8 git tag names a commit, and git diff compares two commits.

export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com GIT_AUTHOR_DATE=2026-01-05T10:00:00Z
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com GIT_COMMITTER_DATE=2026-01-05T10:00:00Z
exec 2>&1
git -c init.defaultBranch=main init -q shop && cd shop
printf 'apples\n' > list.txt
git add list.txt
git commit -q -m "Add the shopping list"
git tag v1
printf 'bread\n' >> list.txt
git commit -q -a -m "Add bread"
printf 'oops\n' > list.txt
git restore list.txt
git revert --no-edit HEAD
git log --oneline --decorate
git diff v1 HEAD
Output
[main 62a0a6d] Revert "Add bread"
 Date: Mon Jan 5 10:00:00 2026 +0000
 1 file changed, 1 deletion(-)
62a0a6d (HEAD -> main) Revert "Add bread"
d54235d Add bread
c6fb135 (tag: v1) Add the shopping list

git diff v1 HEAD prints nothing, because the revert brought back the snapshot of v1.

To share work, you push commits to another repository and clone or fetch from it. git push -u also records the remote branch as the upstream of your branch. git clone copies the whole history.13 14

export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com GIT_AUTHOR_DATE=2026-01-05T10:00:00Z
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com GIT_COMMITTER_DATE=2026-01-05T10:00:00Z
exec 2>&1
git -c init.defaultBranch=main init -q shop && cd shop
printf 'apples\n' > list.txt
git add list.txt
git commit -q -m "Add the shopping list"
git -c init.defaultBranch=main init -q --bare ../server.git
git remote add origin ../server.git
git push -u origin main
git clone -q ../server.git ../copy
git -C ../copy log --oneline --decorate
Output
To ../server.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.
c6fb135 (HEAD -> main, origin/main, origin/HEAD) Add the shopping list

Configuration

Git reads configuration from three levels of files. The system file applies to all users. The global file, ~/.gitconfig, applies to all of your repositories. The local file, .git/config, applies to one repository and wins over the others.4 Git 2.46 gave git config subcommands such as get, set, unset and list. The older options such as --get still work but are deprecated.12 --show-origin and --show-scope tell you which file a value comes from.11

export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=Ada GIT_AUTHOR_EMAIL=ada@example.com GIT_AUTHOR_DATE=2026-01-05T10:00:00Z
export GIT_COMMITTER_NAME=Ada GIT_COMMITTER_EMAIL=ada@example.com GIT_COMMITTER_DATE=2026-01-05T10:00:00Z
exec 2>&1
git -c init.defaultBranch=main init -q shop && cd shop
git config set user.name "Ada"
git config set alias.lg "log --oneline --graph --decorate"
git config get --show-origin --show-scope user.name
git config get alias.lg
sed -n '/^\[user\]/,$p' .git/config
Output
local	file:.git/config	Ada
log --oneline --graph --decorate
[user]
	name = Ada
[alias]
	lg = log --oneline --graph --decorate

An alias such as lg adds a command: git lg runs git log --oneline --graph --decorate.11

Alternatives

Mercurial is also a distributed system. It stores the history of each file in a revlog. Each version is stored compressed, either whole or as a delta from the version before it.15 Subversion is a central system. Clients commit to one repository, and each commit makes a revision with a number.16 Fossil is a distributed system that stores its objects in a SQLite database, and it adds a wiki, tickets and a forum.17 Jujutsu is a version control system that can use Git repositories, and it changes how commits, conflicts and the working copy behave.18 Perforce (P4) is a central system. A user submits a changelist of files to a server.19

History and versions

The Linux kernel project used BitKeeper, a proprietary tool, from 2002. In 2005 the company behind it took away its free-of-charge status.20 On 2005-04-06 Linus Torvalds wrote to the kernel mailing list that he had stopped using BitKeeper and was looking at alternatives.21 He made the first commit of Git on 2005-04-07. Its message calls Git "the information manager from hell", and its README calls Git "the stupid content tracker".22 Junio C Hamano tagged v0.99.2 on 2005-07-26, and he has tagged the Git releases since then.23

Version Date Notes
1.0.0 2005-12-21 first release numbered 1.024
2.0.0 2014-05-28 first release numbered 2.025
2.23 2019 added git switch and git restore, which split the work of git checkout10
2.29 2020 finished SHA-256 object names, marked as experimental26
2.46 2024 added subcommands to git config12
2.56.0 2026-09-27 the version that ran the examples on this page27

References

  1. Scott Chacon and Ben Straub, Pro Git, 2nd edition, What is Git?, Snapshots, Not Differences; Nearly Every Operation Is Local; Git Has Integrity; The Three States
  2. Pro Git, 2nd edition, Installing Git, Installing on Linux; Installing on macOS; Installing on Windows
  3. git-scm.com, Install for macOS, Homebrew; MacPorts; Xcode Command Line Tools
  4. Pro Git, 2nd edition, First-Time Git Setup, the system, global and local configuration files; Your Identity
  5. Git documentation, git-status(1), OUTPUT, Short Format
  6. Pro Git, 2nd edition, Git Objects, the blob example; Tree Objects; Commit Objects
  7. Git documentation, gitglossary(7), index; working tree; ref; repository
  8. Git documentation, git-revert(1), DESCRIPTION
  9. Git documentation, git-restore(1), DESCRIPTION
  10. Git 2.23 Release Notes, the two commands git switch and git restore
  11. Git documentation, git-config(1), COMMANDS; OPTIONS, --show-origin and --show-scope; FILES
  12. Git 2.46 Release Notes, the operation mode options of git config replaced with subcommands
  13. Git documentation, git-push(1), DESCRIPTION; OPTIONS, -u
  14. Git documentation, git-clone(1), DESCRIPTION
  15. Mercurial wiki (archive), Revlog, the whole page
  16. Version Control with Subversion, for Subversion 1.7, Chapter 1, Revisions
  17. Fossil documentation, Fossil Versus Git, 2.1 Featureful; 2.3 Query Language
  18. Jujutsu documentation, Comparison with Git, Introduction; Overview
  19. P4 CLI Documentation (formerly Helix Core), p4 submit, Description
  20. Pro Git, 2nd edition, A Short History of Git, the whole section
  21. Linus Torvalds, Kernel SCM saga.., linux-kernel mailing list, 2005-04-06, the whole message
  22. git/git commit e83c516, Initial revision of "git", the information manager from hell, commit metadata, 2005-04-07; README
  23. git/git tag v0.99.2, tagger and date
  24. git/git tag v1.0.0, tagger and date
  25. git/git tag v2.0.0, tagger and date
  26. Git 2.29 Release Notes, SHA-256 support marked as experimental; the final leg of the SHA-256 transition
  27. git/git tag v2.56.0, tagger and date
  28. Git source tree, COPYING, the note above the GNU General Public License version 2
  29. Pro Git, 2nd edition, Git References, Tags

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/WDE7KC/text.yaml