Before starting lab5, it is highly recommended that you review the Git Reference Sheet. Not all commands will be given to you and the document above has the ones you will need to complete this lab.
The slides for "Git Basics" can be found here. Git Basics
The main part will utilize the core git commands you've learned in order to create a new repository, add files, commit them, and push them up to your github.
There is a bit of setup required that you must do this first.
By the end of this lab, you'll know how to create a git repository, add files to it, make changes, push everything to github, and how collaborate effectively using git and github.
Depending on your working environment, perform the appropriate action:
Environment | Action |
---|---|
Linux Lab Machine | Open a terminal window |
Windows or Personal Computer | SSH into data.cs.purdue.edu * |
* You must have X11 forwarding enabled. If you are on the Windows Lab Computer, follow the instructions below:
- Search for 'Xming' in the start menu and run it.
- Open PuTTy
- Expand the 'SSH' tab from the 'Category' list
- Choose 'X11' from 'SSH' list
- Check 'Enable X11 Forwarding'
- Connect like normal to
data.cs.purdue.edu
within PuTTy.
- You need to set up your global gitconfig with your name and email. These will be tied to each commit so that it is known who committed the code.
# replace 'Scott Opell' with your own name
git config --global user.name "Scott Opell"
# replace 'me@scottopell.com' with your email
git config --global user.email "me@scottopell.com"
- We will want to use a global gitignore file, so we don't have to deal with vim swap files, DS_STORE files, or java class files. The command below copies the gitignore file from this repository into your home directory
wget -O ~/.gitignore_global https://github.com/scottopell/cs190lab5/raw/master/gitignore_global
- Now we need to tell git where the global ignore file is
git config --global core.excludesfile ~/.gitignore_global
-
Run the command below. Don't worry about what it does. It sets the settings of git to the safest options.
git config --global push.default simple
- In your home directory, create a new folder named
cs190lab5_<your_username>
. Ex. I would create a foldercs190lab5_sopell
.
mkdir ~/cs190lab5_$USER
-
You will then
cd
into the directory created above andinit
ialize a new git repository.cd ~/cs190lab5_$USER git init
-
Download this template file
LabTemplate.java
into the repository you just created
wget -O ~/cs190lab5_$USER/LabTemplate.java https://github.com/scottopell/cs190lab5/raw/master/LabTemplate.java
add
this file to the repo
git add <filename>
commit
this file to the repo
git commit -m "<commit message>"
-
Log in to GitHub.com using the account you made for cs180
-
Click the Create a New Repository button.
- Enter in a "Repository name" (
cs190lab5_<username>
) and click "Create Repository"
Make sure NOT to check the box that says "initialize this with a readme"
This is fine when you're doing this on your own, but for this lab, commands will not work as expected
-
Copy to your clipboard the URL in the yellow box titled "Quick Setup"
-
In your local git repo (
cs190lab5_<username>
), add the github URL given as a remote.git remote add origin <git URL here>
-
Do your initial push
git push -u origin master
Note that the above two commands come straight out of the "repository created" page on github. You will be prompted for a username and password here, use your login information for github
- Go to the repo settings
- Add your partner as a collaborator
- Clone your partner's repository into your home folder
cd ~
git clone <his/her_git_URL_here>
This creates a folder in your home directory with the name of his/her repository name.
cd
into your partner's repository- In your partner's repository, compile and run LabTemplate
javac LabTemplate.java
java LabTemplate
It will fail because we have an invalid URL (
https://example.com
) insideLabTemplate.java
.
- Open up your partner's
LabTemplate.java
with gedit and find the line withString URL = "https://exammple.com"
. - Replace the URL with one of the two following URLs
https://api.github.com/zen
or
https://api.github.com/octocat
- Now save this file, compile and run again using the above commands. You will now see some output from it.
- While still inside your partner's repository, you will commit these changes and push them up to their repository
git commit -am 'message here'
git push origin master
- Now,
cd
into your own repository. - Open up your own
LabTemplate.java
in gedit. - The file will contain the
https://example.com
URL. Your parter has committed a change to your own repository. Ask him which URL he/she has used. - You need change the
URL
variable inside LabTemplate.java to other URL, the one that your partner did not use when he made changes to your repository. Commit
your changes using the same syntax as above.- Try to push your changes with
git push
NOTE: This WILL cause an error, that's okay, read on to the next step
- Git will tell you that there are new commits on the remote, so you need to pull them before you can push any of your commits.
git pull
- Git will tell you that there are merge conflicts LabTemplate.java
- You need to now open up LabTemplate in an editor of choice, and look for the section resembling the following
// We need to ensure that we use a URL that starts with https
// the s is important
<<<<<<< HEAD
String URL = "https://api.github.com/zen";
=======
String URL = "https://api.github.com/octocat";
>>>>>>> 581ac725455f2c8aef02a48dc5e45e44ccef69f1
// We can print out the contents of any https site now!
System.out.println( enlightenMe(URL) );
- We want to keep our changes, not our partners, so we will delete the content in between ====== and >>>>>>> and the other merge markers. Your code will now look like you would expect, something roughly similar to this.
// We need to ensure that we use a URL that starts with https
// the s is important
String URL = "https://api.github.com/zen";
// We can print out the contents of any https site now!
System.out.println( enlightenMe(URL) );
- We need to tell git that we have a version of LabTemplate.java that we like, so please
add
LabTemplate.java. commit
these changes.
For a commit that fixes merge conflicts, its common practice to use a message similar to "fixes merge conflicts on LabTemplate"
- Push these changes up to your remote repository (github) and verify that you can still compile and run LabTemplate.
You have now created a repository from scratch, added, committed, pushed, pulled and fixed your first merge conflict!
Before raising your hand to be graded, open up your repository on github and open it to the "network" tab. This is right above the settings tab from earlier.