Tuesday, September 8, 2009

Git Me Checking In

I was working on a project today and happened to be working from a project stored in GitHub. Now, I'm not particularly familiar with Git, so I kind of stumbled through with a lot of help from Google and some help from a friendly engineer who knows more than I do about this stuff. I'm coming from SVN and Perforce, mostly, so please forgive the analogies.

This is what I wound up doing:

A note on topography
With Git, you have several "code chunks" going on at once.
  • Remote master: This is the actual code that will be deployed somewhere. It's the equivalent of the server in, say, SVN or Perforce. If you're working on a project with people, their changes will eventually end up here.
  • Local master: This is your local copy of that remote master branch. Note that this doesn't automatically update when other people check stuff in. You have to update it ("pull" in git, roughly equivalent to "sync" in perforce). No surprise there. (We'll call this "master" below.)
  • My working branch: This is the actual branch you're working on. Near as I can tell, it's basically branched from your local master. (We'll call this "mine" below.)

To "Check Something In"
By "check something in" I mean "I'm done with it and ready for it to go into the remote master so that the world can see it and so it can be deployed to the staging/production server".

1. Start on your branch
git checkout mine
2. Make sure you've committed your changes
git commit files
You'll see something like this:
[mine b0b6f83] Add namespace help
1 files changed, 8 insertions(+), 2 deletions(-)
3. Switch to your local master
git checkout master
You'll see something like this:
Switched to branch "master"
4. Update your local master so it has everything that others have done since you last updated
git pull
You want to see something like this:
Updating 51dd07f..c19dab1
(files)
5. Switch back to your working branch ("mine")
git checkout mine
6. Pull changes from your local master to your local working branch
git rebase master
You might see something like this, but it doesn't appear to affect anything:
warning: 3 lines add whitespace errors
7. Run your tests and make sure it all still works
8. Switch back to your local master branch
git checkout master
9. Merge all changes from your local working branch to the local master branch
git merge mine
10. Finally, send it all to the remote master
git push

And you're done. Obviously, there are a lot more things you can do at every step here. This is just the path of least resistance I found to get started. Once you have this down, I'd definitely encourage you to play with branches and different commits.

And just for kicks, there are a couple other tricks I picked up along the way:

Reverting a file you changed and didn't want to (before you commit):
git checkout -- db/schema.rb

Making your name and email show up properly:
git config --global user.name "Catherine Powell"
git config --global user.email "myemail@example.com"

Many thanks for all the assistance, and in particular to these blog entries:

No comments:

Post a Comment