To get you set up with a GitHub account and personal access token. Then to create an empty repository to put your work from the week and get used to using the basic workflow of Git.
- Access tokens
- Github
- Git Repos
- Clone/Commit/Push
If you don't already have an account go to https://github.com and sign up (top right)
GitHub has moved away from username and password as a way of authenticating repo access. Username/Password combos are known to be insecure (how many do you really have, you should probably have 100s) and we will likely see the way we authenticate change across the internet over the next few years.
To generate a token:
- Login to GitHub
- Click on your name/avatar in the top right corner and select Settings
- On the left, click Developer Settings
- Select Personal access tokens, then Tokens (classic) and click Generate new token (classic)
- Give the token a description/name (such as
repo-access
) in the box labelled Note and tick the box for repo scope (you can read more about scopes using the Read more about OAuth scopes link) - Click Generate Token
- Copy the token - this can be used like a password when authenticating via
Git
(you might want to put this in a password manager)
Configure your git client (in the terminal)
This tells the Git client running in the terminal who you are.
On linux, the commands look like:
git config --global user.name "YOUR_GITHUB_USERNAME"
git config --global user.email "YOUR_EMAIL"
git config -l
The git config -l
will show you all the values stored in the config
(this may show an error if viewed in the Notebook Service terminal - you can ignore that).
When you clone a repo, it will ask for your username/password. Enter your username, and access token as the password.
Credential Caching
It is annoying to have to enter your credentials every time you connect to the remote GitHub service (i.e. pushing or pulling content).
Thankfully, you can cache your credentials.
git config --global credential.helper cache
This saves your username/access token in memory (for up to 15 minutes) so you don't need it everytime.
If you need to clear it:
git config --global --unset credential.helper
There are other stores (you can look these up another time):
credential.helper store
- stores credentials on diskcredential.helper osxkeychain
- can be used with MacOS to store credentials in MacOSs keychaincredential.helper "cache --timeout 3600"
- memory cache for up to an hour
Go to GitHub in your browser, make sure you are logged in, and create a new repository.
NOTE: Remember to tick: Initialize this repository with a README
Copy the link to the repository so that you can clone it in your terminal:
In your terminal window, clone the repository you have just created:
- Open the terminal
- Make sure you are in your home directory (you can get there with the command:
cd
) - Clone the repository with:
git clone YOUR_GITHUB_REPO_URL
It will then prompt for username and password (it won't ask again if you have your credentials cached). REMEMBER: In this case, you should give it your Access token (generated above)
You have now cloned the repo. Now we are going to make a change and push it.
cd my-isc-work/
# The `touch` command will create a new file
touch mydata.txt
If you now run ls
you will see two files. README.md
and mydata.txt
.
See what has changed.
git status
Add the changed file to git
git add mydata.txt
(can use git add .
to add all files)
Write a message to say what you have done and make a commit.
git commit -m "Adding an empty file as part of tutorial"
Push the change to GitHub
git push
# or you can do: git push origin main # where origin==GitHub and main==remote branch name
You will now see empty file mydata.txt
exists on the GitHub version of
the repository.
If working on ISC exercises...
# Go to the local copy of your repository
cd ~/my-isc-work
# Make a local "python-intro" directory
mkdir python-intro
# Copy the entire "notebooks" directory from the "ncas-isc" repo to your local repo
cp -r ~/ncas-isc/python-intro/notebooks python-intro/
If working on ISC exercises...
git add python-intro
git commit -m "Added notebooks"
git push