Github Command Line Setup and Flow
Whoo boy, this is probably going to get a lot of attention.
::: spoiler Background and Purpose I'm not an expert at Github or using git from the terminal. As a matter of fact, I've found it very confusing. So, I've written my own guide to sort of implant the process in my head and help out other dumb dumbs like me. Feel free to leave your commentary and suggestions in the comments. Most of the process of this guide can be found online, but Reddit sucks now and the guides on Github aren't all in one synthesized, cohesive guide for set up, at least not that I could find. Relevant links are listed in most steps.
::: spoiler Changelog Version 1.01 of this guide. V1.0: initial post V1.1: made more explicit the fact that you can change the SSH key name in step 1. Added additional commentary in step 3. Provided a general, brief overview of the process. Added additional instructions in step 3. :::
Usage guide: Each "step" is a stage in the process. Spoilers labelled "Commentary" contain additional information and author commentary. Spoilers labelled "Instructions" contain the instructions relevant to the step.
Overview: Create an SSH key on the machine you're using, then add it to your machine's agent. Following that, paste the .pub key into your github account, and then log into gh cli from the terminal to authorize git pushes to the registered account.
Step -1: Create a github account.
- Navigate to Github.com and create an account with an email of your choice.
Step 0: Install Github CLI.
::: spoiler Commentary
-
Github's CLI software allows you to make a Github repo from the terminal. This link takes you to the installation page for Debian/Ubunutu linux distrubutions. The page has a sidebar with other OS installation instructions. There's a bunch of terminal code there to copy and paste into your own terminal, all that's needed is sudo permissions. :::
-
Follow this link for detailed installation instructions.
Step 1: Generating an SSH key
::: spoiler Commentary SSH stands for Secure Shell and, in brief, is what computers use to talk to each other. It relies on keys to work. In order to push to Github from the terminal/create repositories, you'll need an SSH key to connect your local machine to Github's servers and your github account. :::
- Following is a summary of this link: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent.
::: spoiler Instructions
-
First, paste the following command into your terminal and replace the example email to whatever email you signed up with Github.
ssh-keygen -t ed25519 -C "[email protected]". The second argument in the command (ed25519) is the encryption algorithm. If you have a different one you want to use, replace that with the algorithm you want (not advised). NOTE: DO NOT JUST HIT ENTER, if you wish to change the name see the next bullet point. -
The followup prompt asks where you'd like to save the key. The stuff inside the parenthesis is the default location AND the default name. Hit enter to save it XOR type in a directory and file name to save the key under/in. You can use the same default directory and just change the name if you'd like.
-
The next followup prompt asks for a password. Give it one. On terminal, you won't see anything when you start typing the password; this is normal. If you mess up, just hold backspace for a while and then start typing again.
-
Next step is add the SSH key to your SSH agent. Prime the ssh agent in the background by putting
eval "$(ssh-agent -s)"into the terminal. Then, just typessh-add ~/.ssh/name-of-your-keyinto your terminal, replacing name-of-your-key with the name of your key established in the previous steps. At this point, your key is generated and ready for use. There is another step on the website, but I don't know if it's needed or useful; I didn't use it so whatever. :::
Step 2: Add your SSH key to your github account.
- Following is a summary of this link: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account
::: spoiler Instructions
-
Open the .pub document in the folder you saved your key to, and copy the text. For CLI: type
cat ~/.ssh/name-of-your-key.pubinto your terminal, replacing name-of-your-key with the name of your key. This will output a bunch of text into your terminal, copy that text. If you saved the key to a different folder, replace~/.sshwith the folder's directory. -
On github, navigate to your account's settings page, and find the section titled "SSH and GPG keys." Under the section titled SSH keys, click "New SSH key," give it a title, and paste your key into the textbox.
-
Note: This process can be done via the terminal. The instructions will not be detailed here, but use
gh auth loginand follow the SSH path. :::
Step 3: Authorize your account and make primary email
::: spoiler Commentary
- You'll want to authenticate git with your github credentials. I'm not sure if this is strictly necessary, but if you want to create a repo on github from your terminal, I imagine this is required. I also imagine this is required anyways but I'm not an expert.
- After this stage, you should be good to go for anything else. For example, if you're doing this process to get a new machine connected to your github, I think here is generally up to what you need :::
::: spoiler Instructions
- In terminal, type
gh auth login, and follow the command prompts. When it asks for SSH versus HTTPS, select HTTPS, use ctrl-shift-c to copy the code given, then hit enter to open a link to github's login page in your browser. Select the account you want to use, then paste the code. If you get the terminal output "Logged in as account-name" where account-name is your account name, then it worked. - If git doesn't have an email/user name to associate with you, it'll throw an error telling you what to do. Basically, run
git config --global user.email "[email protected]"replacing [email protected] with your email, and then run 'git config --global user.name "Your Name"' replacing Your Name with whatever name you'd like. :::
Step 4: Make a repository.
::: spoiler Commentary
-
Now, you can make Github repositories from your terminal. Use the command
gh repo createwith no arguments to start the process. From here, you can manually create a new repo on Github, either from scratch or from a template repository. However, if you've started a project on your machine locally, and want to push it as a repo to Github, you'll have to turn it into a repo. This entire step follows that process. The directions though, are more or less straight from thegh repo createline following the "Push an existing local repository to github.com" path. You just need to do an extra step is all. ::: -
Following is (sort of) a summary of this link: https://cli.github.com/manual/gh_repo_create, as well as the directions from
gh repo create.
::: spoiler Instructions
- Before you can take a folder from your machine and turn it into a repo on github, you'll need to turn it into a repo. Open the folder in your terminal, or navigate there manually from the terminal using
cd. Then, typegit initand press enter. - Once repo-ified, use
gh repo create. The followup asks for the path; if you're not already in the current directory in your terminal for the repo, paste the file address for the repo. Otherwise, type a period and proceed from there. Give the repo a name, possibly a description, make it public or private, and add a remote. - Add a remote. Do it. Otherwise, you'll have trouble later actually pushing to the dang thing from your terminal. Learn from my mistakes. :::
Step 5: Git flow.
::: spoiler Commentary
-
"Pushing" means taking all the changes to a repo made on your machine (local changes) and putting them in a remote repository. Essentially, it's syncing the remote location of the repository with the local repository, with the remote repository inheriting changes from the local. The git program lets you push files and changes to files to github. Very nifty program, but very confusing, especially when merge conflicts start to crop up. This guide does not deal with merge conflicts because I do not know how to handle merge conflicts properly and they scare me. This step is more or less an overview of the flow to pushing changes to github. :::
-
Following is a self-written guide with no references to the internet, just what I've been taught previously. Guides for this process surely exist.
::: spoiler Instructions
-
First, all the changes to the local repository need to be staged, essentially gathered together. From the repo's local directory, use
git add .to stage all changes made to the repository. If there's specific files you want to stage, replace the period with the name of the file. -
Once added, the changes will need to be committed. This essentially is just preparing the changes made to be sent to the remote repo. Use the command
git commit -mfollowed by a message to tag the commit with. The commit must have a message. If you don't include -m in the command, you'll be sent to the terminal file editor; I don't know how to use it because it scares me. If your message uses spaces, enclose the message in quotation marks. -
Once committed, you're ready to push. Use
git pushto send the changes up. For the first push, you'll need to set up the upstream branch first, which can be done in the first push to the repository. What's an upstream branch you ask? I have no idea. Use the commandgit push --set-upstream origin masterto push the changes. If you try togit pushwithout doing this first, it'll tell you to do it first. -
Now, if you make changes to the local repo, you can follow this entire step to add, commit, and then push the changes. If someone else makes changes to the remote repo, you can sync your local machine with those changes by doing
git pullin the repo. -
If you want to copy an entire repo from Github to your machine without already having it on there, use
git clone https://github.com/repo-name-goes-herewhere repo-name-goes-here is the name of the repo. I don't believe you need to set up git authentication for this, but for pushing changes you likely will. :::
Step i: No set remote.
- If you made the same mistake as me and did not give a remote, you will have troubles. I did. This link helps you out with fixing it.