Thursday, January 22, 2009

Stupid Git tricks

I've been using git somewhat for a couple of years now. Until recently, though, I was only a very casual user, and I didn't know the full power of git. I'm now maintaining a git tree at work, and I'm having to learn some of the more powerful features of git. This may be an ongoing thing as I learn more about it, but for now, here are some of the commands that I've had to learn, and that are extremely useful:
  1. git checkout -b
    This is really at the core of git, so it's almost not worth talking about, but I'll start here. This creates a new branch off of the branch you are currently working on, and changes over to that new branch. git is all about working with branches, so the recommended way to do things is to have many "topic" branches; one branch for each different topic you are working on or with. Then later on it's easy to merge branches together, push and pull them remotely, etc.
  2. git commit -a -s
    Commit any outstanding changes to the current branch. This is pretty self-explanatory, except for the fact that -s adds your Signed-off-by line automatically.
  3. git rebase -i HEAD~6
    This is somewhat of a baroque command, but it's so powerful, you'll wonder how you ever lived without it. A "rebase" tells git to checkout some previous version (in this case, HEAD - 6 commits), then replay thte commits on top of that. Where this gets really interesting, however, is with the -i flag; this is an interactive rebase. This allows you to do various operations to the individual commits before they are replayed. Your three options are pick, edit, or squash. "Pick" just means to take the commit as-is. "Edit" means that you want to edit the commit in some way before replaying it. This can be as simple as editing the commit message, or as complex as adding new code into the commit. "Squash" means to take this commit, and merge it into the previous one, so they now look like one commit.
  4. git add --patch
    I actually haven't personally used this one yet, but it was pointed out to me by a co-worker. git add is the command you use when you want to add some changes to a changeset before it is committed. The "--patch" flag allows you to choose just specific hunks of the differences that git finds, so if you have some debugging or whatever left in your tree, you can just automatically throw it away. Very cool.
  5. git merge
    This is a command that lets you merge multiple branches into your current branch. It tries very hard to do automatic conflict resolution; if it has to give up, it leaves you in a place where you can fix up thte conflict by hand, and then continue the merge.
  6. git pull remote_branch local_branch
    This command lets you pull *any* remote branch onto *any* local branch. That means someone can point you to their private repository, and you can pull their changes onto your branch locally. Very handy for combining trees.

No comments:

Post a Comment