| theme | default |
|---|---|
| download | true |
Princeton University
Markdown-based slides available as open source; contributions welcome!
Please open these slides on your laptop as we'll jump back and forth between the slides and the browser!
-
Collaboration in the browser
- GitHub Gists
- Creating issues
- Forking a repository
- Creating commits
- Opening pull requests
- Handling merge conflicts
-
Working with a local repository
-
Learning to love git (showcase of advanced topics)
the easiest way to share code on GitHub
The "gist" interface works more like a pastebin (and is very easy to use!)
Use case:
- Quick sharing of simple code snippets
- Has version control
- People can comment
- Embed code snippets in websites that don't natively support it properly
However: Gists are not meant for collaboration or larger pieces of work!
Tasks
- Go to github.com and click the "+" symbol and select "Gist" or directly go to gist.github.com
- Create a simple file with life changing advice and publish it
- If you feel confident about your content, share your URL on Slack!
Tasks
- Navigate to https://github.com/klieret/collab-git-playground-codas-hep-24 ("playground repository")
- Find up to 4 (!) hidden tigers 🐅!
- Be the first to post a screenshot of your finding to Slack!
Tasks
- Please navigate to the playground repository
- Open an issue with a fun feature request
::right::
Bonus tasks
- Edit the title & description of your issue
- Add a comment mentioning another participant
- Use an emoji reaction
- Close your issue
- Check for other issues and comment there
Advanced
- Install the gh command line tool
- Authenticate with
gh auth login - Clone:
gh repo clone klieret/collab-git-playground-codas-hep-24 - Open an issue
gh issue create
While you can open issues, you do not have permissions to directly modify content.
Tasks
- Click the fork button. This will create a "copy" of the whole repository
- Open the
contentfolder and clickAdd file>Create new file - Call your file
<your gh username>_firstand add a few lines to it - Add a commit message and commit
- Confirm that you see your file & a new commit
Usually you always want to commit to a separate branch in this scenario (later!)
::right::
Bonus tasks
- Add a second file
- Open your previous file and make changes to the text
Advanced
You can also fork using the gh CLI.
- Every node is a commit. Every commit points to a parent.
- Your fork "branched off" of the original repository: You're adding additional commits to a parallel reality
- Next step: Bringing your commits back to the original repository
Starting VSCode in the browser
Tasks
- Navigate to the playground repository
- Do one of these:
- Press
.(opens in same tab), - Press
>(opens in new tab) - change the URL from
github.comtogithub.dev.
- Press
- Make some additional changes
- Commit by clicking on the git tab in the left menu, adding a message and pressing 'commit & push'
Change back to the previous view by changing from github.dev back to github.com.
Pro-tip: You can also do that in a PR to quickly append commits there.
::right::
A full development platform
Tasks
- Go back to the repository and open GitHub codespaces:
- Type
echo 'hello world'in the terminal - Install stuff:
sudo apt-get update && sudo apt-get install fortune && /usr/games/fortune
How to bring our changes back to the original repository
Tasks
- If you create new commit on a fork, github will already offer you a button to open the PR. Click it!
Bonus tasks
- Mention one of your issues. If you write
Closes #<number of your issue>and the PR is merged, the issue will automatically close. - Check the differences that the PR will create
- Comment under one of the differences
- Mention another participant
@<name>
Someone just merged your pull request!
- If we want to start another PR, we do not need to fork again.
- This time however, we first create another branch in our fork.
- Use cases:
- Working on several independent experimental features
- Not all of your PRs might be merged!
- Branches are cheap and flexible, always use them!
- A fork copies the entire repository:
- Similar to copying the entire local project folder (including your
.gitrepository) - If the original repository is deleted, your fork persists
- You own your fork and have every permission there
- Similar to copying the entire local project folder (including your
- A branch belongs to its repository and only tracks certain changes
- Branches are cheap and easy, forks are expensive
- If you have write permissions for a repository you do not usually need/want to fork it
- If you need to fork, fork once and then use branches
- If you have write permissions for a repository you do not usually need/want to fork it
- Add another file
<your gh username>_second - Select
Create a new branch for this commit and start a pull request - Give your branch a reasonable name (whitespace discouraged)
- Commit!
- Create another PR to either:
- Your neighbor's
mainbranch - The original repository (
klieret/...) - Your own
mainbranch
- Your neighbor's
- If you want to do the bonus exercises, mark your PR as
draft - If you receive a PR, merge it (unless it's a draft)
::right::
Bonus task: Adding additional commits to a PR
- Go back to the default view of your repository and verify that you now have multiple branches
- Select your new branch
- Modify your just created file and create a new commit on the same branch
- Check that your PR has been updated by this new commit
- Remove
draftstatus and ask repository owner to review + merge
Bonus task: Go crazy!
Commit to various branches, create PRs between your branches or to your neighbors branches.
::right::
Please follow these instructions precisely!
- Go to your fork
- Verify that you are on the
mainbranch (yellow) - Change something in
<your gh username>_firstand commit to the branch (!)merge-conflict(blue) - Open a pull request to your own
mainbranch. Do not merge the PR yet! - Change to your
mainbranch again
::right::
- Change the same (!) line to something different and commit (to
main) - Check back on your PR, it should warn you about a conflict
- Resolve the conflict by determining how both changes should be reconciled
- Commit the merge
Bonus tasks: Verify that if you change different lines with unchanged lines between them, git will do the merge automatically.
Configure name, email and editor
If you run git for the first time,
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
# Choose your favorite editor, e.g., nano or vim
git config --global core.editor nano
# Requires git 2.28
git config --global init.defaultBranch mainLet's log in to GitHub (this will store the GITHUB_TOKEN env var):
gh auth loginand follow the instructions.
Please raise your hand if you have any issues!
cd collaborative-programming-github
cd content
ls
# Get changes that were done on the remote, just in case
git pull
# show status of git repository
git status
# Create new file
touch <your gh handle>-third.txt
# Status is dirty now
git status
# Commit file
git commit <your gh handle>-third.txt -a -m "My third file"
# Clean again
git status
# View past commits (quit with q)
git log
# Push to the remote
git pushBonus tasks:
- Create a few more commits (changing the file)
- Commit without the
-moption and enter your commit message manually
# change all three of your files
git status
# multiple files should now show "unstaged changes"
git add <your gh handle>-first.txt <your gh handle>-second.txt
git status
# two files "staged"
# Commit. Careful: Do not use the -a option
git commit -m "Committing changes to two files"
git status
# one file still showing unstaged changes
git add <your gh handle>-third.txt
git commit -m "Commit to one file"
# Bring changes to github again
git pushHints:
- If you want to add everything to the stage:
git add .or use the-aoption for git commit - If you want to remove a file from the staging area:
git reset <file> - If you want to unstage all files:
git reset
git branch my-new-branch
git status
# still on branch 'main'
git switch my-new-branch # or: git checkout my-new-branch
git status
# Now use your previous knowledge to create some more commits
git status
# Make sure that everything is committed
git log
# Verify that you have added a view commits
git switch main
# Verify that the changes from the other branch are not present
git log
# Also our commits aren't presentBring the commits from my-new-branch back to main
Advanced: Add more commits to main before merging to set yourself up for a merge conflict
# On branch main
git merge my-new-branch
# Should work directly unless you're doing the advanced exercise Advanced: Manually modify the files to resolve the conflict, then git commit -a.
... but what you should really know about
git show: Show details about a commitgit diff: Show differencesgit stash: Temporarily put changes aside.gitignorefiles: Avoid tracking irrelevant filesgit revert: Revert changesgit checkout: Jump through history (or between branches)- ...
Take a look at a cheat sheet like this one and make sure you understand all commands listed.
- If you are developing software, you almost certainly will use git, no matter where.
- Learning to master git is perhaps THE most transferable skill you can hone.
- Git would not be so dominant if you could not learn to love it.
- All repository specific settings live in
<your repo>/.git/config. Take a look! - You can set global settings in
~/.git/config. Take a look!
Rule of thumb: If you are unsure about the metadata of your repository or about commands that modify it, take a look at your
.git/config.
Defining aliases
# Type `git c` instead of `git commit`
git config --global alias.c commit
git config --global alias.ca commit -aAlternatively you can also directly write into your config file.
To alias g to git:
# put this in your bashrc or similar config
alias g="git"and then some more
This page has very nice suggestions for different levels, all of them using gamification but increasing in realism.
can give you more intuition
Go here for a curated list of them.
Licensing Don't forget to add a license to your repository or nobody can use it! See for example the GitHub docs for more information.
Merging vs squashing vs rebasing There are different ways to bring together different branches. Learn about them and keep your history clean.
GitHub actions You can automatically run unit tests or other automated tasks every time you commit (or perform other actions on GitHub). Take a look at GitHub actions. For simple checks with almost no setup, also take a look at pre-commit and its GitHub integration.
You can also practice by improving these very slides! Go to https://github.com/klieret/collaborative-programming-github. Issues, forks and PRs are very welcome! You only need to speak markdown to help.







