Git Init

Melissa Gonzalez
Adventures in Code
Published in
5 min readJul 27, 2017

A Beginner’s Guide to Setting Up Git & GitHub

One of the first things we did on the first day of Bootcamp was to set up a GitHub account and learn how to use Git. And with good reason! Git and GitHub are two very fundamental and crucial tools in a web developer’s arsenal.

We set up Git and GitHub from Day 1 so that we build the habits of committing code and pushing it online.

Git is a widely-used open source version control system. It is a command-line tool that developers use to back up their code and save their revisions on a regular basis. It’s particularly useful when working with complicated code, so you can see step-by-step what the various edits and additions to the code are. Git by itself is a local software that only saves things on your own computer.

On the other hand, GitHub is a website that developers use to store their code online. This allows others to view and use the code, and it also serves as an online back-up. GitHub is also a way for multiple people to work on the same project without interfering with each others’ codes.

So far in class, we’ve only worked on small solo projects, so we’ve only used Git and GitHub with one branch (the master branch). But since it’s a tool we use in almost every class, I thought it’d be a good time to post about how we’ve used Git and GitHub in class so far!

Git

Installation & Configuration

In order to use Git, you must first install it on your computer, then configure it. My computer already came with Git installed, but you can also download it from online. GitHub has some nice set-up instructions! Once you have it downloaded and installed, use the following commands in your terminal to configure it:

To configure git, enter these commands in the terminal:

git config --global color.ui truegit config --global user.name “YOUR NAME”git config --global user.email “YOUR@EMAIL.com”

To see if these commands worked, run: git config --list .

Version Control

To use Git, first create a new repository for each new project. A repository is simply a folder or directory that will get managed by Git. Every project should have its own repository so that your files stay organized. To turn the folder into a Git repository, navigate to the correct folder in your terminal, then use the command: git init

Git is a much better way of saving pervious versions of you code than making a manual back-up!

Once your folder becomes a Git repository, Git will keep track of any changes you make in that folder. You only have to turn the folder into a repository once per project, so you only have to perform the git init command at the start of the project.

Every time you make a make a meaningful change to your code, you’ll want to commit your code. This means that you will tell Git to save that version of your code. Each Git commit should be labeled with what the change was.

In that sense, you should only commit your code once you’re done accomplishing a particular task (ie don’t commit your code if you’re only halfway done with adding a feature, or if it’s not working quite yet). However, you don’t want to make too many changes to your code before committing, either.

In order to commit your code, run the following commands:

git add --all 
# Adds all the changes you’ve made to a “staging area”
git commit -m “Commit message”
# Saves all the changes in the staging area. The commit message should describe what the change was.

Other Useful Git Commands

Apart from git init , git add --all, and git commit -m “Message", here are a few other commands we’ve been using in class:

git status
# shows any new files or files that have been changed that haven't been committed.
git add <filename>
# add only one filename to the "staging area"
git push origin master
# pushes code online to GitHub, which I'll cover shortly!

That’s all we’ve done so far with Git (though to be fair, we’ve only been in class for 4 weeks!) I’ll provide an update in a future blog post once we learn more complicated functions!

GitHub

Setting Up a GitHub Account

Visit github.com and choose a unique username and password. Since this will be an online portfolio of you work, you’ll probably want a professional-sounding username. However, if you already have an online presence with an established username (such as on Twitter, etc), then feel free to use that.

Once you’ve connected Git to GitHub, your puzzle pieces will be in place to start backing up and sharing your code online!

Connecting Git to GitHub

In order to connect Git to GitHub, you need to send your SSH key to GitHub so that you don’t have to enter your password every time you want to push it online.

This is covered on the GitHub website, but for the sake of learning/ remembering what we did, I’ve listed here what we did in class. (Note that I’m still learning the ropes, so make if you’re looking for a definitive guide, you may want to refer to the GitHub link!)

  1. Run this command in your terminal: rm -rf ~/.ssh
    NOTE: This step removes any SSH keys already installed on your computer. Please be sure to copy this code carefully! One student in class accidentally put a space between the ~ and the / in the code, and he accidentally deleted the contents of his computer. Don’t do that.
  2. Run this command AS ONE LINE in your terminal (replace the email with the email associated with your GitHub account):
    ssh-keygen -t rsa -b 4096 -C “your-GitHub-email@example.com”
  3. You will see this message:
    Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
  4. Press Enter.
  5. You will see this message: Enter passphrase (empty for no passphrase):
  6. Enter a password and REMEMBER IT!
    Note that you won’t be able to see the password as you type, so don’t mess it up.
  7. You will see this message: Enter same passphrase again:
  8. Enter the password again. KEEP REMEMBERING IT!
  9. You’ll see something like this:
    Your identification has been saved in /Users/you/.ssh/id_rsa.
    # Your public key has been saved in /Users/you/.ssh/id_rsa.pub.
    # The key fingerprint is:
    # 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com
  10. Run this in your terminal: eval “$(ssh-agent -s)”
  11. You should see something like this: Agent pid 59566
  12. Run this in your terminal: ssh-add ~/.ssh/id_rsa
  13. Run this in your terminal: pbcopy < ~/.ssh/id_rsa.pub
    (Linux users: cat < ~/.ssh/id_rsa.pub and copy directly from screen)
  14. Follow the instructions in Step 2–8 here: https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/
  15. Follow Step 1–4 from the same article: https://help.github.com/articles/testing-your-ssh-connection/

Using Git to Push Code Onto GitHub

Once you have your Git and GitHub set up and talking to each other, you can finally push your code online! I’ve cover this in a separate blog post!

--

--

Melissa Gonzalez
Adventures in Code

Aspiring Web Developer. Fitness Enthusiast. Foodie. Beer Lover. Triathlete. Former Research Scientist.