Git Basics

This guide covers the day-to-day git workflow you'll use in CSE 333: keeping your local and GitLab repositories in sync, recovering from common mistakes, and tagging commits for submission.

All commands below are run from your CSE Linux environment (attu or a CSE Linux VM). Your GitLab repository and your local copy do NOT sync automatically, so it's your responsibility to keep both up to date and avoid stale copies. The workflow here should suffice for CSE 333; for more depth, see the Git Community Book.

Updating Local and GitLab Repositories

Update Local from GitLab

Navigate to anywhere within your local repository and run:

$ git pull

Update GitLab from Local

Make sure you are in your local repository and that all changes are saved before running 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) as <files>, which stages all added/deleted/modified files in the local repo because . represents the current directory.
    • Omit any added/deleted/modified files from your git add command that you don't want committed 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 a smooth, efficient workflow. Frequently updating your local and GitLab repositories reduces the chance of getting into weird situations. Nevertheless, it's 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 the 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:
    Screenshot showing where to find the commit you want to revert.
  3. Click on options on the top-right side of the page and press revert:
    Screenshot showing the location of the Revert option in the options menu.
  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:
    Screenshot of the revert confirmation modal with a Revert button at the bottom.
  5. Your repository has been updated! Don't forget to update your local repository by running git pull.
    Screenshot of the final page after a successful git revert.

Managing Git Tags

Your homework and later exercises are 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)

Add a tag after you have completed the assignment. The tag marks a specific commit (in this case, your latest one) with a name that makes it easy 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