213/513/613 Linux/Git Bootcamp
Sophie, Urvi
1. SSH, bash, and navigating Linux
Copyright By PowCoder代写 加微信 powcoder
2. Using VIM
3. Setting up VS Code
1. On macOS/Linux:
(don¡¯t type in the ¡°$¡±! This just means you¡¯re typing what follows into terminal)
1. Type your password when prompted.
2. If you see a warning about SSH host keys, click or enter ¡°yes.¡±
Windows computers???
¡ñ Use MobaXTerm for file transfer and ssh client!
¡ñ Instructions can be found here:
http://www.cs.cmu.edu/~213/activities/linux-bootcamp/windows-setup.pdf
What are shark machines?
¡ñ Shark machines, linux.andrew.cmu.edu and unix.andrew.cmu.edu are all machines that access the same System (AFS).
¡ñ Shark machines are explicitly set up for 213: they¡¯re standardized for benchmark tests and have correct versions of gcc, gdb and other tools.
Use the shark machines… otherwise your compiled code won¡¯t behave as expected!!!
Error: ssh: Could not resolve hostname
¡ñ log into a specific shark machine
¡ñ The list of shark machines is listed under the ¡°Lab Machines¡± section of the 213
Navigating the shark machines
¡ñ $ cd PATH
¡ñ $ mkdir NAME
¡ñ $ touch NAME
¡ñ $ rm NAME
¡ñ $ cat NAME
¡ñ $ mv FILE DEST
¡ñ $ cp FILE DEST
¡ñ $ tar OPT NAME
list all files in folder. ¡°-a¡± flag lists hidden files
print current file path
enter folder PATH. ¡°.¡± is current folder, ¡°..¡± is
make a folder called NAME
make a file called NAME
remove file called NAME
output file NAME¡¯s content to commandline
move FILE to DEST folder
move FILE to DEST folder
compress to tar file with name NAME or open tar
file based on OPT flags
¡ñ $ scp FILE move FILE from local
machine to DEST folder on shark machine
Editing files
Option 1: VIM
¡ñ Can be run on pretty much any terminal.
¡ñ Highly customizable.
¡ñ According to legend, if you learn all the keyboard shortcuts, the rate at which your
fingers travel approach lightspeed, to the point of being a potential hazard to those in your general vicinity.
Option 1: VIM
1. Let¡¯s start by SSHing into the shark machines!
1. From here, let¡¯s make VIM *spicy* by running the following:
$ vim ~/.vimrc
1. Three big modes:
a. Normal mode: press the ¡°esc¡± key.
b. — INSERT — mode: press the ¡°i¡± key in normal mode.
c. — VISUAL — mode: press the ¡°v¡± key in normal mode.
Option 1: VIM
4. Press ¡°i¡± and make sure you see ¡°– INSERT –¡± at the bottom. Then type that into the text buffer ¡ú ¡ú ¡ú
5. When done, press ¡°esc¡± and then type in ¡°:w¡± to save.
6. Type in ¡°:q¡± to quit VIM. (This can be combined into ¡°:wq¡± to
save and quit in one command :-0)
colorscheme desert
set mouse=a
set number
set cursorline
set colorcolumn=81
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
set smartindent
Option 1: VIM
¡ñ Normal mode: ¡°esc¡± key ¡ð — INSERT — mode: ¡°i¡± key
¡ö Type and stuff :-0
¡ð — VISUAL — mode: ¡°v¡± key
¡ö Use arrow keys to highlight a selection
¡ö ¡°Copy and paste¡±:
¡ñ Highlight text, press ¡°y¡± to yank (copy) and ¡°p¡± to paste
¡ñ Similarly, pressing ¡°d¡± twice will delete the selection, which also makes it available to
paste with ¡°p¡±
¡ð Save: ¡°:w¡±
¡ð Quit: ¡°:q¡±
¡ñ With the given .vimrc, you can also scroll and click with the mouse
¡ñ Try $ vimtutor for more tips and tricks for VIM that we might not have covered!
Option 2: VS Code + SFTP
About VS Code:
¡ñ Text editor with lots of traditional functionality.
¡ð Tabs, easy window split, built-in terminal, tree view, etc.
¡ñ Cool plugins to make code prettier + life easier.
¡ñ People won¡¯t make fun of you for using the mouse.
¡ð (Except for VIM purists)
¡ð (Don¡¯t let the haters get to you)
Option 2: VS Code + SFTP
About SFTP:
¡ñ Secure File Transfer Protocol
¡ñ Can be used to read from and write to files in your AFS directory by synchronizing
changes to files saved directly to your machine
¡ñ Note on academic integrity: since the files exist on both your machine and AFS, you
are responsible to make sure that no one gets access to either location!
Option 2: VS Code + SFTP
On your local machine:
¡ñ Download VS Code here: https://code.visualstudio.com/download
¡ñ You can check out some of the other extensions (linting for C for style???) on your own time, but download liximomo¡¯s SFTP plugin because that¡¯s how we¡¯re gonna be working with the Shark machines.
Option 2: VS Code + SFTP
On your local machine:
¡ñ Create a local 213 folder.
¡ñ Inside there, create a folder called ¡°linux-bootcamp.¡± Open it in VSCode.
¡ñ Ctrl + Shift + P (Windows) or Cmd + Shift + P (Mac) to open up Command Palette:
¡ñ Type in ¡°SFTP: Config¡±
¡ð This should open ¡°sftp.json¡±
¡ñ Type in the info in the top-right.
¡ð Notes: ¡°uploadOnSave¡± will automatically save any local changes whenever you save the file.
¡°downloadOnOpen¡± will automatically update your local version with the AFS version when you open the file.
¡ñ Visit https://github.com/liximomo/vscode-sftp/wiki/config for extra config options
Option 2: VS Code + SFTP
On your local machine:
¡ñ Create a file called ¡°example.txt¡± and type whatever you want into it.
¡ñ When you save, this should prompt a popup to type in your SSH password.
Option 2: VS Code + SFTP
On your favorite Shark machine:
¡ñ SSH into your favorite Shark machine.
¡ñ $ cd into your example directory and list the files. You should see ¡°example.txt¡±
Option 2: VS Code + SFTP
Reminders:
¡ñ SFTP means you¡¯re downloading code from AFS onto your local machine, so take extra precaution to make sure that code is secure and no one steals it!
¡ñ Any time you run $ make, please do so on the Shark machines!!
What is git?
¡ñ Version control system ¡ð Better than:
¡ö copy pasting code
¡ö emailing the code to yourself
¡ö taking a picture of your code and texting it to yourself
¡ö zipping the code and messaging it to yourself on facebook
¡ñ git ¡ GitHub
¡ñ using git this semester will (with high probability) be mandatory!!!
~*style*~ point deductions if you don¡¯t use it
Important commands
¡ñ $ git init
¡ñ $ git clone
¡ñ $ git status
¡ñ $ git log
make a new repository
initialize a repository locally from a remote server
MOST IMPORTANT COMMAND, tells you information about what is going on show commit history. Can use –decorate –graph –all to make it pretty.
stages files to be committed. Flags: –a (all files), -u (only previously added files) stages files to be removed.
¡ñ $ git add
¡ñ $ git rm
¡ñ $ git reset HEAD + file unstages ¡°file¡± from the commit ¡ñ
¡ñ $ git commit -m
¡ñ $ git push
¡ñ $ git pull
commit the changes in the staged files. Write descriptive, meaningful messages! push changes to a remote server.
pull changes from a server
¡ñ $ git reset –hard + hash Used to reset to an old commit (with a commit hash)
¡ñ $ git checkout + filename Used to reset any changes made to a file to previous commit
Configuring git
$ git config –global user.name “
$ git config –global user.email ¡°
$ git config –global push.default simple
(Make sure the email is your D, and make sure to add that email to your GitHub account!)
.gitignore files
¡ñ Make one in each of your projects
¡ð Can use touch, emacs, vim, whatever you want
¡ñ *.o will ignore all files ending with ¡°.o¡± (object files)
¡ñ Useful because when you add a lot of new files with:
$ git add -a
you want git to ignore certain files
¡ñ We will now walk through a git tutorial.
¡ñ Visit the following: https://github.com/Guptacos/git-bootcamp
Cloning the repository
1. Go to to link in previous slide and click ¡°fork¡± in the top right corner to copy the repository to your Github account.
2. Make sure you are in your account, and click the green ¡°clone or download¡± on the right.
3. Copy the link.
4. Open up a terminal window (or xterm for windows users) and SSH into a shark machine
b. navigate to a folder where you want to do this example.
5. $ git clone + the link you copied
a. This will initialize the git repository on your computer, with GitHub as the remote server.
6. $ cd to enter the working repository.
Committing, pushing, & pulling
1. $ ls -a we have 2 files and 2 folders here
2. $ git status branch is up to date with the server, nothing to commit
3. $ git log –graph –decorate –all
Shows a pretty graph of the commit history.
4. $ cd cprogramminglab-handout
5. $ vim queue.c make some changes to queue.c (can use nano, emacs, etc)
6. $ git status now shows that we have unstaged files
7. $ git add queue.c stages the file to be committed
8. $ git status now shows that we staged example.txt
9. $ git reset HEAD queue.c unstages the file (to show you how to do that)
10. $ git status note that example.txt is unstaged again
11. $ git add queue.c stage the file again
12. $ git commit -m ¡°insert a relevant commit message here¡±
13. $ git status shows you are 1 commit ahead of ¡°origin¡± = remote server
14. $ git push this updates the remote server
15. $ git log –graph –decorate –all now we see the new commit on top of all the old ones
Appendix: advanced git
¡ñ $ git branch make a new branch
¡ñ $ git checkout switch to a different branch. Use -b to make a new branch
¡ñ $ git merge name merge ¡°name¡± branch into your current branch
¡ñ The beginning of this example covers basic git, and the later slides cover more advanced topics (branching)
¡ñ Visit the following: https://github.com/eyluo/linux-bootcamp
¡ñ Follow the instructions from example 1 to fork and clone the repository. Then
$ cd to enter the repository and follow along with the next few slides.
Committing, pushing, & pulling
1. $ ls -a we have 5 files and a folder here
2. $ git status branch is up to date with the server, nothing to commit
3. $ git log –graph –decorate –all
i. Shows a pretty graph of the commit history.
4. $ vim example.txt make some changes to example.txt (can use nano, emacs, etc)
5. $ git status now shows that we have unstaged files
6. $ git add example.txt stages the file to be committed
7. $ git status now shows that we staged example.txt
8. $ git reset HEAD example.txt unstages the file (to show you how to do that)
9. $ git status note that example.txt is unstaged again
10. $ git add example.txt stage the file again
11. $ git commit -m ¡°insert a relevant commit message here¡±
12. $ git status shows you are 1 commit ahead of ¡°origin¡± = remote server
13. $ git push this updates the remote server
14. $ git log –graph –decorate –all now we see the new commit on top of all the old ones
1. $ git master
log –graph –decorate –allnote the other branch ¡°realistic ending¡± that branches away from
checkout realistic_ending
switch to the other branch
shows all of our branches
note that there are now different files shown
we can see the story is different than in the master branch-finish it!
switch back to the master branch
will attempt to merge the two branches, but there¡¯s a conflict shows that the conflict is in example.txt
fix the story
6. Add and commit the file, push to the server.
7. $ git checkout master
example.txt
8. $ git merge realistic_ending a. $ git status
b. $ vim example.txt
c. $ git add example.txt
d. $ git commit -m ¡°appropriate message for a merge¡±now the merge is complete
9. $ git log — decorate –graph –allshows that now you still have 2 branches, but they¡¯ve been merged and point to the same files
Resetting & branching
1. $ git log –decorate –graph –all copy the commit hash of a past commit (first 6ish characters usually fine)
2. $ git branch newbranchname make a new branch
3. $ git checkout newbranchname switch to the new branch
4. $ git reset –hard + hash from old commit
5. $ git log –decorate –graph –all note that now HEAD is at the old commit, master
is still at the merge commit from last slide
6. $ ls the files are different now
7. $ vim example.txt the story is different too. Add a line or two to it
8. Add and commit
9. $ git log –decorate –graph –all now we can see how it has separated from the
rest of the tree
a. This is how you would test out new feature. If you decide you like it, you can later merge it into the master branch. If not, you can just leave it and switch back to master.
Adding your new branch to the remote server
1. $ git status note that it says nothing about the origin remote server
2. $ git push doesn¡¯t work, there is no ¡°upstream branch¡± (nothing on
the server)
3. $ git push –set-upstream origin newbranchname
a. This creates a new branch on the origin server, and sets it as the ¡°upstream¡± of your current branch.
In the future when you push, you can just do git push and it will work.
4. $ git log –decorate –graph –all
Note that now there is an origin/newbranchname branch
5. $ git status now branch is up to date with origin/newbranchname
6. $ git checkout master
7. $ git status we¡¯re far ahead of the remote server
8. $ git push
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com