-
JINLANG WANG authoredJINLANG WANG authored
GitLab
In this lab, you'll practice using git. You'll create your own repo, and push changes there.
Background
A repo contains a bunch of commits, and usually a few branches to label different commits. In order to support collaboration and offline work, it is common to have multiple copies of the same repo. For example, say there is a team of 3 people working on an open-source project. There will probably be six copies of the repo: one on each person's laptop or virtual machine and one on each person's GitLab account (with one of the GitLab ones being the primary home for the code).
Various git commands and tools can be used to syncronize the copies.
For example, running git push origin test
uploads the test
branch,
and all the associated commits, to origin
:

origin
is an example of a remote, a shorthand name to use instead
of a full URL for a repo somewhere else (like GitLab). When you
clone
a repo from GitLab, you automatically get a remote called
origin
, but you can setup more yourself.
clone
is a one-time thing to make a new copy of a GitLab repo on
your computer and download all the commits/branches/etc. If new
changes are made on the GitLab repo, you can instead run git pull
to
download these without creating a whole new copy of the repo.
In addition to these three general git commands (clone, pull, push), GitLab has two key tools for syncing repos:
- fork: copy somebody else's GitLab repo to a new repo (called a fork) on your account
- pull request: ask somebody to bring some new code you uploaded to your fork back into the main repo
Consider a concrete example of how to use all these commands. Say you find a bug in pandas
, fix it, and want to share the fix back. You might do the following:
- Find the pandas repo on GitLab
- Clone the pandas repo to a copy on your computer
- Fork the pandas repo to a copy in your own GitLab account
- Make the change to the copy on your computer
- Add a remote so that you can push from your computer to your GitLab fork
- Do a push to upload your changes from your computer to your fork
- Do a pull request from your fork to the main pandas repo
- Somebody in charge of main repo will consider your changes, and probably (a) click a button on GitLab to incorporate your changes, or (b) give you feedback to make the code better first, in which case you go back to step 4
Step 2: SSH Keys
These steps are similar to the previous lab. There, we created SSH keys on your laptop, allowing you to connect laptop=>VM. Now, we're creating SSH keys on your VM, allowing you to connect VM=>GitLab.
Connect via SSH to your VM.
Run ssh-keygen
on your VM and repeatedly hitting ENTER
to accept
all the defaults (including an empty password).
Run the following and copy the output:
cat ~/.ssh/id_rsa.pub
Select "SSH keys".
Name the key "cs320-vm" (or whatever you like, really) and paste the
contents of id_rsa.pub
to the "Key" box.
Click "Add SSH Key" to finish adding it.
Step 3: Create a Repo
Create a public repo called "cs320-lab2". Do NOT initialize the repo by clicking on any checkbox at this step. Do NOT select "Add a README file".
This should create a repo. Go to that repo. Click the Clone button, and then click http. Copy the commands in it. We want to run those in a new directory on your computer. So run this in the terminal:
mkdir cs320-lab2
cd cs320-lab2
Then paste and run git clone
+ what you copied from GitLab. You shouldn't be
asked for a password (if so, double check you did the parts of step
2+3 related to SSH correctly). If you see "Are you sure you want to
continue connecting?", type "yes" and ENTER.
If prompted, you can configure your username/email:
git config --global user.name "your_gitLab_username"
git config --global user.email "your_email"
Refresh the GitLab page for your repo. You should now see the first commit.
Make another change to README.md on your computer (for example, say "hello world"), push those changes to GitLab, then refresh the page. Like this (one step at a time):
nano README.md # make some changes, then save
git status
git add README.md
git commit -m 'say hello'
git push
Keep in mind nano
is an in-terminal text editor so you can only use
keyboard shortcuts (not the mouse). Do control-O to write the file.
"^" means CONTROL, and the bottom of the screen should provide hints.