# Git & GitHub Cheatsheet

The Git workflow we use across all labs and the capstone.

## One-time setup

```bash
git config --global user.name  "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global pull.rebase false
```

## Daily workflow

```bash
git status                          # what's changed
git add file.html                   # stage one file
git add .                           # stage everything (be careful)
git commit -m "Add hero section"
git push                            # push to remote
git pull                            # fetch + merge from remote
```

## Cloning & remotes

```bash
git clone https://github.com/USER/REPO.git
git remote -v                       # list remotes
git remote add origin URL           # add remote
```

## Branches

```bash
git branch                          # list local branches
git branch feature/login            # create
git switch feature/login            # switch (modern)
git checkout feature/login          # switch (older syntax)
git switch -c feature/login         # create + switch

git merge feature/login             # merge into current branch
git branch -d feature/login         # delete merged branch
```

## Inspecting

```bash
git log --oneline --graph --all     # pretty history
git log -p file.html                # changes per commit
git diff                            # unstaged changes
git diff --staged                   # staged changes
git show <commit>                   # full commit
git blame file.html                 # who changed each line
```

## Undoing things

```bash
git restore file.html               # discard unstaged changes
git restore --staged file.html      # unstage but keep changes
git commit --amend                  # fix last commit message / add files
git revert <commit>                 # safe undo (creates a new commit)
git reset --soft HEAD~1             # undo last commit, keep changes staged
```

⚠️ Avoid `git reset --hard` and `git push --force` on shared branches.

## .gitignore

Common entries for our course:

```
# Node
node_modules/
.env
.env.*

# Build output
dist/
build/
*.log

# IDE
.vscode/
.idea/
.DS_Store

# Java
target/
*.class

# Python
__pycache__/
*.pyc
venv/
```

## GitHub workflow (PRs)

```bash
git switch -c feature/x
# ...edit, add, commit...
git push -u origin feature/x
```

Then on GitHub: open a Pull Request, request review, merge when approved.

## GitHub Classroom (this course)

1. Click the assignment link from the course page.
2. Accept the invite — GitHub creates a private repo for you.
3. Clone it: `git clone <your-repo-url>`
4. Edit, commit, **push**.
5. The autograder runs on every push. Check the **Actions** tab for results.
6. Final score auto-imports to Moodle at the deadline.

## Conflicts

When you `git pull` or `git merge` and Git says CONFLICT:

```bash
# Open conflicted files — look for:
<<<<<<< HEAD
your changes
=======
their changes
>>>>>>> branch

# Edit to resolve (delete markers), then:
git add file.html
git commit                          # finishes the merge
```

## Useful aliases

```bash
git config --global alias.st  status
git config --global alias.co  checkout
git config --global alias.br  branch
git config --global alias.lg  "log --oneline --graph --all"
```

## Tools & references

- Pro Git book (free): <https://git-scm.com/book>
- GitHub Docs: <https://docs.github.com/>
- Visualise Git: <https://learngitbranching.js.org/>
- `gh` CLI: <https://cli.github.com/>
