Git on GitHub Exercise
Purpose:
- Figure out how to get a project on GitHub (two options you can explore)
- Figure out how to share the project (i.e., have two people who
are allowed to push code)
- Learn how to merge (pull) when no conflicts
Preparation
This is an partner exercise. Most of your homework should be done
on private repos. For this simple exercise a public repo is fine.
Lesson
Your goal is to create a new project on GitHub and share it between two (or
in some cases three) people. Remember from the slides there are two ways to
set up a project. These instructions will briefly walk through those options.
You should not treat this as a step-by-step tutorial that you walk
through blindly. Try to think at each step about what's happening, on both
local machines and also on GitHub.
Option 1
In this option, the repo is set up on GitHub. You will follow roughly
the following steps.
Person A should:
- Create Repo. Sign into Github and create a new repository.
- It's a good idea to include a README
- GitHub can create a
.gitignore
file (select Java
).
- As stated above, for this assignment, you are allowed to make
a public repository.
- You may want to call your repo
DrawPlay
- Collaborators. Go to the Setting menu and add your partner(s) as collaborator (needed so they can push...
if the repo is public, anyone can clone)
- Clone. On your machine:
- run Git Bash
- cd to your workspace
- Execute
git clone
(remember you
can copy the URL from GitHub, then press Shift-Insert to paste, to avoid typos).
- Sanity check.
- cd into the directory created by the clone command (should be within your
workspace)
- Try
git status
. It should be up to date.
- Also try
ls -a
to see what files you have.
- Unzip.
- You should have downloaded DrawPlay.zip.
- Extract the files into a directory that is NOT your workspace
(this is different between options 1 and 2. The reason is because clone created
the directory... if you extract here, you will overwrite the files).
- Create project. You will need to let Eclipse know about your project.
- First create a new Java Project with the same name as the directory you
just cloned. (may be
DrawPlay
- Be sure that you have selected the same workspace as where you did
the
clone
command.
- Import files. Now import the project.
- Be sure the project you just created is highlighted.
- Select File->Import, open the General folder, select File System then Next.
- Click Browse and navigate to where you extracted DrawPlay (in step 5).
- Click on the src directory and then click Finish.
- After the command executes, you should have two .java files (
Cat
and
MyDrawing
) in your project.
- Try to run the program. You should see a "lovely" cat.
- Commit. First commit to local repo:
- Return to Git Bash.
- Run
git status
.
- You will need to add these files and commit them (you should know these commands;
refer back to your notes from the prior Git exercise if needed).
- NOTE: be sure
.classpath
and .project
are listed in
your .gitignore
file. Remember that you can edit
.gitignore
from within Eclipse (try the File Open command).
- Run
git status
to ensure directory is clean
- Push. In order to share these files, you'll need to push them onto GitHub:
git push origin master
- enter username and password. If you get an authentication error, you are
typing the wrong password (or username).
- Sanity check. Take a look at the repo on GitHub. You should see your changes (spend a few
minutes on this... getting familiar with the GitHub website will make things easier
in the long run)
STOP. Take a minute now to think about what you just did (so you can repeat in the future
with no instructions).
Option 2
In this option, the repo already exists on one person's machine. You will follow roughly
the following steps.
Person A should:
- Extract.
- You should have downloaded DrawPlay.zip.
- Extract the files into your workspace (this is different between options 1 and 2).
NOTE: The DrawPlay directory containing src/bin files should be in your workspace.
You should NOT have a DrawPlay directory that contains a DrawPlay directory. How
to do this depends on your OS and what tool you are using to extract. On my Win7
machine, I use WinZip, and select "Extract to here" option. You may need to experiment
a bit, or maybe extract and then move the directory to your workspace, if that's easier.
- Create Eclipse project.
- In Eclipse, do File->New Java Project. Give it the name of your directory
(most likely
DrawPlay
).
- Run the program.
- Create repo.
- Sign onto GitHub
- Create a new repository. You do not need to create a
.gitignore
or README file. For this assignment, you are
allowed to make a public repository. Note that when you do this, GitHub
recommends some steps to create your
repo. Take a look at what they recommend.
- Collaborators.
- Go to settings menu
- Add person B as a collaborator (as above, needed so person B can push,
but everyone can see).
- Local repo. On your machine, set up your local repository as you've
been doing:
- cd into your project directory
git init
- create a
.gitignore
file. Remember that the idea is .gitignore
specifies files that should not be under version control... things like the
binary class files (*.class) and the Eclipse config files (.project, .classpath)).
- It's also recommended to create a
README.md
file.
- add files to your repo (src and .gitignore)
- Commit your files
- do
git status
regularly, to see what's happening
- Connect repo to GitHub. You need to use the
remote
command to set up a "link" between the repo in your workspace and the one on GitHub.
- Remember that you can copy the url from GitHub
git remote add origin [url]
where [url] is copied from GitHub)
- Push. Now push your code onto GitHub:
git push origin master
- You'll need to enter your username and password
STOP. Take a minute now to think about what you just did (so you can repeat in the future
with no instructions).
Both options
Regardless of how the repo is set up, the steps for the second person should
be similar. Person B should:
- Find repo. Sign onto github and find your partner's repo. You can type the name of the
repo into the search (e.g., type in DrawPlay, you'll see lots of them). Or select
Users in the search criteria, and type your partner's name.
- Clone. From Git Bash, cd to your workspace and clone the repo (
git clone
)
- Sanity check. cd into the project directory. Try a
git status
or maybe look
at the log.
- Create Eclipse project. Bring your project into Eclipse. Make sure you have selected the workspace
where you cloned the repo. Then do New->Java Project and use the name of
the project you cloned. You should see the files.
Now that the project is set up on both machines, your goal is to practice pushing and
pulling. Person B should:
- Make some simple changes to the code. We haven't covered GUIs yet, but it
should be easy enough to change the colors, sizing, text, etc.
- From Git Bash, first commit your code, then push your changes onto
GitHub (
git push origin master
).
- You might also want to "track" your branch:
git branch --track
,
which reduces the amount of typing for git pull/push commands
(e.g., git push
rather than git push origin master
)
Person A should now pull the changes, make some other changes, push. Alternate
between Person A and Person B making changes. NOTE: Be careful not to make conflicting
changes. As long as each person does a pull
before writing any code,
this should not be an issue.
Each person should do at least 3 changes/pushes. To develop good habits,
every commit should include a meaningful comment (e.g., "Changed the ear
color to purple" not "Updated the code").
Submit
Create a .txt file of your git log (remember: git log > log.txt
)
and submit on BB. As always, one submission per
pair, and be sure to put your partner's name in the BB comments.
More practice
Note that you can practice these steps on your own, by creating different
workspaces/repos/etc. Try it!