linux and git boot camp - cs.cmu.edu
TRANSCRIPT
Carnegie Mellon
Connecting Clients
SSH Windows users: MobaXterm, PuTTY, SSH TectiaMac & Linux users: Terminal (Just type ssh)ssh [email protected]
2
Carnegie Mellon
I Need You To Make A Directory
$ ls$ cd private$ mkdir 15-213$ cd 15-213■ All work MUST be done in private
directory or any subfolder within■ For more information on AFS directories
and permission see https://www.cs.cmu.edu/~help/afs/afshome.html
3
Carnegie Mellon
File Transfers ■ Useful for transferring handins to local machine for
submission to Autolab.■ Use MobaXTerm’s file transfer dialog if you’re on Windows■ On Linux or Mac OS X:$ sftp [email protected]:private/15-213
sftp> help(read help for ‘cd’, ‘lcd’, ‘pwd, ‘lpwd’, ‘get’, ‘put’, etc.)
$ scp [email protected]:private/file.txt /local/folder
$ scp file.txt [email protected]:private/folderAlso, you can use FileZilla! Here’s a detailed guide: http://cs.cmu.edu/~213/recitations/using_filezilla.pdf
4
Carnegie Mellon
Continue On...
$ ls$ cd private$ mkdir 15-213$ cd 15-213$ cd lab-handout (Once obtained from GitHub!)
5
Carnegie Mellon
Git Setup (User Information)
$ git config --global user.name "<Your Name>"$ git config --global user.email <Your email>$ git config --global push.default simple
7
Carnegie Mellon
Sample Git Repo Creation
In a new folder (mkdir)$ git init$ echo “a sample file” > readme.txt
To save your progress:$ git add readme.txt$ git commit -m “my first commit”
8
Carnegie Mellon
Git Ignore
For those who want to use git add -all or git add .
Do not track .o files or executable files!!!
Create a file .gitignore in your git repository and add files that you do not want to track
gitignore rules: https://git-scm.com/docs/gitignore9
Carnegie Mellon
14
Now you should have access to a repo like this. Click lower link to get there from previous image.
GitHub Setup
Carnegie Mellon
Set up SSH KeysFirst check if you already have an ssh key: $ cat ~/.ssh/id_rsa.pub should print a stringIf not: $ ssh-keygen -t rsa -C "213GitHub" -b 4096Use the default file path (press Enter).Optionally type in a password. (press Enter for no password)
15
Carnegie Mellon
Unlocking Your Github RepoFrom your terminal, type:$ cat ~/.ssh/id_rsa.pubYour public key will be printed.Highlight it with the mouse and copy
19
Carnegie Mellon
Unlocking Your Github Repo
Give a title and paste *entire* SSH key.Should start with ‘ssh-rsa’ and end with ‘213Github’.
20
Paste Key in this box.
Carnegie Mellon
Accessing Your Github Repo
From your private 15213 directory, type the following:$ git clone [email protected]:<directory path>.gitImage below shows where to find <directory path>:
21
Carnegie Mellon
Initial Commit
Enter cloned directory and do following:$ echo “turtwig” > myteam.txt$ git add myteam.txt$ git commit -m “initial commit”
22
Carnegie Mellon
Push your git commit
$ git push -u origin master
You should now be able to see your updated repo in GitHub
$ git push is all that is required for future pushes in the same folder
23
Carnegie Mellon
Updating Repo (Essentials)
When you edit a file:$ git add <file-name>When you want to save a version locally$ git commit -m “version-name”When you want to backup your commits in the Git Repo:$ git push
24
Carnegie Mellon
Git Commands
25
add Stage new or changed files rebase Modify, combine, delete, ... previous commits
commit Save current staged files merge Combine commits from specified branch into current branch
push/pull Push/pull local index to/from the remote server
checkout Examine a different commit/branch/file
log Show history of git commits stash Temporarily save your current uncommitted changes
status Shows working directory status (added/modified/deleted files)
stash pop Restore previously stashed changes
show Show a file from a different commit or branch
diff Show changes between commits, files, unstaged changes, ...
branch Create a new branch (use a new branch for experimenting safely)
clone Clone a git repository (like a remote GitHub repo)
Carnegie Mellon
More Git
Getting help:■ git help <command>■ Piazza/Office hours
Git tutorials:■ https://www.atlassian.com/git/tutorials (focused
tutorials)■ https://try.github.io (basic interactive introduction)
26
Carnegie Mellon
Terminal Shortcuts
■ Be very very very careful with rm!!!■ There is no trash with rm. It is gone.
Meaning Example
~ Home directory cp foo.txt ~
. Working (current) directory
cp ~/foo.txt .
.. Parent directory cp ~/foo.txt ..
- Previous directory cd -
* Match as many characters as possible
cp ~/*.txtrm *.c
The command line operates on one directory at a time (the “working directory”).
You can use these shortcuts whenever a directory or file path is expected.
27
Carnegie Mellon
More Terminal Shortcuts
■ Pressing tab will autocomplete file/directory names.■ Use the up+down arrow keys to scroll through your
previous commands.■ Control+R lets you search your command history.■ Control+A jumps to the beginning of the line.■ Control+E jumps to the end of the line.■ Control+U clears everything to the left of the cursor.■ Control+C kills your current program.■ Control+D (on a blank line) exits the terminal.■ Control+L clears your screen.
28
Carnegie Mellon
ls <dir>
■ Lists files in the present working directory, or, if specified, dir.■ -l lists ownership and permissions.■ -a shows hidden files (“dotfiles”).
■ pwd tells you your present working directory.
29
Carnegie Mellon
cd <directory>
■ Try running cd - to return to the previous directory.■ Try running cd .. to return to the parent directory.■ Changes your present working directory.
30
Carnegie Mellon
mkdir <dirname>
■ Makes a directory dirname in your present working directory.
■ Directories and folders are the same thing!
31
Carnegie Mellon
mv <src> <dest>
■ cp works in exactly the same way, but copies instead■ for copying folders, use cp -r
■ dest can be into an existing folder (preserves name), or a file/folder of a different name
■ src can be either a file or a folder
32
Carnegie Mellon
tar <options> <filename>
■ For full list of options, see man tar■ tar stands for tape archive. Was used on tapes!■ x - extract, v - verbose, f - file input, p - keep perms
33
Carnegie Mellon
rm <file1> <file2> … <filen>
■ To remove an (empty) directory, use rmdir■ To remove a folder and its contents, use rm -rf
▪ Please be careful, don’t delete your project.▪ There is no “Trash” here. It’s gone.▪ Contact [email protected] to restore.▪ Latest restore is up to a day old!
■ Restore most recent version yourself if you use git!
34
Carnegie Mellon
pipes and redirects
■ A pipe redirects output from one program as input to another program.■ Ex1: cat filename | outputfile■ Ex2: cat filename | grep 15213■
■ Can redirect output to a file.■ Ex3: echo hello > file.txt■ Ex4: echo hello >> file.txt
35
Carnegie Mellon
What’s in a file? (using cat)
■ cat <file1> <file2> … <filen> lets you display the contents of a file in the terminal window.■ Use cat -n to add line numbers!
■ You can combine multiple files into one!■ cat <file1> … <filen> >> file.txt
■ Good for seeing what’s in small files.
36
Carnegie Mellon
man <thing>
■ What is that command? What is this C standard library function? What does this library do?
■ Try it!■ man grep■ man tar■ man strlen■ man 3 printf■ man stdio.h■ man man
37
Carnegie Mellon
Editors (a touchy subject)
■ vim is nice, made for very powerful text editing■ Try running vimtutor to get started learning
■ emacs is nice, made to be more versatile■ Emacs tutorial in emacs: “Ctrl-h t”
■ gedit has a GUI■ Requires X Forwarding: See Appendix
■ I strongly recommend editing on the terminal.■ Gist: Use an editor with auto-indent and line numbers
40
Carnegie Mellon
Configuring bash
The file ~/.bashrc is run every time you log in.Put the following code:PS1="[\u@\h:\w] \$ "alias ls='ls --color=auto'
to change your prompt to look like:[szz@makoshark:~/private/15213] $ lsattacklab bomblab lab-answers
41
Carnegie Mellon
Commands related to 15-213
■ gdb, the GNU Debugger, will be used for bomb lab.■ objdump displays the symbols in an executable.■ gcc is the GNU C Compiler.■ make is a configurable build system often used for
compiling programs.■ We will provide other tools in the handouts as well
42
Carnegie Mellon
Vimtutor Walkthrough■ Chapters 1-3■ Cheatsheet: http://bit.ly/2cl0lJ0
44
Carnegie Mellon
Resources■ Quick references: cs.cmu.edu/~213/resources.html■ CMU Computer Club
■ www.contrib.andrew.cmu.edu/~sbaugh/emacs.html
■ club.cc.cmu.edu/talks/fall15/power-vim.html
■ club.cc.cmu.edu/talks/fall15/power-git.html
■ Great Practical Ideas■ www.cs.cmu.edu/~15131/f15/topics/bash/
■ www.cs.cmu.edu/~15131/f15/topics/git/
■ Official manuals■ info bash■ info emacs■ :help in Vim
45
Carnegie Mellon
tmux
$ tmuxCtrl+b, then c: create a new tabCtrl+b, then n: move to next tabCtrl+b, then p: move to previous tabCtrl+b, then x: kill the current tabCtrl+b, then ?: helpCtrl+b, then ": split horizontalCtrl+b, then %: split verticalCtrl+b, then arrow keys: move between panes
46
Carnegie Mellon
Fancy Terminal Shortcuts
■ Bash automatically splits things up in brackets!■ Ex: cp foo{1,2}.txt = cp foo1.txt foo2.txt■ Ex: cp foo.txt{,.bak} = cp foo.txt foo.txt.bak■ For when typing the same filename gets annoying
■ Bash has for loops!■ Ex: Append “15-213” to every file ending in .c
for file in *.c; do echo “15-213” >> $file; done
■ Have fun, but don’t break things or lose track of time
47
Carnegie Mellon
What’s in a file? (using grep)
■ grep <pattern> <file> will output any lines of file that have pattern as a substring■ grep -v will output lines without pattern as
substring■ grep -n prints line numbers■ grep -R will search recursively
■ Try it: grep ‘phase’ bomb.c■ grep -n ‘printf’ src.c■ grep -R ‘unsigned’ .