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
Working with a local repository
Learning to love git (showcase of advanced topics)
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
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:
However: Gists are not meant for collaboration or larger pieces of work!
Tasks
Tasks
Tasks
Bonus tasks
Advanced
gh auth login
gh repo clone klieret/collab-git-playground-codas-hep-24
gh issue create
While you can open issues, you do not have permissions to directly modify content.
Tasks
content
folder and click Add file
> Create new file
<your gh username>_first
and add a few lines to itBonus tasks
Advanced
You can also fork using the gh CLI.
Starting VSCode
in the browser
Tasks
.
(opens in same tab),>
(opens in new tab)github.com
to github.dev
.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.
A full development platform
Tasks
echo 'hello world'
in the terminalsudo apt-get update && sudo apt-get install fortune && /usr/games/fortune
How to bring our changes back to the original repository
Tasks
Bonus tasks
Closes #<number of your issue>
and the PR is merged, the issue will automatically close.@<name>
Someone just merged your pull request!
.git
repository)<your gh username>_second
Create a new branch for this commit and start a pull request
main
branchklieret/...
)main
branchBonus task: Adding additional commits to a PR
draft
status and ask repository owner to review + mergeBonus task: Go crazy!
Commit to various branches, create PRs between your branches or to your neighbors branches.
main
branch (yellow)<your gh username>_first
and commit to the branch (!) merge-conflict
(blue)main
branch. Do not merge the PR yet!main
branch againmain
)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 main
Let’s log in to GitHub (this will store the GITHUB_TOKEN
env var):
gh auth login
and 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 push
Bonus tasks:
-m
option 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 push
Hints:
git add .
or use the -a
option for git commitgit reset <file>
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 present
Bring 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.gitignore
files: 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.
<your repo>/.git/config
. Take a look!~/.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 -a
Alternatively 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.
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.