Lecture 15: Version control and git

Reading (if you want more detail):

Pro Git, Chapters 1, 2, 3, 10.

Git

Basic idea:

Important conceptually are commits and branches

Commits

A commit records the state of the repository at a particular point in time

Contains:

You should think of git as storing a set of commits.

Branches

Problem: if you’re making changes (= adding commits), you probably want a pointer to the most recent commit. We would like branches to move when we make a new commit.

Start with three commits, two branches

Add a commit:

Switch branches with checkout:

Merging and rebasing

Setup:

Solutions:

Merge

Starting out:

How we want the merge to look:

After running

git checkout master
git merge iss53

Rebase

Rebasing branch2 onto branch1:

Starting out:

After running:

git checkout experiment
git rebase master

After running:

git checkout master
git merge experiment

How all the data is stored

Three basic types of objects:

All these objects are stored by their hash

Hashes

Blobs

Trees

To see the tree, you can use:

git cat-file -p master^{tree}

And the output might look like this:

100644 blob 01b480b010b7fe66e312e1271dd24e128f3a0290    .gitmodules
100644 blob 1d17afb2a980076fc389f3d2747b0bfefd4df839    Dockerfile
100644 blob 716007c1456163b933cb086acae151fc6a24ca6d    README.md
100644 blob 9af5513cf53dfbdedbc69ec43865dec054de0ccd    SConstruct
040000 tree 100d47915afe22615ff111d390170c7265900b7a    analysis

Conceptually, if we have a directory containing

Git would store a snapshot of the directory as three blobs and two trees:

Commits

Putting everything together, we get a graph that describes the files that were present at different commits:

How to create a commit

Remember: a commit is a snapshot of the directory structure

For example:

git init test
cd test
git status
echo "test file" > test.txt
git status

Now we have one untracked file, which means it is not in the staging area.

If we run

git add test.txt
git status

We get output

On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   test.txt

which tells us that test.txt is now in the staging area.

We can now commit what we have in the staging area:

git commit -m "first commit"

which will give us:

[master (root-commit) 8e9c4cc] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

This tells us:

Overall