Github Basics Tutorial
Github Basics Tutorial
This is a basics tutorial that will go over how to set up Github locally so you can push/pull with your SSH key.
Objective
Once set up, you should be able to directly add/commit and push changes to your repo using the following code.
1
2
3
git add .
git commit -m "my-second-commit"
git push
Prerequisite
Make sure you have git installed:
1
https://git-scm.com/
Setup SSH Key
In order this up locally, you will need the local SSH key associated with your account. If you haven’t set one up yet then do the following. See this guide on using ssh with Github for details.
- Run ssh-keygen, to create your key in
~/.ssh/id_ed25519
.1
ssh-keygen -t ed25519 -C "your_email@example.com"
- Next run ssh eval:
1
eval "$(ssh-agent -s)"
- (Mac only) For mac users you will need to do this extra step. Skip if you are not on mac.
1 2
touch ~/.ssh/config nano ~/.ssh/config
Then copy the following to the ~/.ssh/config file.
1 2 3 4
Host * AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_ed25519
- Run ssh-add. For Mac include –apple-use-keychain:
1
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
For Linux and Windows, simply:
1
ssh-add ~/.ssh/id_ed25519
- Now copy the contents of the
~/.ssh/id_ed25519.pub
file. Should look something like this:1
ssh-ed25519 AFSJOIAUWEREIJRIJ...your_email@example
Go to your Github account under https://github.com/settings/keys > SSH Keys, click “New SSH Key”. Paste in the key you just copied. Then give it a title (usually the name of your computer that holds the key).
- Use the following command to test that you have access.
1
ssh -T git@github.com
- The result should say:
1
Hi ArcticTechnology! You've successfully authenticated, but GitHub does not provide shell access.
- The result should say:
Setup Globals
This part is optional, but it allows you to default your username and email:
1
2
git config --global user.name "ArcticTechnology"
git config --global user.email 13512073+ArcticTechnology@users.noreply.github.com
Note: If you are using email privacy restrictions (which is highly recommended) you need to use this private email which Github provides: {ID}+{username}@users.noreply.github.com
. See this post on email privacy for details.
Pull Your Existing Github Repo Locally
The following are instructions to pull an existing Github Repo locally.
- Create a new repository locally:
1
git init
- Add origin:
1
git remote add origin git@github.com:ArcticTechnology/sandbox.git
- Pull the existing repo:
1
git pull origin main
- Rename your branch main from master as Github now defaults to main.
1
git branch -M main
- Push changes and set upstream main
1
git push -u origin main
Once you successfully set upstream main. You should be all ready to make your commits.
Push Up a Brand New Repo
The following are instructions to push up a new repo.
- Create a new repository locally:
1
git init
- Add origin:
1
git remote add origin git@github.com:ArcticTechnology/sandbox.git
- Rename your branch main from master as Github now defaults to main.
1
git branch -M main
- Make your first commit.
1 2
git add . git commit -m "first commit"
- Push changes and set upstream main
1
git push --set-upstream origin main
Once you successfully set upstream main. You should be all ready to make your commits.
Changing the Name of the Repo
When you change the name of your repo, be sure to update the remote origin with the following:
- View existing remote origin.
1
git remote -v
- Update the remote origin.
1
git remote set-url origin git@github.com:ArcticTechnology/sandbox_updated.git
Note: If you have multiple repositories with multiple SSH keys, remember to identify which key you are using. For instance, if your key is
github.com-personal
then you should set your remote origin to:1
git remote set-url origin git@github.com-personal:ArcticTechnology/sandbox_updated.git
Conclusion
Now your Github repo is set up. You can use the following to commit new changes:
1
2
3
git add .
git commit -m "my commit message"
git push