Introduction to Git

Overview

Git is a powerful source code control system that allows developers to store their source code, collaborate, and manage changes. For this tutorial, 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 is also how we will be submitting assignments in this class. 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 /pushbox

Setup

  1. Find PuTTY in the Programs menu and use it to login to nitron.se.rit.edu using your SE account and password. A few notes:
  2. We suggest creating a subfolder for this course, say se350. Use mkdir for this.
  3. Change directories into se350 using the cd command. A few notes regarding directories:
  4. We also suggest using the Bourne-Again Shell on the command line because it gives you more features. When you first reach the command line, type bash

Initial Setup and Activity

Source control systems are great for breaking up your work into logical tasks. For this activity, let's work to complete two tasks:

  1. First, let's set up our username in git. Use the git config command for changing user.name and user.email, like this:
    $ git config --global user.name "Chuck Norris"
    $ git config --global user.email "chuck_norris@rit.edu"
    				
  2. Now let's create a repository. From within ~/se350, type git init .
    You should get the output like:
    Initialized empty Git repository in (your home directory)/se350/.git/
  3. Let's add something to our working tree. Edit a text file called name.txt.
  4. In name.txt, write your name (so the instructor knows who you are).
  5. 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
    				
    				
    				
    				
  6. 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
    #				
    				
    				
    				
    				
    				
  7. 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. To commit, type git commit
  8. 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" - but we hope you switch back by the end of the term!). Git commit messages have this convention: For this class, the most important element is a well-written subject line. "A few changes" is the worst possible commit message - be descriptive without being verbose (50 characters is short - this parenthetical is 49).
    Write a descriptive message for adding the name file, such as "Recording my name for grading".
  9. 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			
    			
  10. Now we have one more action: push. We need to push our commits to a remote repository so the instructor can view it and grade it. The instructors have set up a special directory on nitron, called /pushbox, that allows you to securely push your changes. We have also provided a script that sets up your remote repository properly and hooks up your personal repository with the remote. Here's an example run of the script:
    /pushbox/make-my-pushbox.sh se350-01 winter2012
    (replace the 01 with your section and winter2012 with whatever your term happens to be).
    If everything in the script worked, then you should get the output that looks something like this:
    Initialized empty Git repository in /pushbox/se350-winter2012-chuck-54cd8f737bcb5e8b33916193f56d6e94/
    Success!
    			
  11. Ok let's push now. The command is git push pushbox master. The output looks like this:
    Counting objects: 3, done.
    Writing objects: 100% (3/3), 236 bytes, done.
    Total 3 (delta 0), reused 0 (delta 0)
    Unpacking objects: 100% (3/3), done.
    To /pushbox/se350-winter2012-chuck-54cd8f737bcb5e8b33916193f56d6e94
     * [new branch]      master -> master
    		
  12. You're all set up! We've synchronized our repository with our pushbox 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

Typical Workflow Exercise

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.
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 your pushbox.

Git Tips for SE350

Submission: A correctly set up /pushbox