Skip to content

Guide: Getting Started with Github and Forks

knod edited this page May 25, 2018 · 31 revisions

Getting your hands on our code

This is a guide for people new to the world of git and Github. It tries to be pretty thorough, but feedback for improvement is always welcome.

Disclaimer: Many of the links in here are to guides that we didn't make. We can't do it all, people. At least, not all at once.

Tip: Don't worry, if you break something we can fix it. We have that power.

Assumptions

  1. You know what your command line interface is
  2. You have a Github account
Tip: If you have questions, ask them in the #cliff-effects-tech Slack channel.

Summary

To contribute to our code you need to be able to:

  1. Get our code.
    1. Make your own github copy of our repo (a fork).
    2. Download a copy to your own computer (locally). It'll be linked to your fork in a way that I'll explain later.
  2. Deal with changes to code.
    1. Be able to stay up-to-date with changes to our code - the codeforboston repository.
    2. Be able to make and 'save' changes to the code.
    3. Upload those changes.
    4. Request that we add them to our code.

Informational Sections and Links

  1. A bare-bones guide to git collaboration branch workflow
  2. Diagram of basic code-changing workflow
  3. Intro to branches
  4. New branches
  5. How all the repos relate to one another after you've set up.

Groundwork

To simplify, Github is a folder on a computer somewhere. The codeforboston account is a folder in the Github folder and the cliff-effects repo is a folder inside of that one - github/codeforboston/cliff-effects. Your account is also a folder in the github folder. Your browser is a way for you to see the stuff in those folders. It just looks prettier than your desktop folders.

Basically, we're going to copy the cliff-effects folder to different places so that we can make changes to files without messing up each others' work. It's a bit convoluted (for reasons), and there's a bit more to it - like how we link up those folders so we can sync them with each other - but we'll try to walk through that.

There will be a picture of these folders lower down when you've got some more details.

1. Get Our Code for Yourself

The workflow for this setup is completely different from the workflow you'll use in the future to work with the project. Right now it will go something like this:

codeforboston/cliff-effects -> youraccount/cliff-effects
                                        |
                                        v
                               Desktop/cliff-effects

1.1 Forking our repo

Unless you delete your fork (which does happen sometimes to get a fresh start), you won't have to do this again.

First we're going to copy the github.com/codeforboston/cliff-effects folder into your Github folder - your account. Let's call that account 'youraccount' from now on. That's the folder online where you'll send your changes so that we can see them before bringing them into the official project's code.

Near the top-right of our repo page, there's a button that says 'Fork'. Click that. Now you'll have a new folder/repository in your github account that's also called 'cliff-effects' - github.com/youraccount/cliff-effects.

Github fork button

People will often call this your 'upstream' repository. Technically, 'upstream' is a more general term for any repository on the cloud, but that's the way things panned out. ¯\_(ツ)_/¯

1.2 Download the files to your computer

Unless you delete your fork to start fresh, you won't have to do this again.

Note: This guide discusses how to use git, not the Github desktop app.

So the bummer about downloading your files to your computer is that the other contributors won't be able to see the changes you're making there. That's why we need your Github repository. On the other hand, working with them on your computer will be a lot easier than working with them on Github.

  1. Test if you have git by typing git --version into your command line. If you don't get a number, you don't have git, so set up git. Make sure to follow all the instructions, including setting up authentication.
  2. If you don't already have it open, open up your own account's cliff-effects repository. Make sure it's not the Code for Boston repository.
    1. Go to your profile (click on your user icon in the very top-right to see the menu item for it)
    2. Click on 'Repositories'
    3. Click on the one called 'cliff-effects'.
    4. You'll be able to tell it's your fork because it will have your username next to the words 'cliff-effects' at the top and it'll say it's a fork of 'codeforboston/cliff-effects'

How you can tell it's your repo

  1. Near the top-right of your repository page should be a green button - 'Clone or download'. Click that. You have the option to use SSH or HTTPS. Use the one that you chose when you used git to authenticated with Github. This is what it looks like to clone with HTTPS on knod's repo:

Clone with HTTPS

  1. Once you've chosen SSH or HTTPS, click the 'copy' button. That'll automatically copy the right url for you to paste.
  2. Open Terminal and navigate to the folder in which you want to keep this project's folder. (Navigating in Windows and Mac.) What we're about to do will make a folder there called 'cliff-effects'. The rest of this guide is going to assume you put it in your Desktop folder.
  3. Type exactly git clone, add a space at the end, and then paste the url that you copied from your Github repo. Hit 'enter'. You should get a message like this:
Cloning into `cliff-effects`...
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (8/8), done.
remove: Total 10 (delta 1), reused 10 (delta 1)
Unpacking objects: 100% (10/10), done.
  1. Navigate into that cliff-effects project folder by typing cd cliff-effects and hitting 'enter'.

At this point you can follow the setup instructions to be able to see the project in the browser.

If you do delete your fork, we strongly suggest deleting this folder. If you can't stand deleting it, rename it, move it, or in some way keep it from getting confused with the new folder you create.

Visual Summary

The full picture of our code to yours Full picture of our code to yours

Digression 1 - Branches

So we've talked about how everything's folders, and now there's yet another layer of folders. These are what git calls your 'branches'. Every repo contains these 'branches'. You can see what folders/branches the codeforboston/cliff-effects repo has in it by going to that repo's page and clicking on the drop-down menu above the folders view with the words 'Branch: dev' on it. We should have, at the very least, 'dev', 'gh-pages', and 'master'.

When you forked our repo, you copied those branches/folders too. You can see them in the same way in any repo you have. In your local copy (your local repo), you won't be able to see these folders in your Finder or File Explorer, though. Even though they really are folders, the interface for git doesn't show them in that kind of linear way.

You're going to be jumping around in different branch-type folders a lot so that your changes don't get tangled with other people's changes, though, so it'd be good to be able to easily see which branch you're currently in. When your command line is navigated into the Desktop/cliff-effects folder, you can type git branch to see which branch/folder you're in. Right now you'll only have the branch 'dev'. You can tell what branch you're in because it'll be colored green and have a star to the left of its name.

Tip: Never edit or change files or folders while you're in the 'dev' branch.

2. Set Up for Managing Code Changes

The bare bones

(A more bare-bones guide to the workflow, in case you're already set up and already know the basics)

The workflow

The workflow from now on is different. A very simple representation:

cfb/cliff-effects <----- youraccount/cliff-effects
       |                           ^
       |                           |
       --> Desktop/cliff-effects --

2.1 Keeping in sync with codeforboston/cliff-effects changes

The only branches/folders anywhere you need to worry about for this step are called 'dev'.

What's a remote branch?

So there's yet another kind of folder. It's in your local repository. This is like a Dropbox folder, except it doesn't sync automatically. All it is is a folder that is linked to a specific repo in the cloud and contains whatever you've most recently downloaded from there. You can use this link to upload and download.

Right now, you have one of these branches already. Do git remote and you'll see a list of all the linked up names you have. Right now you should just see origin. That is name that git automatically gave to the link to your Github repo - your fork - when you did git clone. If you want to see the url it points to, do git remote -v. We'll work with that later. You now have to set up a link to the CfB repo.

Why add this remote branch?

Well, whenever your changes, or other people's changes, get accepted into the codeforboston/cliff-effects repo the contents of our 'dev' folder will probably be changed. If you don't manually sync up with us, you won't see our changes and, worse, when you make further changes and then try to send them to us, they may end up conflicting with the changes we've already made. That's why it's important to keep up to date with us.

So that you can pull in our changes, you need to set up what I call a 'linked folder'. I think I'm the only one that calls it that, though. It's like origin, but it'll be pointed at the github.com/codeforboston/cliff-effects folder. You can name it whatever you want, but I'm going to name it 'upstream' and that's how I'll talk about it from now on.

2.1.1 Set up the link to our repo

Unless you delete your fork to start fresh, you won't have to do this again.

  1. Go to the github/codeforboston/cliff-effects repo.
  2. Near the top-right of that page should be a green button - 'Clone or download'. Click that. You have the option to use SSH or HTTPS. Use the one that you chose when you used git to authenticated with Github. This is what it looks like to clone with HTTPS on knod's repo:

Clone with HTTPS

  1. Once you've chosen SSH or HTTPS, click the 'copy' button. That'll automatically copy the right url for you to paste.
  2. In the project's command prompt, type exactly git remote add upstream, add a space at the end, then paste the url you just copied.
  3. Tap 'enter'
  4. Do git remote. You should see:
origin
upstream

Digression 2 - Diagram of repo relationships now

2.1.2 Download changes from the CfB repo

You will need to do this every time you want to work on a new set of changes.

Tip: Always do `git checkout dev` before this next part to make sure
you're on the 'dev' branch (in our terms - in the 'dev' folder).
  1. Now whenever you want to get changes from the codeforboston/cliff-effects/dev branch, do:
    1. git fetch upstream dev. That downloads the changes into a folder called 'dev' in your 'upstream' 'linked folder'.
    2. git merge upstream/dev. Whatever branch you're in, git will try to combine the changes in the 'upstream/dev' folder with the ones you've made in the branch. If you haven't made any changes in the branch, there isn't any chance of any problems. If you do have problems, feel free to post about it in the #cliff-effects-tech Slack channel or to talk to one of us.
Tip: You can combine those commands into one command by just doing
`git pull upstream dev`.

We suggest only ever doing this while in your own 'dev' branch because whatever changes you've made in a different branch may conflict with our changes and then it'll be a bit of a mess for you to work out.

  1. If you've got a good, clean, 'dev' branch that now exactly matches the CfB repo 'dev' branch, do git push origin dev to upload those changes to your online repository, your fork. This keeps your online repository in sync with everything else. We'll explain 'pushing' later.

Digression 3 - New Branches

I'm going to remind you to think of branches as folders. When you make a new 'branch', what you're basically doing is making a copy of the current folder/branch you're in (for example, 'dev') and giving it a name. Each branch should be for a group of changes that are all related to each other. The name should be something to give people a useful hint about what that branch is for. If you're changing the color scheme of the site, you might call it 'colors'. We'll tell you the commands to do that lower down.

If you make some changes on the 'colors' branch and you then do git checkout -b graphs to make a branch to work on graphs for a while, your color changes will get mixed in with your graph changes and we'll have a hard time accepting all those changes at once. What if we like the graph changes, but don't like the color changes? It'll be a mess to untangle. Try to not do that, but it happens to everyone sometimes. We'll help you work it out.

If you do want to work on graph changes, do git checkout dev and then do git checkout -b graphs.

Keep the changes in a branch focused on one topic at a time.

2.2 Make and 'save' changes locally

You will need to do this every time you want to make changes.

Tip: 'Save' early and often. It helps make sure that if you break your code,
you can go back to working code.

We're going to pretend that you're going to call your new branch 'good-branch-name'.

  1. To make sure you're on the 'dev' branch, do git checkout dev
  2. Make a new branch with git checkout -b good-branch-name
  3. Edit files and save them the regular way.
Tip: To see what files you've changed, do `git status`. This will let you see
the right filepath to use later, too.
Tip: To see all the specific changes you've made, do `git diff`.
  1. git add: When you've got a group of edits that go together (for example, you've updated one color), do git add file/path/file-name.whatever for each file that you want to add. See the tips above for getting the correct file names.
  2. git commit: This truly saves your changes. If you mess up in the future, you can go back to any past commit that wasn't messed up. It's like the ultimate 'undo' button.
    1. When you've added all the files that matter to this change, do git commit. This will open an interface for adding a message. Each commit will need a commit message to let people know (or to remind yourself) what this change was about.
    2. Tap 'a'.
    3. Write a short and useful description of what you've done. The convention is to write it in the present tense. For example 'Changes green to red.' Keep it shorter than 80 characters. If you need a longer description, there's a way to do it, but we won't go into it now. You'll get a different chance to leave us a longer explanation when you send us the request to accept your changes.
    4. When you're done, hit 'esc', then type in :wq and hit 'enter'.
  3. Do git status again to make sure that you got all the files you needed. None of those files should show up anymore.

2.2 Upload those changes so you can ask us to use them

You will need to do this every time you want upload changes.

Uploading is called 'pushing' in git. Why would they call it something so strange? I think git was around before 'uploading' was the word everyone used, and everyone had their own name for it. Git called it 'pushing'.

  1. If this is the first time you're pushing this branch and its changes to your fork, do git push -u origin good-branch-name. This pushes your changes, but -u hooks up this branch to remotely track the branch/folder on your online repository. It's useful for reasons, one of which is item #2 here.
  2. If this isn't the first time, just do git push.

2.3 Getting us to take in those changes (with a PR)

This here is purely a Github thing. It's called a PR, or 'pull request'. You're requesting that we pull your changes into our 'dev' branch. That is, you're politely, through Github, asking us to go to our 'dev' branch and do git pull your-repo good-branch-name.

  1. Go to your fork on your account on Github.
  2. Use the drop-down menu that says 'Branch: dev' to pick the 'good-branch-name' branch.
  3. When that page finishes loading, there will be a button next to the drop-down menu called 'New pull request'. Click that button.
  4. You'll see a new page that looks like it has a comment at the top. In that comment will be one of two things.
    1. If you made just one commit, it'll be your commit message.
    2. If you made more than one commit, it'll be your branch name.
  5. If you need to, change the title of that comment to be more descriptive. Still keep it short.
  6. If you want to tell us even more, write more info in the body of the comment.
  7. Look over the code changes that are shown below to make sure they're what you wanted them to be. If they're not:
    1. Go back to your editor and make the changes you need to make.
    2. git add file/path/file-name.whatever and git commit again.
    3. git push again.
  8. If you've also accepted an invitation from us to be a contributor, you can do a few other things if you want:
    1. Go to the right-hand column next to the 'comment'.
    2. Under 'Reviewers', select one or two reviewers. 'knod' is a good bet. Others might be 'ethanbb', 'wacii', or 'shp54'.
    3. Under 'Labels', if you see one or more labels that seems to match what you're doing, select it/them.
    4. Under 'Projects', do the same.
  9. When you're ready, at the bottom of the comment, click 'Create pull request'.

We'll try to get to that PR when we get a chance. If you get impatient, you can try posting in the #cliff-effects-tech channel to see what's up.

More accurate diagram of working workflow

/cfb                            /youraccount
    /cliff-effects                  /cliff-effects
        /dev <----- your changes ------ /bug-fix
          |                                ^
          |                                |
      our changes                          ^
          v                             your changes
          |  /Desktop/cliff-effects        |
          |----> /dev                      |
          |----> /bug-fix ------------------

More Resources

For a basic guide on how to keep your branches untangled as you make changes, check out our git workflow wiki page.

For those moments when you've broken something, checkout http://ohshitgit.com/

Review of Some Git-related Command Line Commands

Exploring

git status - Shows you what files have changes that haven't been committed yet. There are two types of these.

  1. Files that have changes that you've already added to the list of files that will be committed.
  2. Files that have changes, but that you aren't ready to be committed.

git branch - Shows you a list of the 'branch' names in your local repository (the one on your computer). The one with the star next to it is the one that your edits will change.

git diff - Shows you all the specific changes you've made to the code.

git remote - A list of names. Each of these gives you a link to an online repository that you can use to download or upload changes. The one to your own fork is automatically called 'origin'.

git remote -v - A list of the names of online repositories to which you have links plus the urls that they use.

Moving around

git checkout -b branch-name - Make a new branch and go to it so you can start making new changes. branch-name can be whatever you want it be, but try to make it something that will give us a clue about what it's for.

git checkout branch-name - Move to a branch that already exists to work on changes there.

'Saving' edits

git add file/path/file-name.extension - Pick a file and set it up to be saved. That's called 'staging' a file.

git add . - Add all files that have changes all at once. Yeah, we didn't tell you about this earlier. It's easier to make mistakes with this one.

git commit - DO THIS A LOT. Really, truly, for real save these changes so that if you mess up in the future you (or we) can go back to a version that's not messed up anymore. It also helps us fix things when we notice that something got broken a while ago. We can track it down much more easily.

Downloading and Uploading

git pull name-you-already-made some-branch-name - You've set up a folder with some name. When you set it up, you linked it to some repository at some url. Download changes from a branch in that repository and merge them into the branch that you're at. name-you-already-made can be any name you've created with git remote add. some-branch-name has to be the name of a branch that's in that online repository. This command really combines git fetch and git merge.

git fetch name-you-already-made some-branch-name - You've set up a folder with some name. When you set it up, you linked it to some repository at some url. Download changes from a branch in that repository and save them on your machine in a branch in that folder - 'name-you-already-made/some-branch-name'. name-you-already-made can be any name you've created with git remote add. some-branch-name has to be the name of a branch that's in that online repository.

git merge name-you-already-made/some-branch-name - Combine the code in that folder with the code in the branch you're at. name-you-already-made can be any name you've created with git remote add. some-branch-name has to be the name of a branch that's in that online repository.

Clone this wiki locally