4.6 Git Theory#

Estimated time to complete this notebook: 5 minutes

The revision Graph#

Revisions form a GRAPH

import os

top_dir = os.getcwd()
git_dir = os.path.join(top_dir, "learning_git")
working_dir = os.path.join(git_dir, "git_example")
os.chdir(working_dir)
%%bash
git log --graph --oneline
*   b14af2b Merge branch 'main' of github.com:alan-turing-institute/github-example
|\  
| * 7a1335d Add another Beacon
* | bdb0127 Add Glyder
|/  
*   7375d5c Merge branch 'main' of github.com:alan-turing-institute/github-example
|\  
| * a0e175d Add a beacon
* | e41e48f Translating from the Welsh
|/  
*   2ed3750 Merge branch 'main' of github.com:alan-turing-institute/github-example
|\  
| * 7b72dce Add Scotland
* | 16f7517 Add wales
|/  
* ef29071 Add Helvellyn
* 9cba9c4 Include lakes in the scope
* bd7c0f2 Add lakeland
* 43f3942 Revert "Add a lie about a mountain"
* ee151c1 Change title
* da208a3 Add a lie about a mountain
* 973ce32 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