Using Git from the Terminal or Git-Bash

These directions assume:

  • Login: newuser
  • Name: New User
  • Email: newuser at alaska.edu

Let's look at our repository before we start. Open your browser and navigate to https://projects.cs.uaf.edu/redmine/login and log in. Go to the "Projects" link and select the project with your login name, then select "Repository". This is what an empty repository looks like:

RepositoryTutorialEmpty.png

Type the code you see in the boxes into your terminal or command line. Everything before the dollar sign describes who you are, what computer you are on, and what directory you are in, for example: newuser@mycomputer:~/Documents/newuser$. You only need to type what is after the dollar sign on the first line, unless otherwise noted.

Change into the Documents directory

newuser@mycomputer:~$ cd Documents

Clone your empty repository. If you receive a server certificate verification failure, complete the steps to fix it in Frequently Asked Questions

newuser@mycomputer:~/Documents$ git clone https://projects.cs.uaf.edu/newuser 

   Cloning into 'newuser'...
   Username for 'https://projects.cs.uaf.edu': newuser
   Password for 'https://newuser@projects.cs.uaf.edu': 
   warning: You appear to have cloned an empty repository.

Change directory into newuser

newuser@mycomputer:~/Documents$ cd newuser

Create a file that tells Git to ignore whatever you would like. In this case, we're telling it to ignore our compiled .class files. vi is a text editing program, but you can also use notepad on Windows or nano on a Linux/Unix system.

newuser@mycomputer:~/Documents/newuser$ vi .gitignore

Add the following to your .gitignore file. When you are done, press the esc button and type :x. You just told Git to ignore any file that ends with a .class, and you can apply this same method to other file types.

   #Make Git ignore all .class files
   *.class

Tell git about yourself

newuser@mycomputer:~/Documents/newuser$ git config user.name "New User" 
newuser@mycomputer:~/Documents/newuser$ git config user.email "newuser@alaska.edu" 

Make a new directory for your CS103 class

newuser@mycomputer:~/Documents/newuser$ mkdir CS103

This is not really necessary, but Git does not allow you to commit an empty directory. An easy way around this if you really want to commit an empty directory is to create an empty file.

newuser@mycomputer:~/Documents/newuser$ touch CS103/NOTEMPTY

Now we can add the "empty" directory to Git.

newuser@mycomputer:~/Documents/newuser$ git add CS103

Let's see how we're doing

newuser@mycomputer:~/Documents/newuser$ git status

   # On branch master
   #
   # Initial commit
   #
   # Changes to be committed:
   #   (use "git rm --cached <file>..." to unstage)
   #
   #       new file:   CS103/NOTEMPTY
   #

That looks right to me, so let's commit the change. The > are carriage returns.

newuser@mycomputer:~/Documents/newuser$ git commit -m "Added CS103

   > 
   > This directory holds all of my Introduction to Programming material" 
   [master (root-commit) 46a44b8] Added CS103
    0 files changed
    create mode 100644 CS103/NOTEMPTY

I like to keep my files separated by Chapters, Projects, Presentations, and whatever else is appropriate.

newuser@mycomputer:~/Documents/newuser$ mkdir CS103/Chapter1

Now let's do some homework.

newuser@mycomputer:~/Documents/newuser$ vi CS103/Chapter1/Homework1.java

Add the following to your homework file. When you are done, press the esc button and type :x. You just made a Java program!

   /**
    * New User
    * CS103
    * Dr. Java
    */
   public class Homework {
       public static void main(String[] args) {
           System.out.println("I love CS103 homework!");
       }
   }

We need to add the homework to Git so changes are tracked from one commit to the next.

newuser@mycomputer:~/Documents/newuser$ git add CS103/Chapter1/Homework1.java 

Now let's commit our change. The > are carriage returns.

newuser@mycomputer:~/Documents/newuser$ git commit -m "Added Homework1

   > 
   > Homework1 is from Chapter1: Learning how to love Java" 
   [master e106c08] Added Homework1
    1 file changed, 10 insertions(+)
    create mode 100644 CS103/Chapter1/Homework1.java
 

Let's check the log again to see how we're doing.

newuser@mycomputer:~/Documents/newuser$ git log

   commit e106c08daa91fe12f8b66d0b62d19b9c5925c81d
   Author: New User <newuser@alaska.edu>
   Date:   Fri Jun 7 15:37:05 2013 -0800

       Added Homework1

       Homework1 is from Chapter1: Learning how to love Java

   commit 46a44b804b52c90ad4ef5dbd2ba9393c30598353
   Author: New User <newuser@alaska.edu>
   Date:   Fri Jun 7 15:32:23 2013 -0800

       Added CS103

       This directory holds all of my Introduction to Programming material

Git set https://projects.cs.uaf.edu/newuser as the origin of our code when we cloned it earlier, but let's just make sure

newuser@mycomputer:~/Documents/newuser$ git remote -v

   origin       https://projects.cs.uaf.edu/newuser (fetch)
   origin       https://projects.cs.uaf.edu/newuser (push)

We are also on our master branch...

newuser@mycomputer:~/Documents/newuser$ git branch

   * master

…and this is the branch we want to "push" to in our remote repository.

newuser@mycomputer:~/Documents/newuser$ git push origin master

   Username for 'https://projects.cs.uaf.edu': newuser
   Password for 'https://newuser@projects.cs.uaf.edu': 
   Counting objects: 9, done.
   Delta compression using up to 2 threads.
   Compressing objects: 100% (4/4), done.
   Writing objects: 100% (9/9), 816 bytes, done.
   Total 9 (delta 0), reused 0 (delta 0)
   To https://projects.cs.uaf.edu/newuser
    * [new branch]      master -> master

Let's look at our repository again. Open your browser and navigate to https://projects.cs.uaf.edu/redmine/login and log in. Go to the "Projects" link and select the project with your login name, then select "Repository":

RepositoryTutorialAdded.png

RepositoryTutorialEmpty.png (166 KB) John Quan, 06/24/2013 01:32 PM

RepositoryTutorialAdded.png (233 KB) John Quan, 06/24/2013 01:32 PM