# 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/): cd /tmp/ # Run the 'git' command and read the output, its a useful summary. git # Ask git what it knows about the status of the current directory. git status # Introduce yourself to git (set the variables for name and email): git config --global user.name "Nicola Chiapolini" git config --global user.email "nicola.chiapolini@physik.uzh.ch" # Verify that git has correctly stored your config (see 'git help config') git config user.name git config user.email # alternatively use `git config -l` # Create a directory for this exercise and enter it. mkdir demo_git_single_local cd demo_git_single_local # Create a new git repository within the current directory. git init # Verify that you have a brand new ".git/" directory... ls -al # ...and its content. ls -al .git/ # Check the status and compare the output to the one from last time git status # -- A First Commit -- # Create a file named simple_math.py with a single function # ``` # def add(a,b): # return a+b # ``` # (without the ```) cat > simple_math.py <> simple_math.py <` -m ...) git commit simple_math.py -m "add subtraction" # And again the status.. git status # -- Using the Staging Area -- # Add a function `multiply(a,b)` to simple_math.py cat >> simple_math.py <> simple_math.py # Wait, `subtract` has a wrong name as well. # # It should be `sub` not `subtract`. Fix it! sed -i 's/subtract(/sub(/' simple_math.py # Inspect the status and the changes. git status git diff # 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`) git add -p simple_math.py ? s y n # Check the status and the changes between the 3 different versions. git status git diff --staged git diff # Commit *only* the bugfix git commit -m "fix function name of subtraction" # and see how the status changes accordingly. git status # Verify that changes not added to the staging area were not recorded. git diff # Finish implementing `div` echo " return a/b" >> simple_math.py echo "" >> simple_math.py # check the status and the changes git status git diff # and commit the function. git commit simple_math.py -m "add division" # -- 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) # ``` cat > logger.py < simple_math.py # Oh no! What have you done! Shame! # # Look at the status and the local changes. git diff git status # Get rid of the untracked changes git restore simple_math.py # check that simple_math.py is good now git status cat simple_math.py # We are still missing a file. # # Check which commits affected logger.py git log -- logger.py git show HEAD # and revert bad commit. git revert HEAD --no-edit # (`--no-edit` to prevent opening the editor) # Verify that everything is back to normal. ls cat logger.py git status git diff #Relax: your work is safe.