Let’s Git Started: A Beginner’s Guide to Git & Github

No clue how to even approach Git or Github? Kim Whitney shows you all you need to ‘git’ going.

SheCanCode
SheCanCode
10 min readDec 11, 2017

--

Are you new to the tech scene, or unfamiliar with some parts, and feel a little lost when it comes to hearing about the mysterious “Git” or “Github”? Do you think you know what you’re doing but have no clue what the commands you type actually do? Do you want to start a coding project and are unsure how to store or version your code? Then hopefully this tutorial can help! Below, I’m going over all the basics — from all the tiny steps involved for set up, to how git works, and commands you need to know!

What is Git?

Git is a distributed version control system used to share a common codebase among many users and/or track different versions you change and make along the way. Have you ever been working on a paper, saved the file as “RoughDraft1”, made a few changes, saved it as “RoughDraft2”, and eventually save it as “RoughDraft24FinallyDone” — just in case you want to go back to a previous draft you were happy with? Well Git allows you to do just that!

Image: University of Washington Computer Science & Engineering community.

What is GitHub?

Git is the tool that allows you to track and version your code and Github is a service that was created for easier use and visualization of your Git repositories. Github allows you to better manage user accounts for collaboration on repositories, review and request changes, open source projects, host a website for free, and more.

So how do I get started?

First, you have to install it! I recommend reading this website for instructions on how to install it for your platform (MacOS, Windows, or Linux). Since I have a Macbook Pro, I’ll repeat the mac instructions here.

  1. Download the installer for Git on macs — the most recent version is probably best.
  2. Click the downloaded file and open up the package.

If your Mac says your security preferences don’t allow you to open a package from an unidentified developer, go to System Preferences -> Security & Privacy -> General Tab -> click “Open Anyway”.

3. Follow the prompts to install it.

4. Open up your terminal (if you don’t know what terminal is, check the links in the common vocab section above!) and type “git — version”. If you get a response back that looks like this:

Then it’s successfully installed!

5. You want to tell your newly installed Git who you are, so set your username and email by running these commands:

git config — global user.name “Kim Whitney”
git config — global user.email “kim@example.com

But with your name and email instead, of course.

CREATE A GITHUB ACCOUNT

If you don’t have a Github account already, let’s create one! Go to github.com and click Sign Up. If you’re a student, you can use the Student Developer Pack to be able to make private repositories for free — but for now, the free version of Github is just fine.

OPTIONAL — Set up your SSH Keys

SSH (“secure shell”) keys are a secure protocol that can allow users to access remote data — like, for example, code on a remote repository. Setting up your SSH Keys will allow you to publish changes to your code without having to provide your username and password.

How to Set Up Your SSH Keys

  1. Go into terminal and type ‘cat ~/.ssh/id_rsa.pub’ this is checking the contents of the ‘pub‘lic SSH Key in your SSH folder. If you get a “No such file or directory” error — that means you have to create one!
  2. To create an SSH Key, run ‘ssh-keygen -t rsa’ which means, generate an SSH Key of security ‘-t‘ype RSA. Hit enter for every prompt.
  3. Once you have an SSH Key, copy the output of “cat ~/.ssh/id_rsa.pub” — it should start with “ssh-rsa” and end with an email address.
  4. Go to https://github.com/settings/keys, click “New SSH Key”, type in a title — for example “SSH Key for my personal laptop”, then paste the key you just copied in the content box, and press “Add SSH Key”.
  5. Done!

Now that we have Git and GitHub set up, let’s start a project.

You have three different options on how to start a project.

  1. You can “Fork” someone else’s — which means to branch off of a version of an open source repository on Github and make your own changes on top of it.
  2. You can take a directory you already have locally and turn it into a git repository — so cases where you’ve already started a project and now want to start tracking it in git.
  3. You can clone a repository that you’re collaborating on or already have from github to work on locally.

So choose one, scroll down to the section header of the one you choose, and let’s get to it!

1 — Forking A Repository

Forking a repository allows you to freely experiment with changes without affecting the original project. This is useful if you want to use someone’s open source code as a starting point for your project, if you want to add something to an open source project, or you want to help contribute to one! For this beginner tutorial, we’re going to go with forking a repository for a starting point.

GitHub has an example repository made just for testing how to fork something!

In the top right corner, press “Fork”. GitHub will take a second to create the Fork, then you’ll notice you’ll have your own copy of the repo as so:

Next, click “Clone or Download” and copy the URL there (if you have SSH Keys set up, copy the SSH url. If not, click “Use HTTPS” and copy that one instead.)

Now, open up your terminal and navigate to where you want to be putting the files from this repository. Type ‘git clone <URL>’ where <URL> is the URL you just copied and press enter. The repository should then be cloned!

2 — Putting code you already have locally into GitHub

First, navigate into the project folder in your terminal. Type ‘git init’.

Go to Github.com and in the top right corner, hit the “+” button and click “New Repository”

Fill out the information for creating a new repository. Since you already have code in your repository, I recommend not initializing the project with a README, .gitignore, or license. A README is a markdown formatted file typically explaining the project and how to set it up. The .gitignore file tells git which files to not worry about tracking in git (like compiled versions or node modules), and the License is if your project has an open source license.

Click “Create Repository”. Then, follow the second subset of commands from your terminal prompt.

1. Add the remote URL to your local git repository

git remote add origin git@github.com:kimception/myProject.gitv

2. Type git status to see what files are there to be added to the repository. So far, everything should be shown as “untracked” since you haven’t added anything to be tracked yet!

3. Add all the files at once (by using “.” it means everything in that place).

git add .

4. Commit your changes (of adding these files). What you write after the “-m” is the message you’re attaching to the commit. Make it something informative in case you need to go back to it!

git commit -m “initial commit”

5. Push your changes to the remote branch on the master (aka main) branch

git push -u origin master

Now check your repository on github.com and see all your files there!

3- Cloning a repository thats already made

Whether you’re planning on working on someone else’s project with them (make sure you’re added as a collaborator!), you already have a project on Github, or if you just made a new repository on Github yourself, cloning a repository is simple.

Find the repository on Github, click “Clone or Download”. Copy the URL shown there (if you have SSH Keys setup, copy the SSH url. If not, click “Use HTTPS” and copy that one instead.)

Navigate to where you want to store your code in your terminal and type ‘git clone <URL>’ where <URL> is the URL you just copied. The code should now be cloned there to work on locally!

Making changes to your local code

Now that you have code on your computer, you can make whatever changes you want to it! This isn’t like Google Drive where changes you make on your computer automatically show up online — you have to tell Git when and what to record as revisions of your code.

So first — change a few things in the code. Add or delete a file, add a line to one of your files — whatever you want. Then type ‘git status’.

git status tells us a lot. Here, we see that I’m on the “master” branch (we’ll cover branches another time). We have modified our “code.js” file. We have made a new file called “newFile.js” that Git doesn’t track yet — so it’s considered untracked.

Image: nullptr

If you add one of the files you changed ‘git add <filename>’, it gets moved to a “staged” area — meaning it’s staged to be committed as part of a new revision.

Try adding all the files you modified by running git add <the file name>. Once all your changes are “staged”, you can save them as a revision by “committing” them.

Type ‘git commit -m “my commit message here”’. If you’re familiar with the vim editor at all, you can just type ‘git commit then be brought to the editor to type your commit message and save. Make sure your commit messages are informative of what changes have been made in that revision — it helps if you need to return to that revision later on!

If you check your repository on Github.com, you’ll notice that none of the changes you just committed are on there! Why is that?

Image: Manvir Basra

It’s because you haven’t ‘pushed’ the changes to your remote repository yet — they’ve just been saved in your working copy.

For the first time you’re pushing, you must tell git which repository you’re pushing to. If you followed my directions above, then “origin” is a keyword for the remote repository. So type:

git push -u origin master

Meaning, push the changes where the ‘-u’pstream remote is ‘origin’ aka our remote URL and we’re pushing the “master” branch that we’re on!

If this isn’t the first time you’ve made changes, then you can just type ‘git push’ to push your changes.

What if someone else made changes?

If you’re collaborating on a project, or maybe you pushed changes while working on a different computer, you need to make sure your working/local copy of code is the same as the code that’s stored in the remote repository on github.com

To fix this, you “pull” down the changes by running ‘git pull’.

You could have possibly also heard of ‘git fetch’ as a command. If you want your local copy to be aware of the changes made remotely but without updating it, you can run “git fetch”. This command is incredibly useful when you’re working with different branches — which we won’t get into in this tutorial, but maybe a future one!

For now, you should know how to do the basics of creating, setting up, and working on new projects. Feel free to comment below with questions, comments, corrections, tips, your new projects, and more! I’d love to see!

APPENDIX

I’ll leave you with a quick cheat sheet I’ve created of some of the main topics we went over just now. Feel free to come back and reference whenever you need!

Hi friends, I’m Kim! I’m a fifth year (of five!) computer engineering and computer science student at Northeastern University in Boston. I’ve worked four full-time software developer internships (or as we call them at NU, co-ops) so far at EMC, Apple, Starry, and now Turo! I also enjoy art and design, rock climbing, swimming in lakes, and doppler radar — though my passions lately have mainly revolved around diversifying the tech workplace. Please don’t hesitate to send me a message; I’d love to chat!

Follow Kim: LinkedIn | Github

#SheCanCode

--

--

SheCanCode
SheCanCode

SheCanCode’s Community Mission is to work together to help attract, engage and encourage more women to enter, remain, and excel in the tech industry.