Git and Git Hub CS2212 Worksheet.

  1. Make sure that Git is installed on the machine you are working at. If you are using a Windows machine, you can install Git from here:
  2. If you don't have a GitHub account yet, go to the Git Hub website and create an account for yourself --> with your Western Email address
  3. Click on the button that says "Create A New Repo" in the top right corner (the icon looks like a book).
  4. Give it the name "MyFirstRepo" and make it Public. Do NOT check the box that says "Initialize this repository with a README"
  5. Click on the Create repository Button
  6. The next screen is VERY important as it gives you some of the commands you will need AND it tells you where you repo is located THUS LEAVE THIS WEBPAGE OPEN WHILE YOU DO THE NEXT FEW STEPS in Eclipse.
  7. Go into Eclipse and create a new Java Project called "GitTesting", put one class in it called HelloWorld. Put the following code in the HelloWorld Class:
    public class HelloWorld {
             public static void main(String[] args) {
             System.out.println("Hello World");
  8. Run the code to make sure it works.
  9. While in Eclipse, right click on the Project and select Properties. Figure out the location of the project (the directory with the drive that everything is stored in)
  10. WINDOWS USERS: Go to the Start Menu and open Git Bash --> All Programs>Git>Git Bash. At this point you are opening a little window that accepts Unix type commands on a Window will need to remember everything you learned in CS2211 :-)
    MAC/OSX USERS: run Open Terminal from utilities NOTE: Mac users will need to install Git themselves, I am not a Mac user so I can't help you here :-) I was told to tell the Mac users to use Homebrew and get Git for OSX and then type brew install git to get it running but again, I haven't tried this out!
    users open a terminal (make sure you installed Git already)
    ALL USERS: NOTE: if at some point, you get stuck in the Git bash window and it keeps beeping, read this article: (It tells you to push Q to get out of her)
  11. For the first command, type cd drivename: and hit enter to move to the drive that contains you eclipse workspace. For example cd c:
  12. Then type cd foldername to move to the location that the project is stored in. For example cd eclipseworkspace/GitTesting (Note: the slash will be the opposite direction here, use this slash / to separate folders)
  13. Type ls -l
    Notice your src and bin directories
  14. Type git status
    Notice that git tells us we haven't set up the repository yet. git status is a very useful command that will tell us what is git thinks is happening. Use the "git status" command often to check that current state of the repository
  15. Now type git status -h
    For any command where you need more info, just type git command -h (e.g. git commit -h)
  16. We need to do a little setup before we create any local repositories so that our name and email will be associated with the commits (A commit is when you change the code and send it to the repository).
    1. type which git
      this will tell you where your system thinks git is installed
    2. type git config -l
      this will tell you what default values are all ready set. We want to set the and the
    3. type git config --global "FirstName LastName"
    4. type git config --global "your@email.address"
    5. type git config -l
      to make sure it all worked!
  17. Now we are going to create a LOCAL repository on our machine to hold this project. (This will just create a repo in the same area that contains the project, it will NOT move it up to GitHub yet). Type git init
    git init is the command to initialize a new repository.
  18. Again type git status
    Notice that there is now a repo and it is telling you that it can see "untracked" files, in other words, there are files (and folders) that you haven't put in your repo yet, let's now add the files and folders:
  19. Now type git add .
    This command will "stage" all the files (that means make them ready to go into the repository). If you just want to get some files ready, for example, then do
    git add src/
  20. Now type git status
    Notice that the files are now ready to be committed to the local repo
  21. We probably don't want to keep track of binary files (such as bin/HelloWorld.class), so lets set up our repository so that it will not keep track of those files. In the same directory as your GitTesting project, create (using Notepad say) a file called .gitignore (IT MUST BE CALLED .gitignore and in the same directory where you created your local repository). Put the following into .gitignore
    # Ignore my bin folder
    and save the file.
    Now type git add . and the type git status
    Notice that /bin is still ready to be committed. To fix this you need to remove it just once as follow, type: git rm --cache -r bin
    Now type git status
    Any files or folder that you want to ignore, just add them to the bottom of the file called .gitignore (NOTE: this is also a good place to tell it to ignore your .pub key you created (if that key is in the same area as the files going into your repository) so that just in case you have a public repository, others can't see your public key and use it to mess around with your repository).
  22. Now type git commit
    We are now adding the files to the local repo. We also need to put a comment for every commit to tell our group members what we were doing. I am not a fan of vi but that is what is the default editor in Git for Windows, so here are the commands you will need to enter a commit comment:
    press i (to insert text)
    type in your comment such as "My first commit ever in Git"
    press the Esc key
    press :wq (for write/save to the file then quit the editor vi)
  23. Now type git status
    Notice that everything in the repo. The repo now has a branch called master. Think of master (or a branch) as one version of your repository, someone else might "Branch off" from the master code and make another version and then you and this other person will "merge" your versions together. REMEMBER the local repository is initially called master
  24. Now we need to tell our local repo which repo on GitHub it will be associated with. I.e. we need to make a link with GitHub between our remote repo and our local repo. This is when we need to go back to our webpage that we left open that contains the name of repo. Right under where it says Quick setup you will see a box next to the buttons HTTP and SSH, make sure the SSH button is clicked, you should see something in the box like:,
    copy the contents of the box. We are going to give our remote repo the name of "origin" on our local machine (this is just some GitHub conventions but you could call the remote repo anything you wanted on your local machine) and then connect origin to our GitHub repository. Go back to your Git Bash window on windows and type the command:
    git remote add origin
  25. Now type git status
    Notice that nothing has really happened. All we have done in the previous step is set up the link between our local repo and the remote repo. But we still need to "PUSH" our code up to the remote repo. The command: git remote will show you the name of your remote in case you forget!
  26. Now type git push -u origin master
    This is trying to put your local repository (the master) onto the remote one (that you called origin) Notice that you are getting a Permissions error. This is because GitHub is not sure if you are who you say you are. You could just keep signing in and out of GitHub with a password but this would get to be a pain, so instead you can generate a key pair. This is a set of two keys that "fit" together or go together. Every time you try to push stuff up to GitHub, GitHub will check if the key on GitHub FITS with the key on your local machine. If they match, you will be allowed to push and pull from the remote repository. First we need to generate the two keys:
  27. In Git Bash type ssh-keygen -t dsa
    Use the default file folder to save the key in (just hit enter)
    If you get a warning that you are going to overwrite a file, select n and go to the next step
    Hit enter for the passphrase (NO PASSPHRASE) (you don't really need to set one and it is just more passwords to remember, so don't bother setting one)
    Hit enter again
    What you just did was generate two files: one called id_dsa (the private file that says on your machine) and one file called (the public file that you copy the contents of it up to GitHub)
  28. cd to the drive where the keygen was placed and then cd to the directory where the above command stored your keys. (probably ~/.ssh)
  29. type ls
    The file called id_dsa is the private key (the local key) and the one called is the public key (the remote key)
  30. Using Notepad or some editor, open the file called and copy the contents of it into your clipboard
  31. We now have to move the contents of the clipboard onto GitHub. Go back to the webpage for GitHub and click on the icon in the top right called Account Settings (it looks like a wrench and a screwdriver)
  32. Click on SSH Keys on row of buttons along the left
  33. Click on Add SSH key
  34. Give the key the title "MyWorkshopKey" and past the contents of the clipboard into the key box below.
  35. Click on the Add Key button
  36. Go back to Git Bash on Windows and move back (cd) to the directory where your repo is
  37. Type git push -u origin master
  38. NOTE : you might get an error here because about simple and matching, if you get that error, type this command: git config --global push.default simple
  39. Go back to the GitHub webpage , in the account setting page , click on Repositories, (right under SSH Keys), then click on your repo link and you should see all your files there now (except for bin which was ignored).
  40. Now we are going to have your whole group get an existing repository from a private repo on GitHub and then each group member will mess around with the files and see what happens.
  41. I have added each of you to GitHub as members of a team for your groups private repository. This gives your group access to this repository.
  42. Open Eclipse and create a new Java project called GitGroupProject
  43. Go into Git Bash, move to the location of you Eclipse workspace and cd to the src directory for the GitGroupTesting.. Thus, you should be somewhere like C:/eclipseworkspace/GitGroupTesting/src Make sure that the src directory is EMPTY, it can not have any files in it. Type the following command where ? should be your group number (MAKE SURE YOU INCLUDE THE . (DOT) AT THE END OF THIS COMMAND!):
    git clone  .
    This command will copy(pull down) the 2 files we have created into your src directory and create a repository there. The files that should have been pull down from GitHub are:

    import java.util.Vector;

    public class OurGroup {

         private Vector<String> groupMembers;
         public OurGroup()
              groupMembers = new Vector<String>();
              groupMembers.add("Stan Smith");
              groupMembers.add("Bart Simpson");
              groupMembers.add("YOUR NAME"); //PUT YOUR NAME HERE
         public Vector<String> getGroupMembers () {
              return groupMembers;


    import java.util.Iterator;

    public class ShowOurGroup {

         public static void main(String[] args) {
              OurGroup us;
              us = new OurGroup();
              Iterator itr = us.getGroupMembers().iterator();
              while (itr.hasNext()) {



  44. Go back into Eclipse and into src an open the class called
  45. In the spot where it says: //PUT YOUR NAME HERE change it so it says groupMembers.add("YOUR NAME"); where Your Name is YOUR NAME :-)
  46. Run the program and make sure your name is displayed under Bart's and Stans names.
  47. Wait for everyone in your group to complete the step above and have everyone do this command at the same time: git pull
    it should say already up-to-date because the repository on GitHub doesn't have anyone changes yet.
  48. Now, all at the same time, try to do this following:
    git add .
    git commit -m "My first group changes to"

    Note: the -m on the commit command allows you to put a short little commit message, thus you can either add the commit message using vi or if it is a short message using the -m command
  49. First let's try to undo some commits that are JUST on our machine (local commits). Now go back into Eclipse and in the file:, change the text that says "Bart Simpson" to "Homer Simpson", save the file again in Eclipse and then do the following:
    git add .
    git commit -m "now homer is bart "
  50. Now change "Homer Simpson" to "Lisa Simpson", save the file in Eclipse and do the following
    git add .
    git commit -m "now lisa is homer"

  51. Now change "Lisa Simpson" to "Marge Simpson" save the file in Eclipse and do the following
    git add .
    git commit -m "now marge is lisa"
  52. Now change "Lisa Simpson" to "Ned Flanders and SAVE THE FILE BUT DO NOT ADD OR COMMIT THIS TIME
  53. Firstly, lets see all our changes:
    git log
  54. Now lets see our differences from our last committed change (when we put Marge Simpson in) to any changes we have made since the commit (we have put Todd Flanders in)
    git diff
  55. Now let's suppose that we really wanted Homer Simpson to be there. We can use the git reset command to do this BUT never use the git reset command if you have already pushed these changed to GitHub, only use reset if the changes are still ONLY local (have never been pushed up). The reason is because you might reset (go back a few commits) and then push up to the master and in the meantime, one of your groupmates may have pull and when they push, the master will now be out of sync. SO ONLY USE GIT RESET ON UNPUBLISHED (unpushed) COMMITS!! Let's try reset now:
    1. First close the class in Eclipse
    2. type the command git reset --hard HEAD~2
    3. The ~2 means go back 2 commits from the HEAD (where you are now) Open the file again and notice the change
    4. NOTE: ALL changes you have made that have not been commited will be LOST, so be careful with this command
  56. Now open and change "Homer Simpson" to "Peter Griffin", then add the file to the stage (git add and commit it (git commit -m "peter added")
  57. Now again, wait for everyone in your group to get to this step. Close the file and then have everyone type this command: git push -u origin master
  58. The person who did it first will have success, but the rest of the group should have problems. Have all the members (EXCEPT FOR THE SUCCESSFUL PERSON), in Git Bash, who got an error do the command: git pull
    This will pull down the latest version and try to merge the changes that the person who got their first with your changes.
  59. Open up the file again and notice how Git tried to show you the differences (where you added stuff and where your successful group member added stuff). Remove the Git comments and make sure both your name and your group members name are there (the line he/she added), and then do the following commands again:
    git add .
    git commit -m "Lets try this again"
    git push
  60. If you do this before the other unsuccessful members in your group, it should work, if not, you might have to try again. Keep trying until every group members name is in the file. (If a member is unsuccessful, they will have to pull again).
  61. If you want to see all the commits that have happened (yours and everyone elses) type:
    git log
    This allows you to see everything that has happened so far. Now try typing git log --oneline
    this shows you each commit on a single line AND it shows you the number for each commit (it probably only needs to show you about 7 characters of the long commit, you can use those 7 characters and do something like: git log 50a7c8b (except change the 7 characters to be the one of the ones displayed for your log). Try this command also
    git log --graph --decorate --oneline
  62. If you want to see the changes that have happened between what you have typed right now and a previous version, you could do:
    git diff 7charcommitnumber or just try typing
    git diff
  63. Now let's try to undo something, first undo the last commit that was made with the following command:
    git revert HEAD
  64. do git log --oneline and find one of the earlier commit numbers and then try doing:
    git revert 7charcommitnumber
    Note: if you now tried to do git push to put the older version up, this might not work exactly because your group members might all being doing the revert at the same time and thus you will need to resolve the problems.
  65. git revert will roll back ALL your changes to that commit, so if you really just want to change one file but not roll back all the other other files, git revert is overkill, instead use git checkout, for example try:
    git checkout HEAD
    this will give you the version of JUST before the last commit but it will not touch any of the other files. Then you can make your changes and pull first (which will merge your conflicts) and then do your push.
  66. Now type:
    git log --pretty=format:"%h - %an, %cn, %ar : %s"
    This will is a neat clean way of seeing who has been working on the project.
  67. REMEMBER THE MOST IMPORTANT THING TO REMEMBER is PULL IS YOUR FRIEND!, always PULL just before you start working on the project (to get the latest version) AND PULL BEFORE YOU PUSH (so you get any changes merged in before adding your new code)
  68. Show Laura the output from the above command (git log --pretty...) so that she can see EVERYONE in your group has done some work on your repository. Then she will give you the 2%for you git grade.
  69. If you want more information on merging, watch this video made by one of our PhD students -->
  70. The work flow for git should be to pull before you start doing work, do some work, do git add . and git commit ... then before doing the push, do one last pull to make sure you didn't overwrite anyone.
    Remember the work flow:
    1. Pull
    2. Add
    3. Commit
    4. Repeat steps 2 and 3 until you are ready to move up your stuff to GitHub
    5. Pull (just to make sure no one in your group changed the files you are working on)
    6. Get rid of any conflicts (i.e. merge) and repeat steps 2 and 3
    7. Pull again
    8. Push
  71. Let's do one last thing that you will need in order to hand in your assignment. We will indicate a point in time with a tag. This will help us with your assignments because you can "tag" your commit. This will zip all the files together and the t.a. can grab the push that was tagged and know that this the version of the code that you wished to be marked. To tag your code do the following:
    git add .
    git commit
    git tag -a tryinggitlab -m "My first tag message"
    git push --tags
  72. Now to into GitHub and look at the tags tab. Then click on the area that says 1 release . Then you will see that your whole repository has been zipped up into one file that the t.a. can download to mark. When you are ready to hand in your code, use the tag option to put everything together.
  73. I found this really good tutorial with lots of great visualizations if you need more help, review it:
  74. Your group can now delete all the files I put in the GitHub in order to start fresh and start working on your big project!

Review for initial creation of Git Hub and Git repository:

You don't have a repository locally or on Git Hub:
  1. Create a new repository on Git Hub, do NOT check the box that says "Initialize this repository with a README"
  2. On the local machine, move to the area where the files/folders are that you want to put in the repository and do the following commands on the local machine:
    git init  
    git add .
    git commit -m "first commit"
    git remote add origin
    git push -u origin master
You have an existing EMPTY repository on Git Hub:
  1. On the local machine, move to the area where the files/folders are that you want to put in the repository and do the following commands on the local machine:
    git init 
    git add .  
    git commit -m "first commit"  
    git remote add origin  
    git push -u origin master
You have an existing repository on GitHub that contains files that you want to copy to your area:
  1. On the local machine, move to the area where the files/folders are that you want to put in the repository and do the following commands on the local machine:
    git clone 
  2. NOTE: if you want to clone into an existing empty directory (such as the src directory created by Eclipse for a Java project), on the local machine, move to that directory and type the following:
git clone .