Wednesday 14th January 2009
Grabbing changes from another branch
Reusing our earlier example, let's say we have a master branch, and we have created a caching branch. After we've made some bug fixes to the master branch, we'll probably want those changes to be reflected in the caching branch too. We could think of the caching branch as a series of patches on top of a particular commit in the master branch. In this case, we want to modify the caching branch so it now applies those patches to the HEAD of master. This is exactly what git rebase will do.
In this case, with caching checked out, the command we want to execute is:
git rebase master
git will then undo all the changes made in the caching branch, and use HEAD of master as our new starting point. It will then try to reapply all of the changes made in the caching branch. If git has any problems, it will stop and tell you what the problem is and how to fix it. In general, you can rebase onto any commit like so:
git rebase <my-commit>
But what about when we don't want to rebase, but want to grab all the changes from some other branch? For instance, if we want to merge the changes of caching into master, it doesn't make much sense to rebase master on caching. Instead, we use git-pull. This time around, we want to use the command (with master checked out):
git pull . caching
The dot indicates that we're using the repository in the current directory, and the caching is the branch that contains the changes we want to pull in.
In some cases, we don't want to grab all of the changes from a particular branch. Instead, you want to apply just a single patch. In this case, you can use git cherry-pick. Say some commit <my-commit> in another branch fixed a bug that you have in your branch, but you don't want to pull in all the other changes from the other branch. In this case, you could use the command:
git cherry-pick <my-commit>