Crash course in Git
Git is popular source version control system meaning it allows you to track changes to your code during development.
Git has a powerful command-line interface but for our use cases we need only a handful of simple commands listed below. Alternatively, you can use your IDE to issue Git commands from GUI.
Useful links:
Main commands
The most important command in an existing repository:
git statusConfigure Git
git config --global user.name 'Your Name'git config --global user.email 'you@commonwealthu.edu'
Start a repository
- Pre-existing (e.g. on GitHub):
git clone <url>— or, in IntelliJ IDEA, Get from VCS on the welcome screen (File → New → Project from Version Control once a project is already open). It is the same clone, and it opens the result as a project. - Brand new:
git init - Brand new, starting from a project you already have open in IDEA: Git → GitHub → Share Project on GitHub creates the repository on GitHub and pushes to it, so you never visit GitHub’s New button at all.
- Pre-existing (e.g. on GitHub):
Prepare to Commit (staging process)
- Add untracked file or unstaged changes:
git add <file> - Add all untracked files and unstaged changes:
git add .
- Add untracked file or unstaged changes:
Make commits
- Make a commit:
git commit -m 'message'(omitting -m opens a text editor for the message) - Commit all unstaged changes:
git commit -am 'message'
- Make a commit:
View changes
- View unstaged changes from last commit:
git diff - View the difference between a commit and its parent:
git show <commit>(e.g.git show HEAD)
- View unstaged changes from last commit:
Interacting with a remote (GitHub)
- Fetch changes and then merge them into your current branch:
git pull - Push the current branch to its remote “tracking branch”:
git push
- Fetch changes and then merge them into your current branch:
Getting into GitHub with an SSH key
This section is not really about Git. It is about GitHub: how it knows the push is coming from you. This problem is soved by SSH keys.
An SSH key is a pair of files. The private one stays on your computer and acts as your password. The public one goes to GitHub.
Generate a key
There is nothing to install (on modern Windows). Open PowerShell and run:
ssh-keygen -t ed25519 -C "you@commonwealthu.edu"Accept the default file location. You will be asked for a passphrase — leaving it empty will make your life somewhat easier at the cost of security.
You now have two files in C:\Users\<you>\.ssh\:
id_ed25519— the private key. Treat it exactly as you treat a password.id_ed25519.pub— the public key. This is the one GitHub wants.
Register it on GitHub
Copy the public key to the clipboard:
type $env:USERPROFILE\.ssh\id_ed25519.pub | clipOn GitHub: Settings → SSH and GPG keys → New SSH key. Paste, give it a name you will recognize later (“My CU key”), save.
Check that it worked:
ssh -T git@github.comGitHub answers with something like Hi <username>! You've successfully authenticated, but GitHub does not provide shell access. That message is success, not an error.
Working on more than one machine
You need one key, not one key per machine. But the key lives in the home directory of the computer where you made it, and in our labs every machine you login to will have a fresh home directory — so a different seat means a different home directory, with no key in it.
Two ways to handle that:
- Always work on the same physical machine. Simple, but it depends on that seat being free.
- Keep a copy of both key files in your network folder, and copy them into
C:\Users\<you>\.ssh\on whatever machine you sit down at.
The second is the safer default. Do not generate a fresh key every time you move — you end up with a pile of keys on GitHub and no idea which is which.
If SSH refuses a copied key with UNPROTECTED PRIVATE KEY FILE, the copy lost its permissions. Repair them in PowerShell:
icacls "$env:USERPROFILE\.ssh\id_ed25519" /inheritance:r /grant:r "$env:USERNAME:R"When Git cannot find your key
The sign is that ssh -T git@github.com greets you by name, but git push or git clone says:
git@github.com: Permission denied (publickey).
Git comes with its own copy of ssh, and it may look for your key somewhere else. Tell it where the key is. In PowerShell:
git config --global core.sshCommand "ssh -i $($env:USERPROFILE.Replace('\','/'))/.ssh/id_ed25519"That points Git at C:\Users\<you>\.ssh\id_ed25519. The Replace turns the backslashes into forward slashes: Git reads this line the way a shell does, and a shell treats a backslash as special. Check what it saved:
git config --global core.sshCommandIt should print ssh -i C:/Users/<you>/.ssh/id_ed25519. Then try the push again.
If you move to another machine and see the same error there, run the same command there too.