Lab Session 2: GitHub & The Shell
Contents
Lab Session 2: GitHub & The Shell¶
Statistics 159/259, Spring 2022
Prof. F. Pérez and GSI F. Sapienza, Department of Statistics, UC Berkeley.
01/31/2022
Menu for today:
Recap on Lab 01. Quick overview of how to define functions and write errors and exceptions.
Homework No1 Announcement. You can create a repository template for Homework No1 by following the following link in GitHub Classroom. Also remember to send the two paragraphs for the reading assignment No1 (deadline at 9PM).
Playing with the Unix Shell. We are going to manipulate files and directories in GitHub but using the unit shell.
Learning goals for today: practicing GitHub and bash commands in the shell. Until now, we have seen (at least) three different ways of manipulating files and directories (including those we have inside a repository):
Simple manipulation using the files browser in JupyterLab
Using the Midnight Commander from the command shell
Using Bash as command-line interface (CUI) from the command shell.
The idea is that you get familiar with all these. So, even when you are able to all tasks with at least one of the last three, try to learn how to use the others. For example, the Midnight Commander (mc
in the terminal) is very handy and fast at the moment of manipulating file,; while for most of the GitHub manipulations we will be using the commands from the shell.
Useful links:
GitHub, always GitHub…¶
We will be practicing the file and directory commands for manipulations of files inside our test
repository. Remember, during the lectures we initialize a repository called test
that we use for practicing some basic git commands (See how to create an empty repository in the Git Tutorial).
%%bash
git init test
Now, in order to push the repository to GitHub we need to create a new empty repository page. To do that, we go to our GitHub account, then Repositories
window and new
. After we have done this, we will see a list of commands that tell us how to create a new repository from the command line. You may have done some of these steps already. A description of the full process can be found here.
%%bash
git branch -M main
git remote add origin <url>
git push -u origin main
Warming up with the Unix Shell¶
For this section, we are going to follow Chapters 2 & 3 of Research Software Engineering with Python. There we can find a comprehensive explanation about how to use different commands in the command shell. These are some useful commands (for an extensive list of commands, refer to this Bash Shell Reference)
whoami
pwd
: Print Working Directoryls
: Listingcd
: Change Directorymkdir
: Make Directorymv
: Move. This command can be also used to rename a file.cp
: Copyrm
. Remove
Remember that we can always communicate with the Shell on a Jupyter Notebook by adding !
at the beginning of a line or with the magic command %%bash
just at the beginning of the cell.
We can also add flags to these commands to give further information of how do we want the program to be run.
! ls -l
When creating directories and files, try to pick names that avoid confusion with the bash syntax. For example, don’t use spaces and don’t start names with -
.
Now, let’s do our first push to our test
repository:
### [LIVE DEMOSTRATION]
%%bash
git init test
git add text1.txt
git commit -m"first commit"
! echo "Hello" > text.txt
! cat text.txt
! git add text.txt
! git commit -m"My first commit"
#! git commit
%%bash
git branch -M main
git remote add origin https://github.com/facusapienza21/test2.git
git push -u origin main
Now, there are different variations to the previous protocol… can you think in any?
Exercise 1: Push new files to GitHub¶
Create a text file with some text inside using just the shell. Remember the protocol to make push/pull in the Hub!!! In order to this to work, remember to have installed the app for the authentication and that the app has permissions on your repository.
Until now we were working with simple text files. Create a Jupyter Notebook and add it to your new repository (you can do this using the directly thought JupyterLab).
...
Exercise 2: Modifying files¶
Make some modifications to one of the files inside your test
repository. These changes can include modifications in a text file or actual code in a script of Jupyter notebook. Can you open and read such file with the Midnight commander? If so, can you add further modifications to it?
Try to add your commit message directly using the command git commit -m"my message"
and by using the Emacs editor (this will be launched after you enter git commit
without any message). If you made a mistake or want to change your last commit message, you can use git commit --amend
to change it.
Type your answer here, replacing this text.
Exercise 3: Dotfiles¶
Make more modifications to the files in your repository, but this time do the interaction with GitHub with the dotfiles
configuration. Check the git log
, who is doing the modifications in the repository? If your username is no there, you may need to modify your .gitconfig
file… You can also configure your editor for GitHub inside .bashrc
.
Type your answer here, replacing this text.
Exercise 4: More commands¶
Now that you are more familiar with GitHub, let’s try to do some more sophisticated commands from the shell. Try to rename and move files inside the repository. Try changing some of them and removing some others. As you are doing this, use at least once each one of the bash commanders enumerated at the beginning of this notebook.