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â .