Version Control with Git

Any project that changes over time, from a lab report to an operating system, raises the same questions: what changed, when, who changed it, and how do you get back a version that worked? A version control system answers them by recording the project's history as a series of saved versions. Git is the most widely used one. It keeps the complete history inside every copy of the project, so you can work offline and still share work with others through a server.

Repositories and commits

A repository is a project folder together with Git's database of its history, which Git keeps in a hidden .git subfolder. You turn an existing folder into a repository with git init, or copy an existing repository, history included, with git clone.


The history is a chain of commits. Each commit is a snapshot of every tracked file at one moment, together with the information that places it in the history:


commit=snapshot of tracked files+parent commit ID+author, date, message\text{commit} = \text{snapshot of tracked files} + \text{parent commit ID} + \text{author, date, message}


Snapshots are cheap because Git stores identical file contents only once and shares them between commits, so an unchanged file costs almost nothing in a new commit.


Git names each commit by hashing all of that data. Most repositories use the SHA-1 format, in which an ID is 40 hexadecimal digits:


40 digits×4 bitsdigit=160 bits40\ \text{digits} \times 4\ \frac{\text{bits}}{\text{digit}} = 160\ \text{bits}


Repositories created in Git's newer SHA-256 format use 64-digit IDs instead. Commands such as git log --oneline print only the first seven or more digits, lengthening the abbreviation whenever that is needed to keep it unique.


Because the parent's ID is part of the hashed data, changing anything in a commit gives it a new ID, and every later commit built on it must be rewritten with new IDs too. An altered history is therefore easy to detect.

Three places a file lives

While you work, each tracked file can exist in three versions:

  • The working tree: the ordinary files on disk that you open and edit.
  • The staging area, also called the index: the proposed next snapshot. git add copies a file's current contents into it.
  • The repository: the commits. git commit saves exactly what is in the staging area as a new commit whose parent is the previous one.

A file Git has never been asked to record is untracked; it stays out of every commit until you stage it with git add.


Three boxes labelled working tree, staging area and repository, joined by arrows marked git add and git commit, above a chain of three commits each pointing to its parent.



The table lists the everyday commands. In the last row, HEAD means the most recent commit.


Command

What it does

git init

Creates an empty repository in the current folder

git clone URL

Copies a repository and its full history

git status

Lists staged changes, unstaged changes and untracked files

git add file

Copies the file's current contents into the staging area

git commit -m "msg"

Saves the staging area as a new commit

git log

Lists commits, newest first

git diff

Compares the working tree with the staging area

git diff --staged

Compares the staging area with the last commit

git commit --amend

Replaces the most recent commit with a new one that has a new ID

git revert HEAD

Adds a new commit that undoes the most recent one


Amending does not add to the history: the replacement takes the old commit's place, and the old one no longer appears in git log. Reverting keeps the original commit and records its opposite as one more commit.



Sharing work through a remote

A remote is another copy of the repository, usually on a hosting server; git clone names the one it copied from origin. git push sends your new commits to the remote, and git pull fetches commits that others have pushed and combines them with yours. Committing, viewing the log and comparing versions all happen locally; only push and pull need the network. A commit exists only on your machine until you push it.

What to remember

  • A repository is your project plus the .git database of its full history.
  • A commit is a snapshot plus its parent's ID and metadata, named by a hash of all of it.
  • git add stages the current version of a file; git commit saves only what is staged.
  • git diff compares the working tree with the staging area; git diff --staged compares the staging area with the last commit.
  • Push and pull move commits between your repository and a remote.

Example: Tracking one file through staging and a commit

A student's project folder is already a Git repository with one commit. That commit holds main.py, which is 40 lines long, and README.md, and the working tree matches it exactly. The student then does the following, in order:

  1. Edits main.py, adding 12 new lines and deleting 3 existing lines, changing nothing else.
  2. Creates a new file, notes.txt.
  3. Runs git add main.py.
  4. Adds 4 more new lines to main.py.

a) For each of the three files, state where git status lists it now.


Compare each file's version in the working tree, the staging area and the last commit.

  • README.md matches both the staging area and the last commit, so it is ________ and git status does not list it at all.
  • main.py was staged after step 1, so the staging area holds the step-1 version, which differs from the last commit. It is listed under Changes to be committed. Step 4 then changed the working tree after staging, so the working tree differs from the staging area as well, and the file is also listed under Changes not staged for commit.
  • notes.txt has never been added, so it is listed under Untracked files.

So main.py appears in two sections, notes.txt appears as untracked, and README.md does not appear.


b) The student now runs git commit -m "Add input parsing". Which version of each file does the new commit record, and what change summary does Git print?


A commit saves exactly what is in the staging area. The staged main.py is the step-1 version, which has 40 + 12 − 3 = ________. The 4 lines added in step 4 were never staged, so they are not in this commit.


notes.txt was never staged, so the commit does not include it. README.md is carried into the new snapshot unchanged.


Compared with the previous commit, only main.py differs, with 12 lines added and 3 removed, so Git prints:


1 file changed, 12 insertions(+), 3 deletions(-)


The commit records the 49-line main.py and the unchanged README.md; notes.txt remains untracked.


c) Right after the commit, the student runs git diff and then git diff --staged. What does each command report?


git diff compares the working tree with the staging area. After the commit, the staging area matches the new commit, so it holds the 49-line main.py. The working tree version has 49 + 4 = ________. The only difference is the 4 lines from step 4, so git diff shows main.py with 4 insertions and no deletions. notes.txt is untracked, so git diff ignores it.


git diff --staged compares the staging area with the last commit. The commit was made from the staging area a moment ago and nothing has been staged since, so the two are identical and the command prints nothing.


d) The student's git log --oneline shows each commit ID abbreviated to 7 hexadecimal digits. How many different 7-digit abbreviated IDs are possible?


Each hexadecimal digit has 16 possible values, so it carries ________. Seven digits carry 7 × 4 = ________, and the number of distinct values is


167=228=268 435 45616^7 = 2^{28} = 268\,435\,456


There are 268,435,456 possible 7-digit abbreviations, far fewer than the 21602^{160} possible full SHA-1 IDs. In a very large repository two commits can share the same first 7 digits, which is why Git prints a longer abbreviation whenever 7 digits would be ambiguous.

Match each Git term with the description that fits it. Each description is used exactly once, and every term has exactly one match.

Match each term
A.

A saved snapshot of tracked files with an author, a message and a parent ID

B.

The ordinary files on disk that you open and edit

C.

A project folder plus the hidden database of its full history

D.

Another copy of the project, often on a server, that you push to and pull from

E.

The proposed next snapshot, built up with git add

F.

A file in the project folder that Git has never been asked to record

Untracked file

Remote

Staging area

Working tree

Commit

Repository

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A teammate asks you to fix a typo in a file stored in a shared repository on a hosting server. You do not yet have any copy of the project on your computer. Put these steps in the order that gets your fix onto the server.

  1. 1.

    Open the file in the working tree and correct the typo.

  2. 2.

    Run git commit with a message to record the snapshot locally.

  3. 3.

    Run git push to send the new commit to the server.

  4. 4.

    Run git clone with the repository's URL to copy the project and its history.

  5. 5.

    Run git add on the file to stage the corrected version.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

You have just run git commit in a local repository that is connected to a remote on a hosting server. Which of the following statements about the commit you created are true? Select all that apply.

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors