Introduction to Computer Science
Tutorial 2: version control and git
Lin Wang September 7, 2020 Attention
Please log into Zoom via Canvas so that your real name (not your nickname) is always shown
Please use the Chat function sparingly ■ Respect each other, use the Chat to help each other ■ Any form of abuse will not be tolerated ■ Do not post any irrelevant content on the Chat (We monitor the use of Chat and we will look into violations)
2 Recap: Linux and shell
Navigate the filesystem: Redirection pwd: print working directory cat: concatenate files cd: change directory sort: sort records ls: list directory contents less/more: navigating through file uniq: remove duplicates from sorted list Manipulate files/directories grep: match on text patterns cp: copy files and directories mv: move/rename files/directories Misc rm: remove files and directories echo: print text mkdir: create directories *: wildcard referring to all touch: create files ?: wildcard referring to a single character
Tip: learning by doing!
3 Week 1 quizzes
Question 1: Which of the following commands is used to print the current working directory?
❌ cd ❌ ls ✅ pwd ❌ man
Question 2: If the current working directory is workspace/dir1/dir2, which of the following commands will navigate to the workspace/ directory?
❌ cd .. ❌ cd workspace ❌ cd ../../.. ✅ cd ../..
4 Week 1 quizzes
Question 3: Which of the following statements is true about the two commands? (a) cp dir1// dir2 (b) cp -r dir1// dir2
✅ After running command (a), the directory dir2 contains the files from the directory dir1 but not the directories. However, after running command (b), the directory dir2 does contain all files and directories from the directory dir1. ❌ Command (a) is not valid since copying contents from directories requires using the '-r' option, to copy the contents of the directory recursively. ❌ After running command (a) the directory dir2 is a subdirectory of the directory dir1, whereas after running command (b) both directories are at the same directory level. ❌ Both commands copy the contents of directory dir1 to directory dir2. If directory dir2 does not exist, it is created and then the contents of dir1 are copied to the newly created directory dir2.
5 Week 1 quizzes
Question 4: Which of the following statements is true about the three commands below? (a) mv dir1 dir2 (b) mv dir1// dir2 (c) mv -r dir1// dir2
✅ In the case where the directory dir2 does not exist, command (a) is executed successfully but command (b) fails. ❌ When running command (a), if the directory dir2 exists then it is overwritten with the contents of the directory dir1. ❌ Before running command (a) the directories dir1 and dir2 are at the same directory level, and after execution, dir2 is a subdirectory of dir1. ❌ Before running command (b) the directories dir1 and dir2 are at the same directory level, and after execution, dir1 is a subdirectory of dir2.
6 Week 1 quizzes
Question 4: Which of the following statements is true about the three commands below? (a) mv dir1 dir2 (b) mv dir1// dir2 (c) mv -r dir1// dir2
❌ The first command renames the directory dir1 to dir2. If dir2 does not exist, it is first created and then the contents of dir1 are moved to the newly created directory dir2. ❌ After running command (b), the directory dir2 contains the files from the directory dir1 but not the directories. However, after running command (c), the directory dir2 does contain all files and directories from the directory dir1.
7 Week 1 quizzes
Question 5: What does the command below do? cp foo[123] dir1
❌ The command copies all files starting with either foo1, foo2, or foo3 to a directory named dir1. ✅ The command copies a file named foo[123] to a directory named dir1. ❌ The command copies one of the files foo1, foo2, or foo3 to a directory named dir1. ✅ The command copies several files named foo1, foo2, foo3 to a directory named dir1.
See more about shell globbing: https://linux-training.be/funhtml/ch17.html
8 Week 1 quizzes
Question 6: Which of the following commands will fail to execute?
❌ cp file1 ~ ✅ mv -r dir1// dir2 ❌ cp *.txt dir1 ✅ cp dir1 dir2
9 Week 1 quizzes
Question 7: Which of the following statements are true about the four commands below? (a) cp dir1//.cpp dir2 (b) grep -n cin file1.cpp (c) cat file1 file2 >> file3 (d) echo [0-9]*
✅ Command (b) lists the lines that contain the string "cin", along with the line number they were found on. ✅ Command (d) lists all the files and directories whose names start with digits. ✅ Command (a) copies all files ending with ".cpp" in the directory dir1 to the directory dir2. ❌ Command (c) redirects the contents of file1 and file2 into the file file3. If file3 does not exist, it will be created; if file3 already exists, it will be overwritten.
10 Version control
Source: SXSW Hackathon
Imagine you work in a team where all of you contribute to a complex code base. This is very likely to happen in your later coursework.
11 Talking about complex code bases... Line of code in Linux kernel Linux in code of Line
Time
https://commons.wikimedia.org/wiki/File:Lines_of_Code_Linux_Kernel.svg 12 Version control How would you organize the shared code base and work separately without the hassle of code conflicts?
You need a version control system (VCS)! It helps with the following 5 Ws: ■ What changes were made? ■ Where the changes were applied? ■ Who made the changes? ■ When the changes were made? VCS ■ Why the changes were needed?
Source: SXSW Hackathon
13 Centralized version control
A single server holds the code base. Clients access the server by means of check-in/check-outs Examples: ■ Concurrent Versions System (CVS) ■ Subversion (SVN) ■ Visual Source Safe
Advantages: easy to maintain a single server
Disadvantages: single point of failure
14 Distributed version control
Each client holds a complete copy of the code base. Code is shared between clients by push/ pulls. Examples: ■ Git ■ BitKeeper ■ GNU arch
Advantages: cheaper operations, no single point of failure
Disadvantages: a bit more complicated!
Do not worry, we are going to learn it now.
15 ■ 2002 - April 2005: Linux kernel was hosted on BitKeeper ■ April 2005: The copyright holder of BitKeeper withdrew free use of the product due to reverse engineering claims ■ April 3, 2005: Linus Torvalds (creator of the Linux kernel) began developing Git ■ April 5, 2005: Linus Torvalds announced the Git project and it became self-hosting on the second day ■ April 18, 2005: First merge of multiple branches took place ■ June 16, 2005: Linux kernel 2.6.12 was managed by Git ■ December 21, 2005: version 1.0 was released ■ By 2018: around 87% developers were using Git TED: The mind behind Linux
16 Configure your Git client
Install Git ■ Linux/WLS: sudo apt install git ■ macOS: brew install git
Configure your information ■ git config --global user.name "John Doe" ■ git config --global user.email [email protected]
17 Repository Create a new repository
Client Server
Option 2: git clone https://path/to/repository
Repository Repository
Option 1: git init
git clone https://github.com/imlinwang/introcs-example.git git clone [email protected]:imlinwang/introcs-example.git (readonly)
18 Repository The three stages in Git
workspace staging local remote
add/remove commit reset push reset
diff clone/pull
19 Commit Add and commit a file to a Git repository
# create a new file touch newfile.txt workspace staging local # add some content echo "Some text" > newfile.txt # add to the staging env git add newfile.txt add/remove # check status commit git status # commit the changes git commit -m "First commit" # check status git status
20 Branch Allows you to move back and forth between status of a project
You can create a new "feature" branch to work on a new feature without affecting the main branch of the project. At the end, you can merge the feature branch into the main branch.
21 Branch Create a new branch and merge it with the master branch
# create a new branch and check out to it git checkout -b feature # create a new branch git brach feature # check all branches and current branch git branch # check out to the branch master git checkout master # merge the feature branch with the master git merge feature # delete a branch git branch -d feature
22 GitHub A platform for hosting project repositories
workspace staging local remote
add/remove commit reset push
diff clone/pull
23 GitHub Local-remote interactions
# clone a remote repository git clone [email protected]/path/to/repo remote # pull remote changes git pull # add a remote named origin git remote add origin path/to/remote # verify remote push git remote -v # push local commits to remote branch master git push origin master clone/pull
24 GitHub Set up a new GitHub repository and make changes
25 GitHub Issue pull requests
26 GitHub Confirm pull requests
27 GitHub Merge pull requests
28 References
Check the following links if you want to learn more about Git ■ Git Pro: https://git-scm.com/book/en/v2 ■ Git Cheat-Sheet: https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf ■ Git Magic: http://www-cs-students.stanford.edu/~blynn/gitmagic/index.html ■ Oh Shit, Git!?!: https://ohshitgit.com/ ■ Git for Computer Scientists: https://eagain.net/articles/git-for-computer- scientists/ ■ Learn Git Branching: https://learngitbranching.js.org/ ■ MIT Git Tutorial: https://missing.csail.mit.edu/2020/version-control/
29