If git --patch is not solving your problems you're not using it enough.

by Raimon Grau

We all know that git has a “weird” command line interface. Inconsistent and yada yada. It’s been already said. But, know what? There’s a flag you can add everywhere it’d make sense and it works. This flag is “-p”.

-p (or –patch if you’re more verbose), is the way to selectively add parts of the file. If you need to split a bunch of changes into different commits, -p will literaly save you hours (or days).


The usual use case for this is when adding files in git.

git add -p will ask for inclusion or exclusion of every set of patches (git tries to be smart about number of lines of every change to know they come from the same change). Still, if the granularity is too big, s will split the hunk in 2, so you can slice it better.


Say you have branch ‘foo’ and somehow there are 2 features in there, and you just want to merge one of them. The feature you want is not boxed in one commit but spread across the branch.

git checkout master # puts you in master branch
git checkout -b half-foo # creates new branch
git checkout -p foo

And you’ll be in the ‘patch mode’. you then can accept the hunks you want to be in the master branch.


This is getting easier already. If you want to partially reset a commit, git reset 0d83b5ccd -p just allows you to do that. If you don’t get what git-reset flags mean. You can look it up in this nice git cheatsheet.


Stash is a feature that some people love and some just don’t use at all. Nevermind. If you’re from the first group and you like to use stash as an extra dish to put french fries apart and eat your meat comfortably and then put fries back to your plate.

git stash -p will let you stash just part of the changes. But you already guessed that, right?

Was this post useful? Stay tunned and wait for more contents in this blog!

Published: March 28 2014

  • category:
blog comments powered by Disqus