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 hamilton.se.rit.edu using your SE account and password. A few notes:
  2. We require that you create a subfolder for this course called swen250 . The command is: mkdir swen250
  3. Now change the permissions for this folder: chmod 700 swen250
  4. Now verify that your permissions are correct: ls -ld swen250
    The first part of the line should look like this: drwx------
  5. Change directory into swen250 using the cd command. If you are in your home directory you type cd swen250. You can verify your location by typing pwd to print the working directory. A few notes regarding directories:
  6. 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. Carefully follow the instructions below:

  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 "cxn9999@rit.edu"

                        
  1. 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  

  2. Now let's create a repository. From within ~/swen250, type git init .            ONLY RUN THIS COMMAND ONCE!!!
    You should get the output like:

Initialized empty Git repository in (your home directory)/swen250/.git/

  1. Let's add something to our working tree. Edit a text file called name.txt.

  2. In name.txt, write your name (so the instructor knows who you are).

  3. 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. As before use nano .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

 

  1. 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. To commit, type git commit

  2. 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:

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".

  1. 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             
                 
  1. 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 hamilton, 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 swen250-NN summer2168      CHANGE NN to be your section (01 through 04)! ONLY RUN THIS SCRIPT ONCE!!! 


If everything in the script worked, then you should get the output that looks If everything in the script worked, then you should get the output that looks something like this:

Initialized empty Git repository in /pushbox/swen250-01-spring2165-cxn9999-54cd8f737bcb5e8b33916193f56d6e94/
Success!
                 
  1. 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/swen250-01-fall2151-cxn9999-54cd8f737bcb5e8b33916193f56d6e94
 * [new branch]      master -> master
        
  1. 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 swen250

git add . && git commit && git push pushbox master

Also, in bash, you can use the up arrow to bring that command back up quickly. If you realize you don't want to commit partway through, then exit your text editor with an empty commit message and everything aborts safely.

 

Submission: A correctly set up /pushbox