Gitlab Guide

CSE 374 Gitlab Setup

These instructions are for connecting your CSE Linux environment (cancun) to your GitLab repo. The following section has more tips and tricks for using git as you work on your hw.

Go to gitlab

  1. Navigate to https://gitlab.cs.washington.edu
    • Most CSE 374 students should use the "UW NetID" button, but if you have a CSENetID, use the "CSE NetID" button instead.

Add Your SSH Key

Allows you to access your repo without having to authenticate (i.e., type your password) every time. Constant password authentication can sometimes make gitlab almost unusable.

  1. Check to see if your CSE Linux environment account already has an SSH key by running the following from cancun:
    cat ~/.ssh/id_rsa.pub
    • If you see a long string starting with ssh-rsa or ssh-dsa, skip to Step 3. This is unlikely to be the case for most CSE 374 students.

  2. Generate a new SSH key by running:
    ssh-keygen -t rsa -C "<netid>@uw.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 idenfity what machine the key is for.
    • Click the "Add key" button below "Title".

Set Default Options

For the most part, git has sensible default options and things work as expected. However, if you've never used git before, it will ask / remind you to set some options every time you try to commit or push something. Before starting to work with gitlab regularly, we suggest you enter these commands in a terminal window.

     git config --global user.name "Your Name Here"
     git config --global user.email netid@uw.edu
     git config --global pull.rebase false
Of course you should substitute your own name and email in these commands. This only needs to be done once and the settings will be retained and used as defaults in the future.

Your First Commit

Any commands shown below should be run from your CSE Linux environment (cancun)

  1. Find your repo's URL by navigating to your project's details page, clicking the blue "Clone" button on the right, and then clicking the copy icon under "Clone with SSH".
  2. git clone <repo url>
  3. touch README.md
    • This creates an empty file called README.md.
  4. git status
    • This prints out the status of the repo – you should see one new file named README.md.
  5. git add README.md
    • Stages a new file/updated file for commit. This time, it should output git status: README.md staged for commit
  6. git commit -m "First Commit"
    • Commits all staged files with the provided comment/message. This time, it should output git status: Your branch is ahead by 1 commit
  7. git push
    • Publishes the changes to the central repo, which should now be viewable in the web interface. You may need to refresh your browser first.

Developing with Git Workflow

Once you have a made a local copy of your Gitlab repository in your CSE Linux environment, you can now have access to and make changes to files your repository locally on cancun! However, note that the repository on Gitlab and the copy in your CSE environment do not automatically synchronize with each other, so you have an additional responsibility of keeping both of them up to date (so you don't have out-of-date copies in either place or lose work if something goes wrong with your local copy of the repo).

We recommend you use the git workflow described here. (If you are interested in more details, the Git Community Book is a great resource as well).

Updating Local and Gitlab Repositories

To update your local copy of the repository with any new information in the Gitlab copy of the repository, type the following command (in your local repository):

[cancun]$ git pull
You may run into a merge conflict when working with other collaborators, or if the course staff has added new files to the repository. Refer to GitHub Docs for resolving merge conflicts. You can reduce the possibility for merge conflicts by consistently making sure your repository is up to date when developing. You should always do a git pull before adding or committing any new files or changes to your local copy of the repository.

Commit and add changes from your local copy to the Gitlab repository:

  1. Make sure that all files are saved in your local copy first and you are in your local copy of the repository!
  2. [cancun]$ git pull  # make sure your local copy is up to date with Gitlab
  3. [cancun]$ git add <files> # stages files to commit to Gitlab
    • You may want to use . (period character) in <files> from the top-level git repository directory to stage all files to be added to Gitlab repository. (Note: . represents current directory, so at top-level of the repository, you are adding and staging all changes in local copy of the repository!)
    • You should not include files in git add that you do not want to commit to the Gitlab repository.
  4. [cancun]$ git commit -m “<message here>” # add a descriptive message about your update
  5. [cancun]$ git push # push your changes to your Gitlab
You should only commit source files since everything else just takes up unnecessary space in your repo and can be (re)built from the source files. Compilation products also become stale (i.e., out of date) as soon as you make any changes to your source files, and having obsolete copies in your gitlab repository can create potential confusion. We have included in your starting repository a .gitignore file that list files and file types that will be ignored when updating the Gitlab repository. This may be particularly useful for not including executables or object files (.o files), which is already done for you. You can add more files that you may not want to include in .gitignore.

Recovering 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 repository, you reduce the chances of getting into trouble. Nevertheless, you might end up with a “Git spaghetti ball,” so here are some useful survival tips and commands.

Keep it simple

Avoid branching, merging, and other complex git structures in your gitlab repo. While these are widely used in large projects and in industry, they are not needed for this class. When not used correctly, these can leave your repo in an unexpected state where new changes seem to disappear or other odd things happen. Keep it simple.

Be Skeptical About Web Searches

Git, github, and gitlab have enormous user bases and are used for a huge variety of projects. They also have many, many, many features and options and are very complex. Because of that, web searches about git can produce a huge number of potentially off-topic or inapplicable results and, if blindly followed, can leave your repo in strange and potentially very hard to fix states. If you are having problems with your gitlab repo, please contact course staff so we can help you fix things. We deliberately are not taking advantage of many git features to keep things simple and understandable for what we need for this class.

Recovery from Messiness Locally

These git commands are ways to manage local changes to your git repository before pushing to your Gitlab repository.

  • Hide changes and gives you a clean working directory without having to commit a meaningless snapshot in order to save the current state.
  • git stash
  • Revisit your previous changes after you're done working on a fix.
    git stash pop
  • Clear the stash stack of any changes you no longer need.
    git stash drop
  • 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 isntead of the entire branch.
    git checkout HEAD -- path/to/file
  • Revert changes that were already committed.
    git reset --soft HEAD~1