Source Control with Git
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:
- Working Tree: this is just a fancy name for the directory of your code, stored in directories.
- 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 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 Nitron
- 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 OpenBSD; if you don't know much about Linux/Unix/BSD, at some point you should peruse the material in the course schedule related to that. Plan on spending some of your study time for this course getting used to working on
- If you ever need to learn a particular command, use
man ls to learn about the
- Create a subfolder in your home directory for this course such as
mkdir for this.
- 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:
- Your current directory is
- The directory above your current directory is
../. So to go up one directory use
- The ~ (tilde) represents your home directory. Thus, your
swen250 directory can be called
- 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
- You can create a
.profile file in your home directory to automatically use the bash shell. This file should contain exec /bin/bash
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:
A neat feature in git command line is to use the coloring scheme to more easily spot the status of things:
- 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/
- Sign into GitLab using your RIT (not SE) username and password.
- Create a new "project, and name it
swen-250. Make sure the repository is private.
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.
- Now that we've created a repository on GitLab, let's
clone it onto
nitron. Find the SSH URL for your
swen-250 by clicking on Project . The SSH URL will look like
email@example.com:axmvse/swen-250.git (your username will be in there)
- 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 firstname.lastname@example.org:axmvse/swen-250.git
Pushing your first code
- Change directories into your repository folder on nitron, using
- Let's add something to our working tree. Edit a text file called
- 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 mediocre, 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.
name.txt, write your full name (so the instructor knows who you are). Save and exit the text editor.
- 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
- 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:
(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:
- 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").
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:
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
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.
- 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,
- 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
- 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.
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 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
git status - to make sure everything has been committed (should say "# On branch master nothing to commit (working directory clean)")
git push origin master - if it says "Everything up-to-date", you have everything pushed and submitted
- Deleted a file? Use either
git rm or
git add -A . to record that deletion.
- Want to add, commit, push all in one command? Use:
git add . && git commit && git push origin 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.
- Interested in more Git features? Ask your instructor! It's a very feature-rich tool.
Note: Git is not a misspelling of "get", it's a self-deprecating joke about Linus Torvalds
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.