Track your changes to an open-source project with git: Difference between revisions

From Bitpost wiki
No edit summary
No edit summary
Line 40: Line 40:
The index is an extra layer that you don't usually have with cvs/svn.  It's totally a benefit, and you can ignore it by doing the add and commit in one step.  This will add every file that is in the repo and has been modified to the index, then commit, in one step ("cvs/svn style"):
The index is an extra layer that you don't usually have with cvs/svn.  It's totally a benefit, and you can ignore it by doing the add and commit in one step.  This will add every file that is in the repo and has been modified to the index, then commit, in one step ("cvs/svn style"):
  git commit -a -m "I changed this to that."
  git commit -a -m "I changed this to that."
So over time, you'll have a series of commits to your branch.
So over time, you'll have a series of commits to your branch. Later, you'll want to get the latest svn changes and incorporate them.  We're going to use rebase again.  Previously, we used it to update from svn, let's do that again.  Make sure you switch to the svn branch first.
 
Later, you'll want to get the latest svn changes and incorporate them.  We're going to use rebase again.  Previously, we used it to update from svn, let's do that again.  Make sure you switch to the svn branch first.
  git checkout master
  git checkout master
  git svn rebase
  git svn rebase

Revision as of 18:00, 28 January 2008

Here are quick and easy steps to track your changes to an open-source project.

Once you get a handle on git, you'll find it very powerful. But getting started can be the biggest challenge. Distributed source control requires a different way of thinking than using a central repository. git's terminology is a little bent from what you're used to. Use this guide to get past the first hurdles.

Requirements

Before you can start making changes to a project, you should get the code and make sure you can compile and run it. In this guide, we assume that the project is currently under subversion(svn) control. Check out a copy and get it up and running.

The next set of requirements is pretty simple:

  • I need my own branch off of the svn branch.
  • I need the ability to overlay the latest svn changes, over time, on top of changes in my branch.

Notice how simple this is - we're not even asking to commit anything yet. These are fundamental requirements that any developer on a non-trivial open-source project would have. You need to do this if you are going to bang on an open-source project.

Now let's add one more requirement. I'm going to have this code on at least three different machines, so I need to be able to have a common repository for my changes.

  • ability to work from several locations, pushing/pulling my changes to a central repo

Another basic source control requirement, and we're done.

But try to meet this set of requirements with cvs or svn. No longer simple. Enter git. It was born to do this job. If you're with me so far, and it sounds like we're on the right track, I promise you: step through the rest of this guide and you'll be happy you did. Let's get to it.

Setup

NOTE: I'm going to use mythtv in this example, because that's what I wanted to work on.

Create a local git repository of the project's subversion repository, using git-svn. There are two ways to do this. If you do not specify a svn revision number, git will grab the entire history of the project as it is available in the svn repo. WARNING: this may be HUGE for older bigger projects!

git svn clone http://svn.mythtv.org/svn/trunk/mythtv mythtv

If you specify a revision number, git will grab just that version, and then we can grab all changes after that version, too. It's probably worth digging into the project history to find a reasonable revision number.

git svn clone -r15502 http://svn.mythtv.org/svn/trunk/mythtv mythtv

We now have a repo of the project under git control. Simple, eh? git uses branches, and named our svn grab the "master" branch in this repo. See the list of branches:

git branch

Now we can easily update our master branch with the latest svn commits at any time. Do this now to make sure you're completely up-to-date:

git svn rebase

Now we create a branch of our own, derived from the svn base. git loves branches!

git checkout -b mybranch
git branch

Now that you have your branch checked out, you can bang away and change anything you want to (except the top-level .git directory). Eventually you'll want to commit your changes. Time for some terminology (I swear I'll be brief - on a "need to know" basis). As you edit, you're only changing local files. git refers to the locally modified files as your "working set". git doesn't track anything about your working set - you're free to abuse it as you see fit. If you want to keep changes you've made, you need to add each file you've changed or added to the git "index" (also called the "cache"). Then, when you're ready, you commit all the indexed changes to the repository in one single commit. So git provides the "index" as a space in which you can manage your next commit.

Let's say you've changed the configure script. Here's how to commit the change to your git branch:

git add configure
git commit -m "I updated the configure script."

The index is an extra layer that you don't usually have with cvs/svn. It's totally a benefit, and you can ignore it by doing the add and commit in one step. This will add every file that is in the repo and has been modified to the index, then commit, in one step ("cvs/svn style"):

git commit -a -m "I changed this to that."

So over time, you'll have a series of commits to your branch. Later, you'll want to get the latest svn changes and incorporate them. We're going to use rebase again. Previously, we used it to update from svn, let's do that again. Make sure you switch to the svn branch first.

git checkout master
git svn rebase

Now it gets interesting. We're going to switch back to our branch, and once again use rebase. To be more specific, rebase means to take your current set of commits all the way back to the last rebase, REMOVE them from the branch, "rebase" the branch as specified, and reapply all our commits. This is good stuff!

git checkout mybranch
git rebase master

git has now reapplied your changes on top of the latest svn. I told you it was cool!

We've now handled our first set of requirements.



tbc...