Skip to content

Latest commit

 

History

History
86 lines (67 loc) · 8.21 KB

git-tutorial.mdown

File metadata and controls

86 lines (67 loc) · 8.21 KB

Working with git

by: Erin Joan Snyder

Greetings explorer! Currently you are on Github.com, a place that stores your code in repositories (or repos). Github is different from git, which is a source code management system. Basically, git uses Linux commands to track your work and manage your work in association to the work of your team members.

What if I don't know Linux?

No problem! It's not as complicated as it seems. I mean, I figured it out with absolutely NO prior experience in any STEM fields. I didn't even know what the Terminal was! (Don't freak out if you don't know what the Terminal is, I'll cover that!)

So, what do I do?

Let's start from the very, very beginning. You are currently on Github, and you want to use this HR-Algorithms-Meetup repo.

Forking the Repo

Cloning the Repo

  • Open the Terminal on your computer, navigate to the folder you want to save the repo in. To clone the repo onto your local machine. git clone https://github.com/<YOUR GITHUB USERNAME>/HR-Algorithms-Meetup.git If that sounds like gibberish, it's okay. Let's break it down into smaller pieces:

    • If you're on a Mac, use Spotlight (the magnifying glass in the upper right corner of your computer screen) to search for Terminal. Open up that app. If you haven't done anything with Terminal before, a black window will open with white text on it, ending in something like this :>
    • You are most likely in your home directory. You could make a folder here, but it might be difficult to find later. Just for right now, let's go to the desktop and make a folder.
    • In your Terminal, type the following: cd Desktop
      • cd means 'change directory'. You're moving from the home directory to the Desktop directory.
    • Now that we're on the Desktop, let's make a new folder on our computer. Type the following: mkdir Repositories
      • mkdir means 'make directory' and creates a new, empty folder/directory.
    • Now that we've made a directory, let's use cd again to move into that new empty folder.
    • Type the following: cd Repositories
    • Great! You're now capable of creating and moving into directories! High-five!
    • Now let's put something into that empty directory. Go back to the Algorithm's repo on Github. On the right side, you should see HTTPS clone URL over a window with the following URL in it: https://github.com/<YOUR GITHUB USERNAME>/HR-Algorithms-Meetup.git
    • Clicking the button on the right side that looks like an arrow on a clipboard will copy the URL.
    • In your Terminal, type the following: git clone https://github.com/<YOUR GITHUB USERNAME>/HR-Algorithms-Meetup.git
      • You can just type git clone and then paste the URL that you just copied.
  • When you see Checking connectivity... done. you know that you've successfully cloned the repo onto your local machine.

    • What does cloning actually do? It creates a copy of the repo, as it exists right this second on Github, and puts that copy onto your computer. Now you're able to work on it whenever you want!
    • Now that you've cloned the repo into your Repositories directory, we need to use cd again to get into that folder. But what if we're not sure of the folder's name? We can use ls (which, for all intents and purposes, means 'list stuff') to show us what files are in our current directory.
    • In the Terminal, type the following: ls
    • You should see a directory called 'HR-Algorithms-Meetup'. Let's get in there!
    • Type the following: cd HR-Algorithms-Meetup

Branching the Repo

All this time we've been on the master branch. If git is a tree, master is the trunk. We don't want to work on master. I'm going to repeat that. WE DON'T WANT TO WORK ON MASTER!!!! We want to create a branch of our very own! So let's do that using git checkout -b (-b means branch)

  • Type the following into Terminal: git checkout -b <Current Weeks Algorithm> (This name is unique to your forked repo, you can name it anything. Usually these names are the topic of the changes being made)

  • Now we are on our own branch! All the work we do can be kept separate of master which is good in case we mess up. Everyone should have their own branch to work on.

  • Now that we're on our own branch and we're in the cloned directory, let's see what's in there! Type the following into the Terminal: ls

  • All kinds of files and directories popped up! Awesome! We can either use the command cd to move into a directory, or we use the command open to open one of the files in our default text editor.

  • If you are using an editor like sublime-text you can type subl . to open the entire directory in your editor.

  • Let's move into current algorithm for the week.

  • In your Terminal, type the following: cd DynamicProgramming or the folder name of the current algorithm.

  • If you want to create a brand new file, use the command 'touch'. Normally you would create a new file to do your work in, but for now let's have some fun! Type the following into Terminal: touch pictures-of-cats.mdown

    • We just created a new empty file. Let's open it! Type the following: open pictures-of-cats.mdown
    • Let's add a picture of a cat to our new file!
    • http://placekitten.com/ is the best place to get free photos of cats. Add two numbers to the end of that URL to get a photo of a kitten that is the dimensions of the numbers you entered. For example, http://placekitten.com/200/300 will give you a photo of a kitten that is 200 pixels wide and 300 pixels high.
    • In your text editor, type the following: ![Look at this awesome kitten!](http://placekitten.com/400/300 "photo of an awesome kitten")
    • Save your file

Submitting Changes and Solutions

We've made a change to the original HR-Algorithms-Meetup repo that we forked. It now has this new file called pictures-of-cats.mdown. Our file is pretty much the greatest thing ever, and we want to share it with the world, so let's push it up to Github and pull request it!

  • Step 1 is to stage our current work. We want to add our work to the staging area using git add. Type the following into the Terminal: git add pictures-of-cats.mdown
  • Next we need to commit our files. Your commits are a history of your changes. You should be able to look at your commit history and read the story of your code. Lots of programmers are bad at writing commits, and end up with things like "tests pass. good enough." or "asdmsafpnvirepbnirn fd this shit is stupid and I hate it!!!!!!!!!". Instead, use git commit to record what you did, and what you'll do next.
    • Type the following into your Terminal: git commit -m "I copy-pasted a picture of a cat. It's pretty rad."
    • The -m means message and everything in quotes is your commit message.
  • Let's push our branch up to Github so that we can share it with the world! Type the following into the Terminal: git push origin <Current Weeks Algorithm> (or whatever you named your branch)
  • The push command is doing the opposite of pull. Pull brings something down from Github onto your computer, we're pushing something up from our computer to Github.
  • origin refers to the Github repo that we originally cloned
  • <Current Weeks Algorithm> is the name of our branch. Always push to your own branch and then issue a pull request if you would like your work to be merged into master.
  • Go the the official repository on github: https://github.com/MrNice/HR-Algorithms-Meetup
  • On the Github repo on the right side, you'll see a tab that says 'Pull requests'. Click on that.
  • There's a green button in the right corner that says "New pull request". Click on that.
  • There are 2 drop-down windows, 'base' and 'compare'. We want to compare our forked repo / new branch with the official repo / master branch. Change 'compare' to your branch and click the green "Create Pull Request" button.

You did it!!! You forked a repo, cloned it to your computer, created a branch, and issued a pull request. You're a master of git!

Feel free to ask for help with all of this. Git is tricky. Coming from a communications background, it took me almost a year to get comfortable using git. Just stay off master, and you'll be just fine! :)