In Lecture 1 I asked you to use git from the command line. Macs come with a command line app (terminal.app) and since Macs are loosely similar to Unix the terminal commands are pretty much the same as those in Linux distributions. Windows is a bit different, and because of this, I had you install git BASH if you are using a Windows machine. What this means is that, even if you are using a Windows OS, you can still use the nice Unix commands as long as you are in the git BASH terminal.
It is true that there are various options for using git
from the Desktop (e.g. see GitHub Desktop). However, it is more useful for you to be able to do things from the command line. The reasons for this are that the command line is not going anywhere any time soon and that I can't predict what GUI you're future company/group will want you to use. You'll be much more versatile using the command line.
What this means, is that I have to teach you a little bit about how to navigate with the command line. There are a few essential commands that you absolutely must know. You'll pick up the rest as you go through you life. Here are the essential ones:
Let's try to work with these a bit. In the following steps, you should hit the return
key after typing each command.
pwd
. The output should be your home directory.ls
. You should see all the files and directories in your home directory. Notice that if you type ls -l
you get a list with more information where the files and directories are ordered alphabetically. Typing ls -lt
sorts the list by date/time created with the most recent file/directory at the top. ls -ltr
just reverses the order of the sort; now the most recent file/directory is at the bottom.mkdir mydir
. Voila! You have just created a new directory. Try ls
again. Do you see the new directory?cd mydir
. You just changed into your new directory. Type pwd
to see where you are.echo 'Hello world!' > newfile.txt
ls
. You should see the next text file!cat newfile.txt
.newfile.txt
to README.txt
. Just execute mv newfile.txt README.txt
. Type ls
again if you wish.newfile.txt
. Just do cp README.txt newfile.txt
. As usual, ls
will show you what you want to see.newfile.txt
should actually be in a different directory? Let's do the following:mkdir newdir
mv newfile.txt newdir\
pwd
(just to see the current path)mv newfile.txt path_from_pwd/newdir/
cd ..
..
stands for the current directory and ..
stands for the parent directory (that is, the directory containing the current directory. Hence, cd ../
changes to the parent directory of the current directory.cd ../../
. You can use the same pattern for n
directories.rm mydir
. You should see the message rm: mydir/: is a directory
. rm
cannot be used to removed directories as-is. It can only remove files.rm -rv mydir
. The -r
option says to recursively remove the directory and any contents. The -v
option says to be verbose while removing files.That was a whirlwind tour through a few of the more useful Unix commands. It won't take you long to use them effectively.
Note: If you want more information about any command, just type man command_name
into the terminal. This will bring up the manual page for that command. You will see all sorts of information. To scroll down, just hit the space bar
. To exit the man page
just type q
(for quit). Here's an example of a portion of the man page
for the rm
command:
Suppose you want to copy a file from a different location to your current directory. Well, recall that the current directory is represented by the .
object. Here are a few things you can do:
cp path_to_file/file .
: Will copy file to your current locationcp path_to_file/file ./sub_dir
: Will copy file to the directory sub_dir
in your current locationcp ../file .
: Will copy file from the parent directory to your current location
There are other, similar patterns to accomplish roughly the same thing. You'll figure them out as you go through life.In the last exercise, you learned out to create a simple .txt
file. Of course, you really don't want to use the echo
command to write programs. That would be insane. Many of you will write programs using Interactive Development Environments (IDEs) such as Spyder. Spyder is shipped with Anaconda which we will be using when we get to Python. However, I would strongly recommend that you learn how to use a text editor to write your programs and documents.
There has been a battle raging over whether or not programmers who use IDEs are true developers. The answer is that they are and so are people who use text editors. One major advantage of using a text editor over an IDE is that you tend to learn what's going on "under the hood" which in turn makes you a better developer. Many of you will go on to use IDEs in your careers so I want to make sure that you learn how to use a text editor.
There are a variety of text editors (nano, vi, vim, emacs, notepad++, atom, sublime, gedit...). My preferred text editor is vim but it really comes down to personal preference. vim is shipped with most operating systems (which is really nice) and it is also included with git bash.
There are a few vim commands with which you should become familiar. First, to open a file (or a new file) simply type vim filename
. You will get a new file if that filename does not yet exist. Otherwise, you will see the contents contained in filename
.
In this exercise, you will edit the README.md
file in your private repository. The .md
extension stands for markdown. You should probably learn Markdown. It's super easy and quite nice. GitHub also plays nicely with Markdown.
ls
. You should see README.md
.README.md
using your favorite text editor. This exercise will use vim, but I don't care what you use as long as it is a text editor. To open, type vim README.md
.# cs207-F17
.c
.v
and move your cursor to the end of the line using the arrow keys. v
means that you're entering virtual mode
.d
. You have just deleted the highlighted text. i
. You have just entered insert mode
.esc
. You have now exited insert mode
and returned to normal mode
. :w
followed by the return
key.:q
followed by the return
key.:wq
.Great! Now you know the basics of using the vim text editor to edit files. You'll pick up a lot more commands the more you use vim. Here is a vim cheatsheet (one of many) vim cheatsheet.
Now you should update your local repository.
git status
. This tells you which files you changed. You should see that README.md
was modified.git commit -a -m "Updated README file with more informative heading."
. This will register the changes in your local git repo (but not the remote repository yet!). The -m
option means that you're specifying a commit message
and the string in quotes is the informative commit message. All of this is followed by the file you're committing. We'll learn a lot more about this in the coming lectures.git push
. This will push your changes to your remote repository on GitHub. Please do the following:
cs207work
. Please rename it cs207_firstname_lastname
. If you have already done this, then you're done and you can skip to Step 2. If not, here's how you can rename your repo:Settings
tab. Change the name under Repository name
.cs207work
if you're doing these steps.git remote set-url origin url_to_your_repo
where url_to_your_repo
will be something like https://github.com/github_username/cs207_firstname_lastname.git
. What you just did was change the url of the remote repository. Recall that origin
is the short name of your remote repository.git remote -v
. It should list the correct url now.cd ..
.mv cs207work cs207_firstname_lastname
. You just renamed the directory!Settings
again.Collaborators
on the left side.You may have noticed that you are always promted for your GitHub username and password any time you try to interact with GitHub. That can be a bit annoying, especially if you're doing a lot of git
work. To overcome this annoyance, you can set up SSH keys. Rather than re-write everything for you, I'll refer you to the GitHub website which has detailed instructions for Linux, Mac, and Windows users: Connecting to GitHub with SSH. Follow these steps:
Now you can clone your GitHub repos using https
or ssh
. GitHub recommends using https
(like we've been doing), but it's really up to you. Here's a discussion: Why does GitHub recommend HTTPS over SSH?.
Of course, if you really do want to use ssh
instead of https
then you'll have to change your remote url. Remember, we set up our remotes using https
. For example here is what my current remotes look like:
To change the remote to ssh just do
git remote set-url upstream git@github.com:IACS-CS-207/cs207-F17.git
where upstream
is the short name for the remote repo that you're referring to and git@github.com:IACS-CS-207/cs207-F17.git
is the ssh address of the remote repo.
Now here is what my repos look like:
You can change all of your remotes to be through ssh
if you wish or keep them as https
.
All of our coding work will be done in Python this semester. We will work with the Python3 distribution provided by Anaconda. Anaconda works with Windows, Mac, and Linux operating systems. You should download and install it now: Anaconda Download. Install the Python 3.6 version!
Once you have successfully installed Anaconda, you can start coding in Python. But first, for this course you need to be able to work with Jupyter notebooks. In fact, all of the lecture exercises will be done in Jupyter notebooks and some of the homeworks will be too.
There are two ways to open a Jupyter notebook. One is to open the Anaconda Navigator
and Launch
a Jupyter notebook. The other way is to launch a notebook from the command line. We'll continue working from the command line. Please navigate to the homeworks/HW2
directory now using the commands from Problem 1. To open a Jupyter notebook from your current directory just type jupyter notebook .
.
Start a new Python3 notebook by clicking on Python 3
in the New
dropdown menu.
You can rename the notebook by clicking on the current name at the top of the page. You should name this assignment HW2-final
(as per the instructions on the course webpage).
You can write Python code but we'll put that on hold for now. For this first foray, just write some basic markdown. You can change the character of the cell by selecting the Code
dropdown menu and choosing Markdown
.
Notice that the cell is highlighted in blue. In order to actually edit the cell, you should hit the return
(or enter
) key. Now the cell should be be highlighed in green and your curser should be blinking inside the cell. You're ready to add content!
As mentioned previously, Markdown is a very easy language to work with. Now that you have your own Jupyter notebook open and have selected the Markdown cell, you're ready to write some Markdown. Here's a Markdown cheatsheet with everything you need: Markdown Cheatsheet.
The task is to write a short Markdown document. Your Markdown document must have at least one of the following:
You can include other features if you want. Don't spend too long; this exercise is meant to simply introduce you to Markdown and to get you started working with Jupyter notebooks. You can even use Markdown to write the documentation for your final project! Once you've added all the content you want to add, you would like your notebook to display more than just ugly text and commands. To do this, you just run the cell. I usually do that by using the keyboard sequence control-return
. Of course, you can also do it from the toolbar menu: Help - Keyboard Shortcuts
you will find all sorts of shortcuts that make your life easier and the time you spend in Jupyter more efficient.
Once you've completed your Markdown document you can save your notebook. Jupyter saves the notebook periodically but it's good practice to save it yourself. After saving the notebook, you can close it by opening the File
menu and clicking on Close and Halt
. control-C
. Now everything is closed!
We just started digging into git
recently, but you need to commit the changes you just made to your repo. This will be the last time I ask you to blindly type in git
commands. Maybe you're starting to feel more comfortable with the git
workflow now anyways. Follow these steps:
git status
. You should see something like the following image (with the appropriate modified files, usernames, etc.): modified:
. This is because git
doesn't know about it yet! Your new notebook should be listed under Untracked files
. You need to tell git
about your new notebook. To do that, just type git add new_filename
.git status
again. You'll see something similar, but not identical, to: git
is aware of the new file. Next, you need to commit that file to your local repository. To do so, just type git commit -m "descriptive message"
. I might suggest a description like L2 exercises and first Markdown page. You should see something like: git push
. You'll see something that looks like: And now you have the basic git
workflow down. Of course, we haven't talked about what git
is actually doing yet. And we'll have to discuss what to do when things go wrong. But at this point, using steps 1-6 above, you will be able to make local changes and update your GitHub repo.
Open your browser and go to your GitHub repo. You should see the changes along with your commit message.