Git and GitHub¶
You will install git, create your own repository on GitHub, put your first project in it, and push it. After this page your work has a history and a copy that is not on your laptop.
This is not administration. Every sprint in this course is delivered from this repository, and at the end of the semester it is the thing you show someone. Starting it properly in week one costs half an hour. Starting it in week nine costs a weekend.
The four places your work lives¶
Git is confusing at first because your files exist in several states at once, and you cannot see any of them in Explorer.
Three of those four are on your own machine. Only the last one involves the internet, which is why most of git keeps working on a train.
1. Install git¶
Close PowerShell and open a new one, then check:
If winget is not available
Download the installer from git-scm.com/download/win and run it. Accept every default. The defaults include Git Credential Manager, which is what saves you from typing a password on every push.
2. Tell git who you are¶
Every commit records a name and an email. Set them once:
Use the same email you will use for GitHub, so your commits are linked to your account.
3. Create your repository on GitHub¶
- Create an account at github.com if you do not have one, using the email from step 2.
- Click New repository.
- Name it
java-oop, followed by your surname. For examplejava-oop-hoxha. - Leave it Public, unless your lecturer tells you otherwise.
- Do not tick "Add a README". You are about to push an existing folder, and an empty repository makes that simpler.
- Click Create repository.
GitHub now shows you a page of commands. Ignore it. The next step covers what you need.
4. Turn your project folder into a repository¶
In PowerShell, go to the project you made on the last page:
Before your first commit, tell git what to ignore. Create a file called .gitignore in that folder containing:
Never commit the bin folder
bin holds the .class files Eclipse generates from your source. They can be rebuilt from your code in one second, they change on every single build, and committing them fills your history with noise that hides your actual work. The rule underneath: commit what you wrote, never what a tool produced from it.
5. Your first commit¶
git status lists what is staged. Read it before committing. If you see bin/ in that list, your .gitignore is in the wrong folder or has a typo.
6. Connect it to GitHub and push¶
Copy your repository's URL from GitHub. It looks like https://github.com/yourname/java-oop-surname.git.
git branch -M main
git remote add origin https://github.com/yourname/java-oop-surname.git
git push -u origin main
A browser window will open asking you to sign in to GitHub. That is Git Credential Manager. Approve it once and it will not ask again on this machine.
Refresh your repository page. Your files are there.
When it goes wrong¶
Invalid username or token. Password authentication is not supported
GitHub stopped accepting account passwords over HTTPS years ago. You need Git Credential Manager to handle sign-in, and it comes with Git for Windows.
Check which git you are using:
If it is not under C:\Program Files\Git\, you have a different git installed that cannot reach the credential helper. Install Git for Windows from step 1.
Then point git at the helper explicitly:
Push again. A browser window should open.
src refspec main does not match any
You have not committed anything yet, so there is no main branch to push. Run git status. If it lists files as untracked or staged, go back and finish step 5.
failed to push some refs / Updates were rejected
GitHub has something your local repository does not, usually because a README or licence was added when the repository was created. Pull it in first:
You committed bin/ by accident
Add the .gitignore if it is missing, then stop tracking the folder without deleting it:
The files stay on your disk. Git just stops watching them. The old commits still contain them, which is fine and not worth fixing now.
fatal: not a git repository
You are in the wrong folder, or git init was never run. Check where you are with pwd, and look for a hidden .git folder:
Check yourself¶
Prove the whole loop works
Make a change, commit it, push it, and see it appear.
- In Eclipse, open
Check.javaand add a secondSystem.out.printlnline. Save. -
In PowerShell, from the project folder:
-
Refresh GitHub and open
Check.java. Your new line is there. - Click the commits link. You now have two, with your messages and your name.
That history is the thing this page was really for. From now on it grows every week, and by week 14 it is a record of how you built the whole project.
Think about it¶
- Git keeps your work in three states on your own machine before anything reaches GitHub. Why not skip the middle and just upload the folder? Now suppose you have changed six files and only two of them are finished.
- You were told not to commit
bin/, but everything in it came from files that are committed. What is the general rule underneath that? Now imagine a generated file that takes an hour to produce rather than a second. Does the rule still hold, and what would you do instead? - A commit message is written for a reader. Who is it, and when do they read it? Now imagine it is you in week twelve, hunting for the last commit where the program still worked. What would you want the messages to look like?
- GitHub is unavailable for a whole day. What can you still do, and what can you not? What does your answer tell you about where your repository actually lives?