Sciware
Intro to GitHub
https://sciware.flatironinstitute.org/21_IntroGithub
https://github.com/flatironinstitute/learn-sciware-dev/tree/master/21_IntroGithub
Rules of Engagement
Goal:
Activities where participants all actively work to foster an environment which encourages participation across experience levels, coding language fluency, technology choices*, and scientific disciplines.
*though sometimes we try to expand your options
Rules of Engagement
- Avoid discussions between a few people on a narrow topic
- Provide time for people who haven’t spoken to speak/ask questions
- Provide time for experts to share wisdom and discuss
- Work together to make discussions accessible to novices
(These will always be a work in progress and will be updated, clarified, or expanded as needed.)
Zoom Specific
Future Sessions
- Tomorrow: GitHub Part 2: collaboration
- June 29: Showcase of code editors, development environments (show & tell)
- July 21 (preliminary): Shells, environments, command-lines
Today’s Agenda
- What is Git and GitHub?
- Setting up git and GitHub on your computer
- Getting code off of GitHub
- Putting code onto GitHub
Intro to Git and GitHub
Version control
- keeps track of the edit history to one or more files
- serves as a backup
- makes it easier to collaborate and combine multiple changes to the same file
an open-source, distributed, command-line, version-control tool
- released in 2005 by Linus Torvalds for Linux kernel (alternative to CVS, svn, ...)
- dominant tool for academic and industry software development
- distributed: no central server, every repo is fully functional, independent, and can "sync" with any other
GitHub
- A central website for storing and sharing git repositories
- Started in 2008 as a freemium service, now owned by Microsoft
- Provides repository management, permissions, collaboration tools, CI, etc.
- Alternatives: gitlab, bitbucket, …
Setting up GitHub on your Computer
Make sure git
is installed
> git version
git version 2.30.1
If this returns an error, please raise your hand or put a yellow sticky on your laptop.
Setting your name in Git
See what name is currently set
> git config --global user.name
Set your full name
> git config --global user.name "Mona Lisa"
Setting your email address
See what email address is currently set
> git config --global user.email
Set an email address
> git config --global user.email "youremail@flatironinstitute.org"
(Ideally set to the same email address you used for GitHub account)
Generate an SSH key
> ssh-keygen -t ed25519
- We’re going to generate a new key (one you hopefully don’t have already)
- It is easiest to leave the passphrase blank
> cat ~/.ssh/id_ed25519.pub
ssh-ed25519 AAA..... user@host
Copy this whole line to the clipboard
Add the SSH key to GitHub
- On GitHub:
- Profile Photo > Settings > SSH and GPG keys > New SSH Key
Add the SSH key to GitHub
Setup Git’s default text editor
So that you don’t get stuck in vi:
> git config --global core.editor "nano -w"
How to set up your favorite editor with Git:
https://git-scm.com/book/en/v2/Appendix-C%3A-Git-Commands-Setup-and-Config#ch_core_editor
Questions?
Getting code from GitHub onto your computer
GitHub Jargon
- Directory containing the code
- repository or repo, for short
- "Download the code"
- Your computer drive
Download the code to your computer in GitHub-ese is
Clone the Repo to your local
Clone the repo
- Go to the repo on the GitHub website
- Click Green Code button
- Choose SSH tab
- Click the clipboard icon to copy the repo path
Clone the repo (continued…)
- In a Terminal window, clone the repo:
> git clone git@github.com:flatironinstitute/sciware21-git-intro
> cd sciware21-git-intro
- A directory will be created containing all of the files in the repo.
- The directory name will be the repo name.
What does git clone
do?
- Using the
git clone
command connects the directory to the repo on GitHub in case you ever wanted to interact with it later.
- It generates hidden directory
.git
> ls -a
- It also saves the URL to the repo and names it origin
> git remote -v
origin git@github.com:flatironinstitute/sciware21-git-intro (fetch)
origin git@github.com:flatironinstitute/sciware21-git-intro (push)
Questions?
Activity
Find a repo and clone it to your computer
https://github.com/explore
Survey
http://bit.ly/sciware-github1-2022
Putting code on GitHub
Make a project directory (folder)
> cd #out of sciware21-git-intro
> mkdir silly_repo
> cd silly_repo
> touch silly_code.py
> touch silly_file.txt
Create a repo on GitHub
- Go to your homepage on GitHub
- Click the Repositories tab
- Click the green New button
- Name the repository silly_repo
Initialize the directory to use with GitHub
> git init
> git status
Name the primary branch main
- It’s possible to have multiple branches of the code where different things are being worked on.
- The primary branch is usually called main.
> git branch -M main
> git status
Notice:
- branch name
silly_file.txt
is in red and is untracked
Specify which files that you want to transfer
Use the git add
command to specify exactly which files you want to transfer to GitHub.
> git status
> git add silly_file.txt
> git status
Notice:
silly_file.txt
is now green
silly_file.txt
needs to be committed
Save the changes
- Use the
git commit
to save the local changes.
- Add a commit message to document the changes.
- Launch a text editor where you can type the commit message:
> git commit
Alternatively, you can commit directly from the command line:
> git commit -m "add silly file"
> git status
What’s in a commit message?
- Like a comment in your code
- Says what you changed and why
Connect the repo to GitHub
- Use
git remote add
to provide the URL to the GitHub repo.
- The repo that is in your personal profile is usually called
origin
> git remote -v
> git remote add origin git@github.com:kelle/silly_repo.git
> git remote -v
Upload the repo contents to GitHub
- Use the
git push
command to upload the committed changes to the GitHub repo.
> git push origin main
Check GitHub
silly_file.txt
should now be in the repo on the GitHub website.
Questions?
Activity
- Push
silly_code.py
to the repo
- Put one of your projects into a new GitHub repo
Troubleshooting
- Find GitHub buddies
- The best way to figure things out is by asking folks for help
Troubleshooting
- Avoid problems by keeping track of the state of your local.
- Inspect
git status
before and after every command until you gain confidence
https://medium.com/@kenwarner/command-line-ux-matters-too-improve-your-git-status-colors-170de858953d
Troubleshooting
- There are many resources for common git and GitHub problems on the internet.
- Consider discussing with a buddy before copy/pasting.
Tomorrow
Using GitHub to collaborate
Survey
http://bit.ly/sciware-github1-2022