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:
- Working Tree: this is just a fancy name for your code,
stored in directories however you like.
- Repository: the Git database of your project's entire
history. Everyone has a copy of the repository stored in a
.git
directory at the root of your working tree. You do not need to edit
anything in there directly, only through a git
command
on the command line.
- Add: take the changes to your working tree and get
ready to store them in the repository.
- Commit: store the changes from the
add
command into your local repository. When you commit, you need to
write a descriptive message of the changes, and Git also records the
timestamp.
- Push: take the commits stored in your local repository
and place them in a remote repository, thereby integrating your work
with others' work.
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
- 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.
- We suggest creating a subfolder for this course, say
se350
.
Use mkdir
for this.
- Change directories into
se350
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 ~/se350
- 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:
- Task 1: Fill in your
name.txt
file
- Task 2: Store your code from the FizzBuzz activity in
a subdirectory.
- 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"
- 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/
- 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. Check out our resources page for more information
on learning different editors.
- In
name.txt
, write your name (so the instructor
knows who you are).
- 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
- 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
#
- 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
- 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".
- 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
- 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!
- 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
- 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.
- 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).
- Think about how you will break up this work into smaller
tasks
- Edit, compile, test (your typical programming workflow)
- At the end of one of your mental tasks, add and commit.
- Continue to complete your other tasks until the assignment is
completed.
- When you are ready to submit your assignment, push your
changes.
- 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.
- Make a subdirectory for FizzBuzz - called FizzBuzz.
- Copy your FizzBuzz code into your FizzBuzz directory
- Commit that code as-is.
- Now add some comments to your FizzBuzz source code, and make
any other code cleanup that you wish to make.
- Commit those changes, giving a descriptive message.
- Push to your pushbox.
Git Tips for SE350
Submission: A correctly set up
/pushbox
- For this assignment, the instructor will find for your
pushbox and make sure it's set up properly.