# git exercise. Single developer and local repository. # # In this demo/exercise we will develop a toy math-library # with simple functions: `add`, `sub`, `mul` and `div` # The functions are trivial, but to understand git, let's assume they were not. # Each function should be committed seperately. # # Hint: the demo and solutions modify all files using command-line tools. # This allows to automatically run this demo. Feel free to use an editor # instead. # -- Setting Up -- # First move to a safe place (e.g. go into the tmp folder with cd /tmp/): # Run the 'git' command and read the output, its a useful summary. # Ask git what it knows about the status of the current directory. # Introduce yourself to git (set the variables for name and email): # Verify that git has correctly stored your config (see 'git help config') # alternatively use `git config -l` # Create a directory for this exercise and enter it. # Create a new git repository within the current directory. # Verify that you have a brand new ".git/" directory... # ...and its content. # Check the status and compare the output to the one from last time # -- A First Commit -- # Create a file named simple_math.py with a single function # ``` # def add(a,b): # return a+b # ``` # (without the ```) # Check the status and compare the output to the one from last time # tell git to keep track of simple_math.py (put it in the staging area) # and check how the status changes # Now record commit to the repository. # Check status again, it is instructive. # -- Modifying Files and Committing Changes -- # add a function `subtract(a,b)` calculating a-b to simple_math.py # See how the status changes accordingly. # Show the changes between current and staged files. # Directly commit simple_math.py (`git commit ` -m ...) # And again the status.. # -- Using the Staging Area -- # Add a function `multiply(a,b)` to simple_math.py # And check the status... # Add the new changes to staging area # and check what is going to be committed # Oh! We wanted to use 3-letter function names # # Rename `mutliply` to `mul` # and see how the status changes accordingly. # Then look at the changes between the 3 different versions. # Update the staging area, # check status and changes # and finally commit. # -- Splitting Changes into several Commits -- # start to work on the division function # # add `def div(a,b):` to simple_math.py # Wait, `subtract` has a wrong name as well. # # It should be `sub` not `subtract`. Fix it! # Inspect the status and the changes. # We are not done with `div` yet but we want to commit the bugfix now. # In addition, we would like to keep the bugfix in a separate commit: # # Add only the bugfix to the staging area. (Hint: use `git add -p`) # Check the status and the changes between the 3 different versions. # Commit *only* the bugfix # and see how the status changes accordingly. # Verify that changes not added to the staging area were not recorded. # Finish implementing `div` # check the status and the changes # and commit the function. # -- Changes Affecting Several Files -- # Now we want to add output to each of our functions. # # Create a file `logger.py` with content # ``` # def logger(a,b): # print(a,b) # ``` # Check the status # and understand the output of git diff # Now add a call to `logger()` to each function in simple_math.py. # (Don't forget to import the function) # Inspect the status. # Prepare the staging area for this commit. # Check that the staged changes include everything # Commit # Check that everything looks good. # -- Looking around -- # See the logs of all commits. # See the logs of all commits with the git GUI. # Show the changes made by the bugfix-commit. # (get the commit-hash from git log) # -- Unwanted Changes -- # Go crazy. # # Remove logger.py # and commit the change. # Replace the content of simple_math.py # Oh no! What have you done! Shame! # # Look at the status and the local changes. # Get rid of the untracked changes # check that simple_math.py is good now # We are still missing a file. # # Check which commits affected logger.py # and revert bad commit. # Verify that everything is back to normal. #Relax: your work is safe.