Working with GIT
Knowing how to use a source control system will be an invaluable tool for you going forward, perhaps personally or even potentially for changes in laws, but especially in a team setting. At its heart, version control is just a way to manage the many changes that occur to your files over time, but that simple idea changes everything! It allows you to revisit previous versions of your code, work with different versions at the same time, and work in teams and track who made which changes. At its best, version control is like a journal that you use to track major, and minor, events in your project. At its most practical, it is like a backup system that prevents you from losing significant work if something happens to your machine. At its worst, it is simply a submit system where you only track your work when told to.
Version control systems have been around for about fifty years and GIT is currently the cool tool to use (partly because it was created by Linus Torvalds, the creator of Linux, and partly because of the popularity of GitHub, the largest public repository of code). Using source control well is not difficult, but it does take some practice and a little bit of command-line savvy (we do not suggest using visual GIT tools, even the one built into IntelliJ, until you are confident in your version control skills).
Here is a video showing following these instructions as well.
Install and Configure GIT
- Download and install GIT
on Mac or Linux, you can use the built in Terminal program to interact with GIT
on Windows, there are some settings you should set to work well with other platforms and it comes with a program called Git Shell that you can use to enter the commands below (you can also install a full version of Ubuntu Linux!) - Within Terminal/Bash, configure GIT for your computer (note, these commands need to be done once for each computer you program on)
git config --global user.name "YOUR NAME" git config --global user.email "NETID@duke.edu"
Login to Gitlab
- Within a Browser, go to the website: https://coursework.cs.duke.edu/
- Press the
Shibboleth
button to log in using your Duke NetID - After you have successfully been logged in through Duke's standard log in page, there may be a warning at the top of the page saying you need to set up an SSH key.
If so, go toUser Settings → SSH Keys
to set it up because you have never used the CompScicoursework
Gitlab site. - Clicking on that link should take you to a page prompting you to paste the public part of SSH key into the given text area
- Next, we will get that public key
Generate an SSH Key
- Within a Browser, go to the website: https://coursework.cs.duke.edu/help/ssh/README
- If you have not already generated an SSH key for another class (such as CompSci 201):
- Within Terminal/Shell, do Step 1 in the second section "Generating a new SSH key pair" and accept the defaults (steps 2 and 3)
ssh-keygen -t rsa -C "NETID@duke.edu" -b 4096
- Copy public part of the key using the command given in step 4 corresponding to your OS
- Within Terminal/Shell, do Step 1 in the second section "Generating a new SSH key pair" and accept the defaults (steps 2 and 3)
- If you have not already connected your current computer to the CompSci
coursework
Gitlab site: - Within Terminal/Shell, copy the public key using the command given in Step 4 in the second section "Generating a new SSH key pair"
- Within a Browser, paste the copied public key into the Gitlab text area (
User Settings → SSH Keys → Key Text Area
)
giving it the same title:id_rsa
, and pressAdd key
Getting Your Own Copy of a Gitlab Project's Starter Code
- Within a Browser, visit the project for this lab, https://coursework.cs.duke.edu/CompSci290_2021Spring/module01_exercises
- Press the
Fork
button in the upper right corner of the page to create a version of the project in your own account with the same name (e.g.,NETID/module01_exercises
)
Note, you will use fork to create your own repository of exercise projects so you can experiment on your own, but a repository will be provided for you for your Portfolio projects (so no fork is needed) - On the project page that appears, press the blue
Clone
button and copy the SSH URL (e.g.,git@coursework.cs.duke.edu:NETID/module01_exercises.git
)
Cloning a Copy of the Repository onto Your Local Machine
- Within Terminal, change to the folder in which you intend put your work for this course:
cd PATH_TO_WORKSPACE_FOLDER
- Download a local version of the project that is linked to your online repository
git clone git@coursework.cs.duke.edu:NETID/module01_exercises.git
- That command will create a folder named
module01_exercises
within your workspace folder that contains configuration information for GIT (e.g., a folder named.git
— note that it starts with a period)
ls -a module01_exercises
Push Changes to your Project back up to GitHub
- Within VS Code, open the newly created folder using the button on the Welcome page or using
File
to begin working on the code→
Open - Open the file
README.md
by double clicking on it - Add your name and save the file (or any other changes you want to make)
- Within Terminal, add your changed file to those staged for your next commit
git add README.md
- When you have added all the files to be grouped together into a single commit to the repository, note that with a message describing the changes contained in this commit
git commit -m "Included my name in the README"
- Within Terminal, push all your commits to the remote repository, e.g., Gitlab, so that others (UTAs, possible team mates, those following your web page, etc.) can see your changes
git push -u origin master
- Within a browser, refresh the page to see your changed README file
Publish your Project to the Web
To allow anyone to view your web page on the World Wide Web, WWW, it must be saved in a public location. In this course, we will use Gitlab Pages to host the web pages we develop.
- Within a browser, scroll down to the
Settings
option on the bottom left side of your repository page and selectPages
from the menu - You should see that a URL is provided to the publicly hosted version of your repository's web site automatically (it may take a few minutes before your site is first available):
https://NETID.github.io/module01_exercises/
Summary
The remaining exercises will remind you to repeat the typical steps to working with GIT so it becomes a regular habit:
git add
to select changed files for this changegit commit -m
to describe your changesgit push
changes back to Gitlab from your machine
Those three commands should be sufficient for most of this course, but you are encouraged to experiment with its range of commands so you are more comfortable with them: