Lecture Notes 5#

git#

work directory#

folder where we have our files locally (example recipe)

commits#

  • a commit (noun): a snapshot of our project at a given time that has been saved

  • to commit (verb): the act of saving changes to history

repository#

sequence of commits (history)

example#

starting code (green2red.py)

required_volume = 1
required_fat_content = 3 # in percent
available_fat_content = 1.5 
cream_fat_content = 40

conversion_ratio = (required_fat_content - available_fat_content)/(cream_fat_content-available_fat_content) 
replaced_volume =  conversion_ratio * required_volume

print(replaced_volume)
0.03896103896103896

Initialize#

We initialize the project folder for git

$ git init .

Check the state of our project#

$ git status
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        green2red.py

nothing added to commit but untracked files present (use "git add" to track)

Track our source file in git#

$ git add green2red.py

Save file to history#

$ git commit -m "initial backup" green2red.py

Further changes with git add /git commit cycle#

  • change formatting

  • introduce main function

  • Output with 1 decimal

  • Check on number of arguments

  • Allow for required volume on command line

  • initial backup

import sys

if len(sys.argv) > 1:
    required_volume = float(sys.argv[1])    
else:
    print("Usage:", sys.argv[0], "<required_volume>")
    exit()

required_fat_content = 3 # in percent
available_fat_content = 1.5 
cream_fat_content = 40

conversion_ratio = (required_fat_content - available_fat_content)/(cream_fat_content-available_fat_content) 
replaced_volume =  conversion_ratio * required_volume

print(f'Replaced volume: {replaced_volume:5.1f}')

Create a new branch for developent#

$ git branch refactor-to-function
$ git switch refactor-to-function

Changes in new branch#

  • change formatting

  • introduce main function

import sys

def main():

    if len(sys.argv) > 1:
        required_volume = float(sys.argv[1])    
    else:
        print("Usage:", sys.argv[0], "<required_volume>")
        exit()

    required_fat_content = 3 # in percent
    available_fat_content = 1.5 
    cream_fat_content = 40

    conversion_ratio = (required_fat_content - available_fat_content)/(cream_fat_content-available_fat_content) 
    replaced_volume =  conversion_ratio * required_volume

    print(f'Replaced volume: {replaced_volume:5.1g}')


main()

Merge to main#

$ git switch main
$ git merge refactor-to-function
$ git log --all --oneline
0df9df8 (HEAD -> main, refactor-to-function) change formatting
af1f422 introduce main function
197b320 Output with 1 decimal
50f0d3e Check on number of arguments
613d9d6 Allow for required volume on command line
e362b8b initial backup

git diff#

can be used to examine difference between versions

  • git diff (compares work directory with cache)

  • git diff --staged (compares cache with repository’s active branch)

  • git diff af1f422 0df9df8 (compares changes between two chosen commits)

  • git diff main refactor-to-fanction) (compares two branches)