A place to discuss Development techniques, .NET, XNA, NHibernate or anything else that tickles your fancy

Sunday, June 6, 2010

Git Day to Day usage

Part 2 of the Git Guides I wrote up for my XNA team:

Let's discuss how to use Git once you've got your personal repository cloned. First you're going to want to open up Git Bash to follow along. You can use Tortoise and Git Gui to do these processes as well, but the command line has everything I need to be able to walk you through this. So right click on the repo folder (the folder you cloned to) and choose "Open Git Bash Here".


When you clone your repo from your personal repo, Git automatically adds your remote repository as your "origin" repo. You can type in "git remote" and see it listed under "origin". This is your working repository, and what you've got write access to. First things first, you need to add your "upstream" repo. This is typically a read only repository and it's where everyone pulls their changes from. In this case, it's:
Code:
ssh://git@projects.domain.com/projectMain.git


Type in:
Code:
git remote add upstream ssh://git@projects.domain.com/projectMain.git


Now when you type in "git remote" you'll have two listings: origin and upstream.

Origin: Your personal repo that you have write access to
Upstream: The main repo that you only have read access to which you fetch changes from

So lets say you've settled in for an evening of coding, here's the typical way you'd begin your night:
Code:
git fetch upstream
git rebase upstream/master
git branch SomeNewBranchName
git checkout SomeNewBranchName


This 1.) Gets changes from upstream 2.) Rebases your master branch, updating it with the changes from upstream 3.) branches master to a new branch for you to make some changes (favor branching over working on the master branch directly) 4.) switches your current branch to the new branch which changes all the files on your HD so you can start working on your new branch immediately

Say you make a few changes, and now you're ready to commit for the first time. You can run a "git status" to see the files that you've got that have changed. Or you can open up the Git Gui and it'll show you your pending changes. In general, we want to add all the changed files to the repo, like so:
Code:
git add *


Now your changes are staged you can perform a local commit:
Code:
git commit -m "Made some changes and these are my checkin notes"


You can continue working on that branch and staging/making local commits with additional notes. That's part of the beauty of a distributed repository source control system, you are your own source control offline. So when you're finally done and you'd like to put what you've got online you can use:
Code:
git push origin SomeNewBranchName


This will send your local checkin history to the repo you have specified as "origin" (which is the place you cloned from, your writable repo). Now, if that feature is ready to be pulled in to the trunk of our project, you can issue a pull request to the maintainer. Once they receive the request, They'll go in and pull the changes you just pushed to your repo, and then merge them in to the main projects master branch for everyone to see the next time they do a "git fetch upstream".

Now you can run your fetch/rebase process again, and your master branch will be updated with the changes that was pulled in to the trunk.

So, to recap:
1.) Add the main repo as upstream
2.) fetch/rebase to update your local repo
3.) create a new branch whenever you start in on a new feature/change
4.) do lots of small local checkins at logical points as you work on a feature/change so it's easier to roll back to
5.) push to your origin when complete or when you want someone else to check out something you're working on or you want to make sure what you've got gets backed up

Tips:
- Favor branching first over working on master
- Add everyone elses repos to your git config list so you can check out everyone's changes if the need arises. But make sure that you've got your personal repo, and the main upstream repo specified as you'll use those two exclusively during your day to day process

0 comments:

Post a Comment