Rails Girls Next

February 19, 2014


Tips and Tricks

Working with Git

Created by Lucy Bain, @lucykbain

Nowadays Git is part of most developer's work flows. It's a handy tool, and a great one to learn more about. This evening we're going to learn more about Git on it's own, without a full Ruby on Rails project to clutter things up. Instead, we'll write a story.

Some of you may have played this game before. Someone starts a story and writes just one line, then the next person continues the story by adding another line, the next person contributes another line, and so on. Eventually you're left with a story (of sorts) that everyone has contributed to. We're going to play that game, but we'll use Git!

Step 0: Clone the repo

Make sure you have a GitHub account. If you've done Rails Girls before, you should have one already, but this is the time to find your username and password.

I made a repository and Angie made a second repo for today's stories, you'll need to clone your assigned repo and open it in your editor (I'll be using Sublime).

To clone Lucy's repo use git clone git@github.com:lbain/RGN-july-2014.git, and to clone Angie's repo git clone git@github.com:angiegove/RGN-july-2014-group2.git.

Step 1: The demo

I'll go through a demo with another leader so you can see all the steps involved. Here's a quick review:

  1. Pull down the latest version of the code git pull origin master
  2. Make your change (open story.txt and add a line)
  3. Stage your change with git add story.txt
  4. Commit your change using git commit -m "your commit message here"
  5. Push your change to GitHub git push origin master

Step 2: Make your contribution

We'll go around the group so everyone gets a turn. When it's your turn, follow the steps above to add your line to the story. Feel free to be silly and don't worry about typos, we'll fix them later!

Step 3: Checkout a branch

There's been some creative differences between myself and another mentor, we want to take the story in different directions. We'll create new branches each and assign you to work on a particular branch.

You need to checkout the branch you're working on with git checkout BRANCH-NAME

Step 4: Make another contribution

Just as we did before, everyone will contribute to the story line one at a time. The commands remain the same, but instead of pulling and pushing to master you will use your BRANCH-NAME instead.

Step 5: Make a pull request

At this point, there have probably been a few mistakes, or some plot twists that didn't quite make sense. We'll tidy those up now. But we don't want to go around changing other people's work without asking them. So we'll make a pull request to let them know.

Your mentor will demo how to make a pull request, then it will be your turn.

The main command you'll use is git checkout -b BRANCH-NAME.

Step 6: Merge a pull request

Once everyone has made pull requests, you need to go back and comment on two pull requests that aren't yours. Then head back to your pull request to see what people said about yours, and decide if your pull request should be merged in or closed and left out.

Step 6: Fix a merge error

Sometimes you might make changes and forget to run a git pull first, or someone else might git push changes while you're making changes of your own. This can cause merge conflicts. Here's how we'll simulate this:

  1. Persons 1 and 2 will git pull, make a change to the story, and then git commit
  2. Person 1 will git push
  3. Person 2 will try to git push, but should get rejected
  4. Person 2 will git pull, resolve the merge conflict, and git commit
  5. Person 2 will git push, successfully this time
  6. The current person 2 will be person 1 to the next person in the group

Go around the group and get everyone to be person 1 and person 2 so everyone has resolved a merge conflict.

Step 7: Enjoy!