Source Control with Git

Overview

Git is a powerful source code control system that allows developers to store their source code, collaborate, and manage changes. It is a key tool in any software engineer's toolbox.

Git uses some terminology that we need to remember:

In this exercise, we will be focusing on how to use Git on the command line, including creating a repository, adding files, committing files, and pushing your changes to a remote repository.

Git in SWEN 250

In this course, Git is not just a tool for you to use, it's your submission system. So understanding how to use Git is critical for for this course. If you mess up your submission, you won't get credit for all your hard work.

You will be creating one personal repository for this entire course. As you work, you will be asked to make many commits at logical times. When you are ready to submit an assignment, then you will push your changes to your repository.

Getting on hamilton

  1. Find PuTTY in the Programs menu and use it to login to hamilton.se.rit.edu using your SE account and password. A few notes:
  2. Create a subfolder in your home directory for this course such as swen250. Use mkdir for this.
  3. Change directory into swen250 using the cd command. You can verify your location by typing pwd to print the working directory. A few notes regarding directories:

Semester Setup

First, let's set up our username in git. You only need to do this once for the entire semester. Use the git config command for changing user.name and user.email, like this:

$ git config --global user.name "Bojack Horseman"
 
$ git config --global user.email "bjh9999@rit.edu"

A neat feature in git command line is to use the coloring scheme to more easily spot the status of things:

$ git config --global color.ui true
  1. Now let's go and create our repository on GitLab. GitLab is a web application that allows you to have a remote Git repository, much like GitHub. RIT has its own installation of GitLab that we will be using for this course. Go to https://kgcoe-git.rit.edu/
  2. Sign into GitLab using your RIT (not SE) username and password.
  3. Create a new "project, and name it swen-250. Make sure the repository is private.
  4. Give Reporter permissions to your instructor and course assistant(s). You may need their usernames - be sure to ask if they have not provided it. To do this, click on the Gear icon in the upper-right and go to Members. Add them as a member with Reporter as the type.
  5. Now that we've created a repository on GitLab, let's clone it onto hamilton. Find the SSH URL for your swen-250 by clicking on Project . The SSH URL will look like git@kgcoe-git.rit.edu:axmvse/swen-250.git (your username will be in there)
  6. Back in PuTTY, use git clone to clone the url. Make sure you clone into your home directory. Your command will look something like this: git clone git@kgcoe-git.rit.edu:axmvse/swen-250.git

Pushing your first code

  1. Change directories into your repository folder on hamilton, using cd
  2. Let's add something to our working tree. Edit a text file called name.txt.
  3. In name.txt, write your full name (so the instructor knows who you are). Save and exit the text editor.
  4. Ok, now that we've written some "code", we've completed our first task. So it's a logical time to add, commit, and push our changes. First, let's see where we stand. Type git status. You should get output like this:
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#       name.txt
nothing added to commit but untracked files present (use "git add" to track)
  1. Before we continue, let’s create a new file which will give save us some time in the future and prevent saving into the repository “stuff” we don’t usually want. For now we will exclude the basics. Let’s create this special file. Create a file in the root of your repository called .gitignore create and edit the file and add the following contents. Be careful that your copy/paste does not ruin the formatting:

Copy starting from line below:

# This file will prevent Git from adding files that match the following
# patterns to the repository. If you need to add a file anyway, you can use `git add -f [file]`
 
# Text editor swap files
*.swp
*.swo
*~
 
# Compiled binary files
*.o

(Copy up to line above)

Note that git will not ignore a file that was already tracked before a rule was added to this file to ignore it. In such a case the file must be un-tracked, usually with git rm --cached filename

As we expect, we've got our new file to add. Add your changes to git by typing git add . Running git status now gives us the following output:

# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached ..." to unstage)
#
#       new file:   name.txt
#
  1. Now it's time to commit. Committing your changes to source control generally means that you are in a relatively stable state. It does not mean that you are done with the assignment - that's not committing enough for this class. Commit when when your program is compiling, and a piece of your work is done. The more often you commit, the easier it is to roll back to a stable state, and the more atomic and understandable your changes are.
  2. To commit, type git commit. By default, git will open up the vim text editor for you to enter your commit message. (If you would rather use nano to edit your commit messages, then run git config --global core.editor "nano"). Git commit messages have this convention:
  3. Write a descriptive message for adding the name file, such as "Recording my name for grading".

  4. Exiting out of the text editor will automatically finish your commit. When that is done, your output will look something like this:

    [master (root-commit) 5d859e9] Recording my name for grading
    1 file changed, 1 insertion(+)
    create mode 100644 name.txt
  5. Now we have one more action: push. We need to push our commits to our remote repository on GitLab so the instructor can view it and grade it. Ok let's push now. The command is git push origin master. The output looks like this:

    UPDATE THIS
  6. Just to double-check that we pushed our code, head back to your browser view of your repository and find your new changes.

You're all set up! We've synchronized our repository with our GitLab so that the instructor can see our changes. From here on, we can continue to modify, commit, and push our code right up to the deadline. Now try out the workflow below and review our tips.

Your Typical Workflow

Here's the typical workflow we want you to use.

  1. Start working by running git status. Commit or remove any changes you might have forgot to deal with before (i.e. start with a clean working tree).
  2. Think about how you will break up this work into smaller tasks
  3. Edit, compile, test (your typical programming workflow)
  4. At the end of one of your mental tasks, add and commit.
  5. Continue to complete your other tasks until the assignment is completed.
  6. When you are ready to submit your assignment, push your changes.
  7. Double-check that all of your changes are pushed with git status.
  8. Double-check your changes are pushed to GitLab.

Learning this pattern of work is critical to your success as a future software engineer. Don't wait to do Git stuff until the very end of your work.

Exercise

For this exercise, let's use our FizzBuzz code into our working tree.

  1. Make a subdirectory for Fizzbuzz - called FizzBuzz.
  2. Copy your FizzBuzz code into your FizzBuzz directory
  3. Commit that code as-is.
  4. Now add some comments to your FizzBuzz source code, and make any other code cleanup that you wish to make.
  5. Commit those changes, giving a descriptive message.
  6. Push to GitLab.

Git Tips for swen250

All of your assignment and projects will be in your personal repository for this course. To speed up grading, please name files and directories exactly as specified in the assignment or project description. Doing this properly is part of your grade.

To check that you have submitted, run these two commands

Note: Git is not a misspelling of "get", it's a self-deprecating joke about Linus Torvalds

Submission

A correctly set up repository.

For this assignment, the instructor and graders will find for your repository on GitLab and make sure it's set up properly.