Git and GitHub
Git records every version of your project; GitHub keeps it online and lets others work with you
Set up once
| What you want | Command |
|---|---|
| Check that Git works | git --version |
| Your name and email, written into every commit | git config --global user.name "NAME" and git config --global user.email "EMAIL" |
Call the first branch main |
git config --global init.defaultBranch main |
| Keep your email private | use GitHub’s noreply address (Settings → Emails) as user.email |
See lesson 04, section 1 and lesson 00 for the SSH key.
Track changes
| What you want | Command |
|---|---|
| Start tracking a project | git init |
| What changed, what is staged? | git status (one line per file: git status --short) |
| Why is a file not listed? | git check-ignore -v FILE |
| Show changes you have not staged | git diff |
| Show what the next commit will contain | git diff --staged |
| Stage one file, or everything | git add FILE, git add . |
| Stage only some changes in a file | git add -p FILE (answers y or n for each change) |
| Take a file out of the stage (keeps your changes) | git restore --staged FILE |
| Record the staged changes | git commit -m "MESSAGE" |
| Stage and commit every changed tracked file | git commit -am "MESSAGE" (does not add new files) |
| Delete a file and stage the deletion | git rm FILE |
Taught in lesson 04, sections 2 to 4.
Look back
| What you want | Command |
|---|---|
| One line per commit | git log --oneline |
| The commits that changed one file | git log --oneline -- FILE |
| All branches as a graph | git log --oneline --graph --all |
| What the last commit changed | git show --stat HEAD (HEAD: where you are now) |
| Every change to one file, as diffs | git log -p -- FILE |
| Which commit last changed each line | git blame FILE |
| Get a file back as it was before commit HASH | git restore --source=HASH~1 FILE (HEAD~1: before the latest commit) |
Undo
| What you want | Command |
|---|---|
| Throw away changes you have not staged | git restore FILE (the changes are lost) |
| Put uncommitted changes aside, to switch branch or pull | git stash, later git stash pop |
| Fix the last commit, before you push it | git add FILE, then git commit --amend |
| Undo a commit you already pushed | git revert HASH (a new commit; the history keeps both) |
See Going further: which undo? after the Break it.
Branches
| What you want | Command |
|---|---|
| Make a branch and switch to it | git switch -c BRANCH |
| Switch to another branch | git switch main |
| Merge a branch into the one you are on | git merge BRANCH |
| Delete a merged branch | git branch -d BRANCH |
| Finish a merge conflict | edit the files (remove the <<<<<<<, =======, >>>>>>> lines), then git add FILE and git commit -m "MESSAGE" |
| Stop a merge and go back to before it | git merge --abort |
See sections 7 and 8.
GitHub
| What you want | Command |
|---|---|
| Connect your project to a new GitHub repository | git remote add origin git@github.com:USER/REPO.git |
Where does origin point? |
git remote -v |
| Push a branch the first time | git push -u origin BRANCH |
| Push and pull later | git push, git pull |
| Get new commits without changing your files | git fetch |
| Copy a repository to your computer | git clone git@github.com:USER/REPO.git FOLDER |
| Log in with HTTPS instead of an SSH key | pixi global install gh, then gh auth login; use https://github.com/USER/REPO.git |
| Pull request | push a branch, then Compare & pull request on GitHub; merge when the Reproduce check is green |
| After the pull request is merged | git switch main, git pull, git branch -d BRANCH, git fetch --prune |
.gitignore, as in the template
# Environments: rebuilt from pixi.lock
.pixi/*
# Downloaded data, except its README
data/raw/**
!data/raw/README.md
# Rebuilt by code
results/**
reports/*.html
Commit what people write; ignore what programs download or rebuild. A # starts a comment only at the beginning of a line.
Official cheat sheets and references
- Git cheat sheet, from the Git project: https://git-scm.com/cheat-sheet
- GitHub Git cheat sheet (web page and PDF, several languages): https://training.github.com/downloads/github-git-cheat-sheet/
- Pro Git, the free book from the Git project: https://git-scm.com/book/en/v2
- GitHub Docs, About pull requests: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests