6. Git Theory#

The revision Graph#

Revisions form a GRAPH

import os

try:
    from google.colab import drive  # type: ignore

    drive.mount("/content/drive")
    drive_dir = "/content/drive/MyDrive"
except ImportError:
    print("Not running on colab")
    drive_dir = os.path.join(os.getcwd(), "drive", "MyDrive")
    os.makedirs(drive_dir, exist_ok=True)

print(f"Drive dir: {drive_dir}")

git_dir = os.path.join(drive_dir, "learning_git")
working_dir = os.path.join(git_dir, "git_example")

if os.path.exists(working_dir):
    print(f"Git directory: {git_dir}")
    os.chdir(working_dir)
else:
    print("Start from the beginning")
Not running on colab
Drive dir: /mnt/nvme1n1p2/home/yj.lee/workspace/projects/lecture/book/lectures/softeng/vcs/drive/MyDrive/learning_git/git_example/drive/MyDrive
Start from the beginning
%%bash
git log --graph --oneline
* cc360cb Add Glyder
* e5451fd Add another Beacon
* b101602 Translating from the Welsh
* ac80a2f Add a beacon
* 54fa40f Add wales
* 5524925 Add Scotland
* 4fa39c8 Add Helvellyn
* d2b4434 Include lakes in the scope
* b943bed Add lakeland
* 9793609 Revert "Add a lie about a mountain"
* 6626d56 Change title
* 453f5bc Add a lie about a mountain
* 74efc42 First commit of discourse on UK topography

Git concepts#

  • Each revision has a parent that it is based on

  • These revisions form a graph

  • Each revision has a unique hash code

    • In Sue’s copy, revision 43 is ab3578d6

    • Jim might think that is revision 38, but it’s still ab3579d6

  • Branches, tags, and HEAD are labels pointing at revisions

  • Some operations (like fast forward merges) just move labels.

The levels of Git#

There are four Separate levels a change can reach in git:

  • The Working Copy

  • The index (aka staging area)

  • The local repository

  • The remote repository

Understanding all the things git reset can do requires a good grasp of git theory.

  • git reset <commit> <filename> : Reset index and working version of that file to the version in a given commit

  • git reset --soft <commit>: Move local repository branch label to that commit, leave working dir and index unchanged

  • git reset <commit>: Move local repository and index to commit (“–mixed”)

  • git reset --hard <commit>: Move local repostiory, index, and working directory copy to that state