April 30, 2017

Quick GitHub Tutorial

There are tons of great GitHub tutorials out there:

https://www.udacity.com/course/how-to-use-git-and-github--ud775

Which are super great, the thing is though, it can be somewhat intimidating and it can get confusing and sometimes you just want to make something do something quick and work from there instead of learning all the things and only then maybe doing something.

So, for the purposes of reminding myself how to do these things as well as helping those who comment that they'd like to get to learning some GitHub but don't know where to start- hopefully this will help. For this example I'm going to just track a file, but you can do it with a folder too.

1. I have a file/folder/project I want to track- what do I do?!!

Calm down. Download git and install. It's not gonna do anything, so don't fret if you don't see a pop-up.

Open your Command Line Interface (terminal if osx, powershell if win) and navigate to the folder you want to track with git, here are some commands to help with that (type these in the command line to do things):

ls => lists all folders/files your terminal is currently at
cd => changes directory of where your terminal is currently pointed at
cd ~ => goes back (mind the blank space)

Example, given your CLI starts up at your home directory (which is where Desktop is on a mac):
Type: cd Desktop

Now:
Type: ls

You should now be seeing a list of all your files/folders on your Desktop.

*click here for a crash course with the command line. Here for a command cheat-sheet.

Are you at the folder where your folder or file is? Here's what mine looks like; I'm trying to track this brief list of water pokemon on my desktop: waterPokemon.rtf

Cool, now to begin tracking that file, you first need to initialize git!
Type: git init

Now, we need to add this list of water pokemon to the "Staging Area". It's just something you need to do. If you try make git register (commit) without first adding, it's just not gonna do it, it needs to track it first!:
Type: git add <fileName>

After typing that, the CLI isn't going to do anything, so get the status on the current directory to see what's up:
Type: git status


You'll notice the file is ready to be committed, so commit it:
Type: git commit -m "super descriptive commit message!" <fileName>

Here's what you should see:


That's it! You've now created a local repository :)

2. I want it to be on GitHub.com tho!

So you don't just want to track this thing locally? You're worried your computer might blow up and you'll lose everything? Then make a GitHub account.

On the front page of GitHub.com you should see a green "New repository" button, click it:

If not, just go here

Fill out the details on the form and click the green "Create repository" button


After that GitHub.com will give you some options and since we already have a local repo that we just want to "push" into GitHub, use that "...or push an existing repository from the command line" option. Make sure your CLI is still in that directory of the folder/file you were tracking, and just copy and paste that option's code into your CLI:


Check your GitHub.com, your file should now be listed in the new project you created

That's it! Your folder/files whatever it was you were working on, are now on GitHub!

3. Interesting, but I thought GitHub/Git was about version control, not just pushing your stuff online!

True true, so then...make a new branch!

Type: git branch <branchname>

It won't look like git did anything but,
Type: git branch <branchname>

This shows you a list the branches on the file you're tracking. The asterisk tells you the current branch you're on (master branch in my case). To switch to that new branch we made:
Type: git checkout <branchname>

Now,
Type: git branch

You'll see we're now in your new branch where you can make local edits to that file then push your edits to the remote version on GitHub by making a "Pull Request".


4. What's this whole pull request deal?!!

Well, it's how you push your local repository of a project to the "master" project. So say your friend was working on a file, you downloaded it, made changes to the file, "staged" it, "committed" it, and after you did all that locally, you finally decided to push it to the remote repository where the master is- so push your branch!

I've already saved my changes on the txt file, and staged & committed locally, now I'm just pushing remotely to make the pull request:
Type: git push origin <branchname>

Go to your GitHub and you'll now see an option to create a new Pull Request on the project:


After you make the pull request, the owner of project (you in this case) will be able to review it and merge it to their master copy if they want to:


That's pretty much all there is to it!

Well actually not, there's tons more to this. Many commands and methods approaches to version control on git (which is what makes it super powerful) but, there are way better tutorials out there to get you acquainted with those.

5. Extras

If you ever want to delete a git repo locally just "change directory" to the parent repo and
Type: rm -rf .git

To delete the remote repo, go to github and delete it in the project settings all the way at the bottom.

Here's a couple of nice gui's for git:

https://www.sourcetreeapp.com/
https://www.gitkraken.com/download


Thanks for reading!