Gitlab Guide

Throughout CSE 333, you will use Gitlab and git to implement version control on exercises and homework. This also enables collaboration on homework. This guide will help you get started with Gitlab and how to use git effectively in this course.

CSE 333 Gitlab Setup

The following instructions are for connecting your CSE Linux environment (attu or CSE Linux VM) to your Gitlab repo. The later Git Workflow section has tips and tricks for using git as you work on the Homework, including if you are collaborating with a partner.


Find Your 333 Repositories

  1. Navigate to Gitlab.
    • If you have a CSE NetID, use the green "CSE NetID" button to log in; otherwise, use the white "UW NetID" button.
  2. In your list of Projects, you should find TWO CSE 333 repositories ("repos" for short): cse333-26wi-students/cse333-26wi-ex-<netid> and cse333-26wi-students/cse333-26wi-hw-<netid>. Click either to enter the web browser interface for that repo.
    • The exercise repo will be created shortly after Lecture 1.
    • The homework repo will be created at the end of Week 1.
    • If you don't see your repos by then, please contact the instructor(s) ASAP to have them created.

Add an SSH Key

This will allow you to access your repo without having to authenticate every time (i.e., no more typing your password!) and will greatly improve your workflow.

  1. Check to see if your CSE Linux environment (attu or CSE Linux VM) account already has an SSH key by running:
    $ cat ~/.ssh/id_rsa.pub
    • If you see a long string starting with ssh-rsa or ssh-dsa, skip to Step 3.
  2. Generate a new SSH key by running:
    $ ssh-keygen -t rsa -C "<netid>@cs.washington.edu"
  3. Print your SSH key to the terminal by running:
    $ cat ~/.ssh/id_rsa.pub
  4. Highlight and copy the complete key (i.e., starting with ssh- and ending with your username and host).
  5. Add your SSH key to GitLab:
    • Navigate to your SSH Keys page by clicking on your avatar in the upper-right, then "Preferences", then "SSH Keys" in the left-side menu.
    • Paste your SSH key from Step 4 into the "Key" text box and give a "Title" to identify what machine the key is for.
    • Click the "Add key" button below "Expiration date".

Git Configuration

The git commmand looks for a file called .gitconfig located in your home directory that contains a variety of options that control git's behavior. Some commands (e.g., commit and push) will produce verbose warning messages if certain options are unset; these commands will properly configure git. If you've set up git before, this section can be safely skipped as these commands only need to be run once per user (though running them again won't cause any harm).

  1. $ git config --global user.name "<your name>"
  2. $ git config --global user.email "<your netid>@cs.washington.edu"
  3. $ git config --global push.default simple

Cloning Your Repos

The version of your repository that can be viewed through the Gitlab web interface is the remote repository. You can clone a remote repository to create a local copy. There is no limit to the number of local copies you can clone (on different or even the same computer), but each will be independent of each other without specific updating commands so we recommend working with a single clone.

  1. Find your repo's URL by navigating to your project's details page, clicking the blue "Code" button in the upper-right, and then clicking the copy icon under "Clone with SSH".
    • You will want to do this for both your exercise and homework repos.
  2. $ git clone <repo url>
    • This will copy the remote repo contents into a directory within your current location with the same name as your repo (e.g., cse333-26wi-ex-<netid>).
    • The repos may be empty at the beginning of the quarter.
  3. OPTIONALLY, you may rename the cloned directory for ease of navigation without breaking any functionality:
    $ mv cse333-26wi-ex-<netid> 333ex
    • We recommend cloning your exercise and homework repo in the same parent directory, e.g., ~/333ex and ~/333hw OR ~/cse333/ex and ~/cse333/hw, where ~ is your attu home directory.

Your First Commit

Once you've cloned a repo, you can make modifications to it. When it's in a state that you want to store/add to its version history, you need to commit your changes and then push the commits to the remote repo. Here, we show you a sequence of commands to add a blank README.md file to your remote repo; more general instructions can be found in the Updating Local and Gitlab Repositories section.

  1. First, navigate into the repo that you want to update, e.g.,
    $ cd cse333-26wi-ex-<netid>
  2. Create an empty file called README.md.
    $ touch README.md
  3. Print out the status of the repo; you should see 1 new file named README.md.
    $ git status
  4. Stage the new file for commit. In this example, it should output git status: README.md staged for commit.
    $ git add README.md
  5. Commit all staged files with the provided comment/message. In this example, it should output git status: Your branch is ahead by 1 commit.
    $ git commit -m "First Commit"
  6. Publish the changes/commit to the remote repo, which should then be viewable in the web interface (you may need to refresh your browser window).
    $ git push

Developing with Git Workflow

Once you have a made a local copy of your Gitlab repository in the CSE Linux environment (attu or CSE Linux VM), you can now make changes to files your repository locally!

The expected git workflow on this page should suffice for CSE 333, but you can find more details in the Git Community Book.


Updating Local and Gitlab Repositories

Update Local from Gitlab

Navigate to anywhere within your local repository and use the command:

$ git pull

Update Gitlab from Local

Make sure that you are in your local repository and that all updates to files have been saved before using the following commands:

  1. $ git pull  # make sure your local copy is up to date with Gitlab
  2. $ git add <files>  # stages files to commit to Gitlab
    • It is common to navigate to the top-level repository directory and then use . (period character) as <files>, which will stage all added/deleted/modified files in the local repo because . represents the current directory.
    • You should omit any added/deleted/modified files from your git add command that you don't want commited to the Gitlab repository.
  3. $ git commit -m "<message>"  # add a descriptive message about your update
  4. $ git push  # push your changes to Gitlab

Recovery Methods from Weird Git Situations

Keeping your workspace organized is crucial to keeping your workflow smooth, efficient, and as productive as it can be. By making sure to frequently update your local and Gitlab repositories, you reduce the chance of getting into weird situations. Nevertheless, it is common to end up in a "Git spaghetti ball" every so often, so here are some useful survival tips and commands.

Recovering from Local Messiness

The following git commands are ways to manage local changes to your repository before pushing to Gitlab:

  • Reset your files to the HEAD of the branch if you don't want your changes anymore:
    $ git reset --hard HEAD
  • Reset a single file instead of the entire branch:
    $ git checkout HEAD -- <path to file>
  • Revert changes that were already committed:
    $ git reset --soft HEAD~1
  • Stash/hide local changes and revert to a clean working directory:
    $ git stash
    • Revisit your stashed changes after you're done working on a fix:
      $ git stash pop
    • Discard the last stashed state:
      $ git stash drop
    • More info on git stash.

Reverting to an Old Commit on Gitlab

This is a way to remove a commit that you pushed to Gitlab but no longer like. These instructions take inspiration from Gitlab documentation.

  1. Log in to the Gitlab web interface and navigate to your repo for CSE 333. On the left menu, hover over "Repository" and then click on "Commits" to view your repo's commits in reverse chronological order.
  2. Go to the specific commit you want to revert (i.e., the earliest commit you want to remove) by clicking on its title:
  3. Click on options on the top-right side of the page and press revert:
  4. You won't need a merge request for changing your own work (unless you want to), so you can unselect the "Start a new merge request with these changes" checkbox and click Revert:
  5. Your repository has been updated! Don't forget to update your changes in your local repository by running git pull.

Managing Git Tags

Your homework and later exercises will be submitted for you based on commits in your Gitlab repositories that are marked with special tags (i.e., you do NOT submit anything yourself to Gradescope). This section is about managing those tags.

Adding a Tag (command line)

Make sure to add a tag after you have completed the assignment. What the tag does is mark a specific commit (in this case, your latest one) in your repo with a name that makes it easy for someone to find/reference.

		  $ git tag hw<#>-submit  # or ex<#>-submit; this is the required tag naming scheme for CSE 333
          $ git push --tags       # push tags from local to remote
        

Adding a Tag (web interface)

  1. Navigate to the repo in Gitlab.
  2. Hover over the "</> Code" item in the left sidebar and select "Commits". Yes, there is a "Tags" option, but that is more difficult to describe/use, so go to "Commits" instead.
  3. Click on the commit message for the commit that you want to tag (likely the top/latest one).
  4. Click the "Options" drop menu in the upper-right and select "Tag".
  5. Enter the tag name hw<#>-submit or ex<#>-submit and click "Create tag".
  6. Note that the above only created the tag in the remote repo. Run the following command to see the tag locally:
    $ git fetch --tags

Removing a Tag (command line)

If you add or commit changes to an assignment after adding a tag, you will need to remove the old tag with the commands below and then add a new tag afterward because tag names must be unique:

          $ git tag -d hw<#>-submit
          $ git push origin --delete hw<#>-submit
        

Removing a Tag (web interface)

  1. Navigate to the repo in Gitlab.
  2. Hover over the "</> Code" item in the left sidebar and select "Tags".
  3. Find the tag that you want to delete (likely the top/latest one) and click the trash can icon on the right.
  4. Click the "Yes, delete tag" button to confirm deletion.
  5. Note that your local tag is still hanging around! To delete it, you need to run:
    $ git fetch origin --prune --prune-tags