In this tutorial, you will learn how to:
Note: While this tutorial makes use of GitHub, it should be reasonably straightforward to adapt its contents to an alternative hosting provider, like GitLab.
Following instructions from: docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account
GitHub (and most Git hosting providers) support two types of connection:
HTTPS
, with authentication based on username and
passwordSSH
, with authentication based on a private
key you keep on your machine, and a public key that you
share with GitHubWe’re going to use SSH
, which is generally the preferred
option and is simpler when your account has Two-Factor Authentication
enabled.
To do so, whenever we want to use Git on a new machine, we’ll need to:
To generate a new SSH keypair, open a terminal and run:
ssh-keygen -t ed25519 -C "your.email@example.com"
ed25519
, use:
ssh-keygen -t rsa -b 4096 -C "your.email@example.com"
This will create two files in an .ssh
directory under
your home directory, which we can list with:
ls -l ~/.ssh
You should see:
id_ed25519
(or similar)id_ed25519.pub
(or similar)github.com
in your web browserSettings
SSH and GPG keys
, select
New SSH key
work-laptop
Authentication Key
cat ~/.ssh/id_ed25519.pub
Let’s create a repository on GitHub to publicly share the blog we made last week:
github.com
in your web browser+
dropdown at the top-right, select
New Repository
blog
Public
Your new repository will be completely empty, with no commits.
Now let’s connect your local blog
repo to this remote
repo on GitHub:
Quick setup
instructionsSSH
tab, and copy the URLcd blog
)git remote add origin git@github.com:<username>/blog.git
IMPORTANT: If the network you are on does not allow traffic over SSH port 22, you will need to use a different URL format:
git remote add origin ssh://git@ssh.github.com:443/<username>/blog.git
To check our remote has been added successfully, run:
git remote -v
origin
is the conventional name for the primary
remote.Now let’s push the current state of the local repository
(specifically, the main
branch we have been working on) to
the origin
remote:
git push -u origin main
Go back to your repo on GitHub and you should see your blog files!
Now try cloning your neighbour’s repo:
Code
dropdown, and copy
the SSH URLfriend-blog
by running:git clone git@github.com:<friend-username>/blog.git friend-blog
Again, if the network you are on does not allow traffic over SSH port 22, you will need to use a different URL format:
git clone ssh://git@ssh.github.com:443/<username>/blog.git friend-blog
Have a look at their blog posts!
cd friend-blog
ls -l
cat entry_1.md
We should add a README file to every repo to provide a starting point for anyone looking at it (including our future selves!).
Let’s each add a README to our own repo, then fetch the README that our neighbour added into our local clone of their repo.
First, go back to the terminal opened to your own blog’s directory,
and create a README.md
file:
nano README.md
Give it the following content’s
# My Blog
## Contents
* [Entry 1](entry_1.md)
Save and exit nano
(Ctrl + x
,
y
, Enter
), then add and commit the README:
git status
git add README.md
git status
git commit
# Commit message: Add README
git status
git status
, you should see that your
branch is “ahead of origin/main by 1 commit”origin/main
is a remote reference that
tells us what the main
branch looked like on the
origin
remote the last time we cloned or “fetched” from
that remote.origin/main
does not yet have our latest commit:git log
To publish the latest commits from the local copy of our branch to the remote, we just need to run:
git push
Now check the status and log again, and see that
origin/main
has been updated:
git status
git log
Finally, refresh your repo’s page on GitHub, and see that you have a fancy-looking README file!
Now go back to the terminal open in your friend-blog/
directory, and fetch the latest commits:
git fetch
Now git status
tells us that our local repo is
behind the remote by 1 commit:
git status
We can also see this from the log:
git log origin/main
git log
defaults to showing us commits on our
current local branch, we need to tell it to look at commits on the
remote branchBecause we haven’t made any commits of our own since we last cloned/fetched, we can simply tell Git to update our local branch by “fast-forwarding” through all of the commits on the remote branch:
git merge --ff-only origin/main
--ff-only
tells Git to only update our local branch if
it is possible to “fast-forward”.git merge
in more detail in the next lessons.git pull
, which performs both a fetch
and
merge
from one command
pull
until you fully
understand how merges work, and the implications of the different ways
the pull
command can be configured.Sometimes we have files that we don’t want Git to track, especially when sharing repos with others:
For example, let’s say we have a file containing some secrets:
touch secrets.txt
Git is ready for us to start tracking it:
git status
But we will tell Git to never track that file by adding its path to a
.gitignore
file:
nano .gitignore
Add the following line into .gitignore
:
secrets.txt
Now Git will ignore secrets.txt
, though we will need to
make sure we commit the .gitignore
file:
git status
Finally, let’s use GitHub’s Pages feature to publish your blog as a static website!
Settings -> Pages
Deploy from a branch
, select the
main
branch, then Save
https://<username>.github.io/blog
You can make a pretty fancy blog or documentation website on GitHub pages with a static site tool like Jekyll, or by crafting your own HTML and CSS.