Getting Started with Git

Introduction to Git & SE's /pushbox

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:
    • In PuTTY, if you use your mouse to select text on the PuTTY screen, it is automatically copied to the Windows clipboard. So if you want to copy text from your PuTTY console, just select, then switch to a Windows editor and paste
    • To paste from the Windows clipboard into your PuTTY console, use Shift + Insert.
    • Nitron uses Ubuntu Linux; if you don't know much about Linux/Unix, at some point you should peruse the material in the course schedule related to Linux commands.
  2. We suggest creating a subfolder for this course, say se250. Use mkdir for this.
  3. Change directories into se250 using the cd command. A few notes regarding directories:
    • Your current directory is ./
    • The directory above your current directory is ../. So to go up one directory use cd ../
    • The ~ (tilde) represents your home directory. Thus, your se350 directory can be called ~/se250
  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
    • Use the up and down keys to scroll through previous command you ran
    • In most situations, hitting Tab will auto-complete to known commands and directories

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 ~/se250, type git init .
    You should get the output like:
    Initialized empty Git repository in (your home directory)/se250/.git/
    				
  3. Let's add something to our working tree. Edit a text file called name.txt.
    • If you have never used a command line text editor before, the simplest text editor is nano. Type nano name.txt to create and edit the file, use hotkeys shown at the bottom - (e.g. ^X means Ctrl+X)
    • While nano is a pretty good quick-and-dirty text editor, we strongly suggest learning a more feature-rich editor like vim as the course moves forward.
  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 often enough. 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:
    • The first line is the subject line, which is a concise description with a maximum of 50 characters
    • The second line is always blank
    • Anything on the third line and after is a more thorough description of the changes.
    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. Thus far, we have been showing you material that's non-specific to this department. For remote repositories, we've 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 forgotten 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, adding and committing as needed.
  6. When you are ready to submit your assignment, push your changes. (You can push as many times as you like.)
  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 SWEN 250