Skip to content

Git and GitHub

30 minutesWeek 1Needs: Install Eclipse

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.

How work moves from your folder to GitHubFour stacked boxes joined by downward arrows. Your folder holds the files you edit. The command git add moves changes into the staging area, which holds what will go into the next commit. The command git commit moves them into your repository, the history stored on your laptop. The command git push copies them to GitHub, the copy other people can see.Your folderStaging areaYour repositoryGitHubthe files you editwhat goes into the next committhe whole history, on your laptopthe copy that is not on your laptopgit addgit commitgit push

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

winget install -e --id Git.Git

Close PowerShell and open a new one, then check:

git --version
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:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Use the same email you will use for GitHub, so your commits are linked to your account.

3. Create your repository on GitHub

  1. Create an account at github.com if you do not have one, using the email from step 2.
  2. Click New repository.
  3. Name it java-oop, followed by your surname. For example java-oop-hoxha.
  4. Leave it Public, unless your lecturer tells you otherwise.
  5. Do not tick "Add a README". You are about to push an existing folder, and an empty repository makes that simpler.
  6. 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:

cd C:\dev\workspace\Setup
git init

Before your first commit, tell git what to ignore. Create a file called .gitignore in that folder containing:

bin/
*.class
.settings/
.classpath
.project

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 add .
git status

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.

git commit -m "First Java project, verifying the setup"

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:

where.exe git

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:

git config --global credential.helper manager

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:

git pull --rebase origin main
git push -u origin main
You committed bin/ by accident

Add the .gitignore if it is missing, then stop tracking the folder without deleting it:

git rm -r --cached bin
git commit -m "Stop tracking compiled output"
git push

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:

Get-ChildItem -Force -Name .git

Check yourself

Prove the whole loop works

Make a change, commit it, push it, and see it appear.

  1. In Eclipse, open Check.java and add a second System.out.println line. Save.
  2. In PowerShell, from the project folder:

    git add .
    git commit -m "Add a second line of output"
    git push
    
  3. Refresh GitHub and open Check.java. Your new line is there.

  4. 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

  1. 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.
  2. 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?
  3. 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?
  4. 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?