Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 78 ยท Getting Started

Getting Started with Git

Learn what version control is, why Git won, how Git differs from GitHub, and run your first real Git repository right here in your browser with Git 2.56.

Every project starts out simple. You have a folder, a few files, and a vague plan. Then the folder grows. You copy report.docx to report-final.docx, then report-final-v2.docx, then report-final-v2-REALLY-final.docx. A teammate edits one copy while you edit another. A week later nobody knows which file has the good paragraph, and nobody dares to delete anything.

Version control is the cure for that mess. It is a tool that records every change you make to a set of files, who made it, when, and why. You can look at the past, compare any two moments, undo a mistake, and let several people work on the same project without overwriting each other. Git is the version control system that almost the whole software industry uses today. It was created in 2005 for the Linux kernel, and it is free, fast, and runs on every operating system.

This lesson explains what Git is, what it is not (it is not GitHub), and then lets you run a real Git repository in your browser. The next lessons build on it: How Git Works explains the model, and Your First Repository goes step by step.

What version control does for you

Think of Git as a time machine plus a notebook for your project.

  • History. Every saved state of the project is kept. You can go back to last Tuesday's version without having made a copy.
  • Accountability. Each saved state has an author and a message that says why it exists. Months later, "why did we change this line?" has an answer.
  • Safe experiments. You can try a risky idea on the side (a branch) and throw it away or keep it, without touching the working version.
  • Teamwork. Many people change the same files at the same time, and Git helps combine the work and points out real conflicts.
  • Backup and sharing. A repository can be copied to other machines, so there is never a single copy of the truth that can be lost.

None of this is only for programmers. Writers, designers, researchers and system administrators use Git too. It works best with plain-text files (source code, Markdown, configuration), but it can store anything.

Centralized and distributed systems

Older tools such as Subversion (SVN) used a centralized model: one server holds the history, and you check files out from it. If the server is down or you are offline, you cannot look at history or save your work.

Git is distributed. When you copy a repository (a clone), you get the complete history, not just the latest files. Every clone is a full backup and a full workspace. That has big consequences:

  • Almost every command is local and therefore very fast. No network is involved when you commit, look at the log, or create a branch.
  • You can work on a plane, in a tunnel, or behind a firewall, and share your work later.
  • There is no technical "master copy". Teams agree that one repository (for example on GitHub) is the official one.
TEXT
   Centralized (SVN)                 Distributed (Git)

      +--------+                    +--------+     +--------+
      | server |                    | clone  |<--->| clone  |
      +--------+                    | (full  |     | (full  |
       ^   ^   ^                    | history|     | history|
       |   |   |                    +--------+     +--------+
     you  Bo  Cy                         ^   \       /   ^
                                         |    v     v    |
                                         |   +--------+  |
                                         +-->| shared |<-+
                                             | remote |
                                             +--------+

Git is not GitHub

Beginners often mix these up, so let us be clear.

Git GitHub, GitLab, Bitbucket
What it is A program on your computer Websites (services) that host Git repositories
Needs internet No Yes
Does Records history, branches, merges Stores a shared copy, adds pull requests, issues, CI, permissions
Who makes it The Git project (open source) Companies

Git works perfectly without any of them. The hosting services add collaboration features around a shared copy of your repository. You will meet them later in GitHub Basics and GitLab, Bitbucket and Self-Hosting.

Your first Git session

Below is a complete, real session. It runs a genuine Git 2.56 inside our server-side sandbox, so every line of output is what Git actually printed. Press Run and read the output top to bottom.

The first lines set the author name, email and date through environment variables. Normally you store your identity once with git config (see Installing and Configuring Git). We use variables here so that the commit hash is the same on every run.

Bash Create a repository and make a commit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Here is what happened:

  1. git init turned the empty folder notes into a repository. It created a hidden folder called .git where all history lives.
  2. We created a file with echo. At that moment Git knew nothing about it; it was just an untracked file.
  3. git add todo.txt told Git "include this file in the next snapshot". This is called staging.
  4. git commit -m "..." recorded the snapshot together with a message.
  5. git log listed the history. You see one commit with its full 40-character hash, the author, the date and the message.

The hash after commit is the commit's ID. It is computed from the content, the author, the date, the message and the parent commit. Change any of them and you get a different hash. That is why the same example always prints the same hash for you and for everyone else.

In your own terminal the date and name in the log would be real ones. We pin them here only so the output is reproducible.

Seeing the history grow

A repository becomes useful when it has several commits. The next example adds more changes and then shows the history compactly with --oneline. It also uses git status, the command you will type more than any other. It tells you what Git thinks is going on.

Bash Three commits and a status check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

--oneline shows each commit as a short hash and its message, newest first. After the last edit, git status --short prints M todo.txt. The M in the second column means "modified in the working folder, not yet staged". You will learn to read all of these codes in Status, Diff and Seeing Changes.

The idea of snapshots

Many tools store history as a list of changes ("line 5 was added"). Git thinks differently: every commit is a snapshot of the whole project at that moment. If a file did not change, Git just points to the existing copy instead of storing it again, so snapshots are cheap. The next lesson, How Git Works, draws this picture in detail. For now remember the three verbs of everyday Git:

TEXT
edit files  --git add-->  staging area  --git commit-->  repository history

You change files, you stage the changes you want in the next snapshot, and you commit to record it.

Undoing is part of the design

Because every commit is kept, almost nothing you do in Git is permanent. Here is a taste: we change a file, decide we do not like the change, and restore the committed version.

Bash Throwing away an unwanted edit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

git restore todo.txt replaced the edited file with the version from the last commit. The empty status afterwards means the working folder matches the history again. There are many ways to undo things at different levels; Undoing Things gives you the full decision table, and The Reflog shows how to even recover things you thought you had destroyed.

git restore <file> discards uncommitted edits for good. They were never committed, so Git has no copy. Commit early and often, and you will rarely lose work.

Where does Git keep everything?

All history lives in the hidden .git folder at the top of your project. Delete it and the project becomes an ordinary folder again; copy it and you have copied the whole history. Let us peek inside, and ask Git where the top of the repository is, even from a subfolder.

Bash The hidden .git folder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

You will tour this folder properly in References, HEAD and the .git Directory. The important lesson now: Git commands work from any subfolder because they search upward for .git, and HEAD is simply a small text file naming the branch you are on (main here).

A short history of Git

Version control is older than most programmers. SCCS (1972) and RCS (1982) tracked single files and locked them while someone edited. CVS (late 1980s) added shared repositories and several editors at once, and Subversion (2000) fixed CVS's weakest points, such as changes that were not atomic. All of them were centralized.

The Linux kernel, one of the biggest collaborative projects, used a proprietary distributed tool called BitKeeper from 2002. In April 2005 the free-use arrangement ended, and Linus Torvalds decided to write a replacement. He wanted speed, a simple design, strong protection against corrupted history, and cheap branching for thousands of contributors. The first version of Git was written within days; by June 2005 the kernel itself was managed with it, and Junio Hamano became the maintainer who still leads the project. Git 1.0 followed in December 2005.

Hosting sites made Git the default: GitHub launched in 2008, alongside Bitbucket's and GitLab's later Git support. Today Git is cross-platform (Linux, macOS, Windows), and the same ideas you learn here work in every editor, hosting service and CI system.

Features that make Git different

  • Distributed: every clone holds the full history, so you can commit, branch and browse the log offline.
  • Fast and local: almost every command reads your own disk, so it takes milliseconds.
  • Cheap branches: a branch is a tiny file holding one hash, so you can create one for every idea and merge or throw it away.
  • Integrity: every object is named by a hash of its content, so silent corruption is detected and history cannot change without changing the hashes.
  • Staging area: you choose exactly what goes into each commit.
  • Non-linear development: thousands of branches can be developed in parallel and merged.
  • Free and open source: GPL licensed, with many graphical clients built on the same engine.

The next example shows the "content is the name" idea: two completely separate repositories compute the same hash for the same bytes, so they agree on what a file is without talking to each other.

Bash Same content, same name

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The first two lines print the identical hash, and a single extra character gives a completely different one. The clone ends with a log of two commits although it never contacted a server.

What our sandbox can and cannot do

Every example in this tutorial runs in a throwaway directory on our server. That has a few rules you should know about:

  • Fresh start every run. Files and repositories vanish when the run ends, so each example builds everything it needs.
  • No network. Commands that talk to GitHub (git push origin, git clone https://...) cannot run here. Instead we use local remotes: another repository on the same disk, which behaves almost exactly like a server. Where something truly needs the internet, the code block is clearly labelled read-only.
  • No interactive programs. Editors, pagers and anything that waits for you to type cannot run. We pass messages with -m and use scripts instead.
  • Fixed identity and dates, as you saw, so hashes are stable.

Everything that is explained here works the same way on your own computer, where you will also have an editor, a pager and your real network.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main difference between Git and GitHub?

    Git is the version control software and works offline. GitHub (like GitLab or Bitbucket) is a hosting service that stores a shared copy and adds features such as pull requests.
  2. 2. Why is Git called a distributed version control system?

    Each clone is a complete repository with all history. That is why commits, logs and branches are local and fast, and why no single server is required.
  3. 3. Which command turns an ordinary folder into a Git repository?

    git init creates the hidden .git folder that holds the history. git add and git commit need a repository to already exist.
  4. 4. In the basic Git workflow, what does git add todo.txt do?

    git add puts the current content of the file into the staging area. Only git commit records it in the history.
  5. 5. Why do the examples in this lesson set GIT_AUTHOR_DATE and GIT_COMMITTER_DATE?

    A commit hash depends on author, date, message and content. Fixing the identity and dates makes the output reproducible. In real work you simply let Git use the current time.

Summary

  • Version control records the history of your files; Git is the most widely used system for it.
  • Git is distributed: every clone has the full history, so most commands are local and fast.
  • Git (the tool) and GitHub (a hosting service) are different things; Git works fine without a network.
  • The everyday cycle is edit, git add (stage), git commit (record).
  • git init, git status, git log and git restore are the first commands to know.
  • In our sandbox each run starts fresh, has no network, and uses fixed identity and dates so output is reproducible.

Next: How Git Works: Snapshots and the Three Areas