Sub topic 3
What Git Is and Where It Fits
This chapter assumes you have read the earlier chapters on why version control exists and how local, centralized, and distributed systems differ. It also assumes you can open a terminal and run simple commands. Here you will learn what Git is, where it came from, what it does and does not do, and how it relates to services such as GitHub. The next chapter covers Git's snapshot model in detail.
What Git is
Git is a free, open-source, distributed version control system. It records the history of a set of files, lets you return to any recorded state, and lets many people work on the same files in parallel and combine their work later.
Two descriptions of Git are both accurate, and each explains different behavior:
- A version control system. You tell Git when to record a state of your project (a commit), and Git keeps the history.
- A content-addressable object store. Underneath, Git is a simple database. You give it content, and it stores that content under a key computed from the content itself. The version control commands are built on this store.
The second description explains many behaviors that look odd at first. Identical files are stored once. A changed file is detected by its key changing. A corrupted file is detected because its content no longer matches its key.
Where Git came from
Git was created in April 2005 by Linus Torvalds, the creator of the Linux kernel. The kernel project had been using a proprietary distributed tool called BitKeeper under a free-of-charge arrangement. When that arrangement ended, the kernel developers needed a replacement. No existing open-source tool met their needs, so Torvalds wrote one in a matter of weeks. Junio Hamano took over as maintainer later in 2005 and has led the project since. Git is released under the GNU General Public License version 2.
The kernel's needs shaped the design goals:
- Speed. Common operations such as committing, viewing history, and comparing versions should feel instant.
- Simple underlying design. The core data model should be small enough to reason about.
- Strong support for non-linear development. Thousands of parallel branches should be normal, and merging should be routine.
- Fully distributed. Every copy of a project should contain the whole history.
- Scale. A project the size of the kernel, with a large history and many contributors, should stay usable.
These goals explain Git's behavior today. Branches are cheap because they are only small pointers. History is available offline because every clone has all of it. Many commands feel low-level because the design favored a small, powerful core over a polished interface.
Git is local first
In a centralized system such as Subversion, most commands contact a server. In Git, almost every command works on your own machine. Committing, viewing history, comparing versions, creating branches, and switching between them all read and write only local files. Only a few commands talk to another computer, namely those that copy history between repositories: clone, fetch, pull, and push.
This has practical effects:
- You can work on a plane, on a train, or behind a failed VPN, and keep committing.
- Commands are fast because they do not wait on a network.
- The server is not special. A "central" repository is a convention your team adopts, not something Git requires.
- Nothing you do locally affects anyone else until you choose to share it.
You can see this for yourself. The following commands create a repository and store a piece of content in it, without any network, account, or server. They use Bash syntax, so on Windows run them in Git Bash, which is installed with Git for Windows.
mkdir fit-demo && cd fit-demo
git init
id=$(echo "Hello, Git" | git hash-object -w --stdin)
echo "$id"
git cat-file -t "$id"
git cat-file -p "$id"
git remote -vWhat each step does:
git initcreates an empty repository in the current directory.git hash-object -w --stdinreads the text from standard input, writes it into Git's object store, and prints its key. The-woption means "write"; without it, Git only computes and prints the key.git cat-file -tprints the type of the stored object, which isblobhere. A blob is Git's name for a file's contents.git cat-file -pprints the stored content, which isHello, Git.git remote -vprints nothing. The repository has no remote, and everything above still worked.
The key printed by the third command is 40 hexadecimal characters. The same text always produces the same key, on any machine.
Integrity through hashing
Git computes a hash of every object it stores. A hash is a fixed-length fingerprint calculated from content: any change to the content, even one character, produces a different fingerprint. Git uses these fingerprints as object names, and commits include the fingerprints of the content they refer to.
Three consequences follow:
- Corruption is detectable. If a stored file changes on disk, its content no longer matches its name, and Git can report the problem with
git fsck. - History is tamper-evident. Each commit's name depends on its parent commit's name. Changing an old commit changes its name, and so changes the name of every later commit. You cannot silently rewrite history without producing different names.
- Duplicates cost nothing. Two files with identical content produce the same key and are stored once.
By default Git uses SHA-1 for these hashes. SHA-1 is no longer considered safe against deliberate collision attacks, so since 2017 Git has used a hardened implementation that detects known attack patterns. Git also supports SHA-256 repositories, created with git init --object-format=sha256. Support for SHA-256 is still limited. Many hosting services and tools do not accept those repositories, so SHA-1 remains the practical choice for most work. Hashes in Git identify content for integrity. They are not digital signatures, so they do not prove who made a commit. Commit signing is a separate feature.
Plumbing and porcelain
Git's commands fall into two informal groups:
| Group | Purpose | Examples |
|---|---|---|
| Porcelain | High-level commands for people | `git add`, `git commit`, `git status`, `git switch`, `git merge` |
| Plumbing | Low-level commands for scripts and tools | `git hash-object`, `git cat-file`, `git update-ref`, `git rev-parse` |
Porcelain commands are the stable, friendly interface. Their output may change between versions to improve readability. Plumbing commands have stable output designed for programs to parse. If you write a script that reads Git's output, prefer plumbing commands or options such as git status --porcelain, which is designed to be machine-readable.
Git, GitHub, and other hosting services
Git and GitHub are often confused. They are different things made by different organizations.
| Name | What it is | Runs where |
|---|---|---|
| Git | The version control tool and file format | Your computer |
| GitHub | A web service that hosts Git repositories and adds collaboration features | Hosted service (also self-hosted as GitHub Enterprise Server) |
| GitLab | A similar service, available hosted or self-managed | Hosted or your own servers |
| Bitbucket | A similar hosted service from Atlassian | Hosted service |
| Gitea, Forgejo | Lightweight self-hosted services | Your own servers |
These services provide features Git itself does not: user accounts, access control, pull requests or merge requests for code review, issue tracking, automated builds, and a web interface for browsing code. Underneath, they store ordinary Git repositories. You can move a repository from one service to another because the history is a Git repository in every case, and your local copy remains complete.
Because of this, you can learn and use Git with no account on any service. This tutorial uses only your local machine until sharing is introduced in a later chapter.
Where Git sits in a typical workflow
The diagram shows the usual arrangement. You edit files on your machine and commit them to your local repository. A hosting service holds a shared copy that teammates and automated systems also use.
Only the arrows labeled push and fetch or pull need a network. Everything to the left of them is local.
How you use Git
Git is a command-line program first. Other tools are layers over it:
- The command line. The
gitprogram is the reference interface. Every feature is available there, and documentation and troubleshooting advice usually assume it. - Graphical clients. Tools such as Git GUI (bundled with Git for Windows), GitKraken, Sourcetree, and Fork display history as a graph and offer buttons for common actions.
- Editor and IDE integration. Visual Studio Code, JetBrains IDEs, Visual Studio, and others call Git for you and show changes inline.
Graphical tools and editors run the same Git underneath, and they all read and write the same repository. You can mix them. A commit made in an editor shows up in git log on the command line. This tutorial uses the command line because it shows exactly what Git is doing, and that knowledge carries over to every other tool.
Git compared with adjacent tools
The earlier chapter on version control models described the categories. This table places specific tools in them.
| Tool | Model | Notable characteristics |
|---|---|---|
| Git | Distributed | Cheap branching, full local history, very widely used |
| Mercurial | Distributed | Similar model to Git, with a more uniform command set, and a smaller user base |
| Subversion (SVN) | Centralized | Single central history, a revision number per commit, still found in older projects |
| Perforce Helix Core | Centralized | Handles very large repositories and large binary files, common in game and hardware development |
Git is the default in most open-source and commercial software development. Choose another tool when your situation calls for it, not out of habit. Perforce, for instance, suits teams with huge binary assets and a need for file locking. You will, however, encounter Git almost everywhere, so it is the one worth learning first.
What Git is not good at
Git is not the right answer to every problem. Knowing its limits helps you avoid common mistakes.
- Large binary files. Git stores each version of a file in full (compressing and packing similar objects when it can), so large files that change often make repositories grow quickly. The Git LFS extension keeps large files on a separate server and stores small pointers in the repository.
- Very large repositories. Projects with millions of files and long histories can make commands slow. Features such as partial clone, sparse checkout, and the file system monitor reduce this cost, but they take configuration.
- Backups. Pushing to a hosting service does give you another copy, but only of what you committed and pushed. Uncommitted work, ignored files, and local-only branches are not protected.
- Secrets. Because history is permanent and shared on every clone, a password or key that was committed once stays in the history even after you delete the file. Removing it requires rewriting history, and you should treat the secret as exposed and replace it.
- Deployment. Git tracks source. It does not build, test, or deploy anything. Those jobs belong to build and release tooling that reads from Git.
- Merging non-text files. Git merges text by lines. For binary formats such as images or office documents, it cannot combine two edits, and one version has to win.
Common misunderstandings
- "Git and GitHub are the same." Git is the tool. GitHub is one service that hosts Git repositories.
- "Git stores differences between versions." Conceptually Git stores a snapshot of the project at each commit. This is the subject of the next chapter. Git may compress storage using deltas internally, but that is an optimization, not the model you work with.
- "The server is the real repository." In Git, every clone is a full repository. A shared server copy is a convention.
- "A commit is automatically shared." A commit exists only in your local repository until you push it.
Check your understanding
Exercise
You are on a flight with no internet. List three Git operations you can still do, and name the kind of operation that you cannot do.
Show answer
You can commit changes, view history with git log, and create or switch branches (also compare versions with git diff). You cannot do operations that contact another repository, such as git clone from a server, git fetch, git pull, or git push, until you are back online.
Exercise
A teammate says "I deleted the password file in my last commit, so it is gone." Explain why this is wrong, and say what Git feature makes it so.
Show answer
Git keeps every earlier commit in the history, and every clone contains that history. The old commit that contained the file still exists and can be inspected or restored. This is a result of how Git stores content as permanent, hashed objects linked into a history. The password should be treated as exposed and changed.
Exercise
In the demo above, why does git remote -v print nothing, even though the other commands worked?
Show answer
The repository was created with git init and has no remote configured. Creating objects and reading them back are local operations that need no remote or network. A remote only matters when you share history with another repository.