Sunday, October 5, 2008

git add --patch gives me an erection

One of the reasons I tossed out my playboys and husslers in favour of the git man pages is git add --patch (or git add -p)

To say something like
git add -p is the fucking bomb, I wish it was a pony so I could ride it around the backyard and pet it and feed it straw all day long
would be the understatement of the new millenium.

If you use git and aren't familiar with git add -p, then take off your pants and read this raving immediately.

OMG wtf is it?!!?


git add --patch lets you cherry pick the changes you want to add to the index. This means you can make 10 changes to a file, then only add 5 of them to the next commit.

I dunno about you, but when I'm hacking the mainframe I can never seem to stick to one task. I always notice some bug, or some fucked up white space, or have to hack something else to get the current task done etc. Its nigh fucking impossible for me to keep the changes I make suitable for one atomic commit.

So I'm a bad boy. I don't care. Smack my ass and tell my parents. Just make sure you have breasts and a vagina if you do, or I'll bust your fucking chops. Hell, you can even skip the vagina if your breasts are exceptional, but don't try and tell me your working directory doesn't get tangled as fuck too.

How do you use it?


It's easy. Just go git add -p file1 file2 ... or just git add -p to work with all tracked files. This launches a program that will loop through all the changed hunks one by one asking you whether you want to stage them or not. It looks a lil sumthang like this:



For every hunk, it prints out a diff and asks you what to do with it. You can hit ? to see the choices you have:


To quit just hit ctrl-c but be aware that the hunks you added will still be in the index.

Other useful tidbits


git add -p is actually a sub routine of the git add --interactive program. Sometimes it's faster to use git add --interactive (aka git add -i) to select which files you want git add -p to work with rather than entering them in on the command line.

Remember to do a git diff --cached to before you do the final git commit to make sure you didn't add anything extra by mistake.

If you do add something by mistake then use git reset HEAD path/to/file to remove the changes from the index.



Have a nice day.

2 comments: