‘git add –p’ is awesome

It lets you stage parts of files for commit, let me show you how.

Intro

I cannot do one thing at a time, I try to, but never succeed. Especially when It comes to coding. All the time I find myself working on a feature, and when jumping around in the code base I find stuff that needs fixing, and I fix it. As a result of this, my commit messages often have a bullet list, or some “and”s in there. I don’t like this at all – but on the other hand, I don’t like commit messages that hide what the commit does. But today, from the InfoQ presentation by Scott Chacon, A tale of three trees, I learnt about “git add –p”. My git mentor at work Philip Nilsson pointed me to it. Let me show you how it works.

The starting point

After some coding

running “git status” on this shows

just as you would expect, the changes are there and unstaged.

The partial stage

So, what if I now only want to stage and commit the fix in method A, not method B. Easy! Just run:

That command will diff you working dir and feed you with each change separately, and ask you what to do with it.

First output:

I’ll answer “y” here, which gives a question on the next change:

 

And here I’ll answer “n”.

The result

Running “git status” now shows the magic:

Some of the changes from my foo.txt file have been staged for commit, but some haven’t.

How neat is that?

Splitting up chunks

When you start using this you’ll find out that git does not always understand what changes should go together. But of course git has options for controlling that! Hitting “?” when choosing what to do with a chunk displays the options for each chunk:

 

The interesting option here is “s” which stands for “split the current hunk into smaller hunks” – which will do just what is says.

This Post Has 3 Comments

  1. And -p stands for –patch

  2. Definitely awesome. Not that surprising that git can do this. But I really wonder if hg and bzr are able to do this too?

Leave a Reply

Close Menu