
Git add -p - robin_reala
http://johnkary.net/blog/git-add-p-the-most-powerful-git-feature-youre-not-using-yet/
======
terhechte
I'm using SourceTree [1] as my main Git UI when I'm not in the terminal, and
it makes it very easy to stage only parts of a changed file. Just as the
author describes, this feature really improves the quality of the commits. It
also makes it easy to remove parts of the changes in a file that were only
there for debugging purposes - prior to the commit without having to go back
to the editor.

It is a wonderful feature, and the fantastic thing about the SourceTree UI is
that it shows it by default making it really easy for everybody who uses the
app to only commit the actual, relevant changes.

[1] [http://www.sourcetreeapp.com/](http://www.sourcetreeapp.com/)

~~~
mahmoudhossam
Windows or Mac only, pass.

~~~
tootie
Git command line and Linux. The dream setup for masochists.

~~~
pyre
This is not needlessly inflammatory how? What does this add to the discussion?

~~~
tootie
You're probably right, but git just drives me insane. And so does the "no true
Scotsman" approach to development.

------
colinbartlett
The most powerful git feature John Kary just learned about. These linkbait
headlines drive me crazy.

~~~
revscat
I, too, see stories on HN that are passing on information that I was already
aware of. Instead of commenting on their obviousness, though, I just move on
to the next story and let those who did not previously know enjoy their new
found knowledge. I don't presume myself their better because I happened to be
aware of it first.

Also, relevant XKCD:

[http://xkcd.com/1053/](http://xkcd.com/1053/)

~~~
sltkr
I think the complaint is about the presumptuousness of the title of the post,
not so much the obviousness of the content.

~~~
stanmancan
This title isn't even really link bait. It tells you the feature in thw title.
If you know what the "p" flag does, no need to click on the link to find out.
For those that don't know of the feature, it is enticing enough to get them to
check it out

~~~
xmlninja
Im mostly disturbed by the "most powerful" part because this is subjective to
project and how you work. It's not a very common case for me to commit parts
of changes in a file because in most of my cases all changes I do to a file
belong to the same commit since I work with feature branches. However when I
do need this it's indeed powerful but judging by the enthusiasm of the post
and title it just smells like the endorphin release of new feature
discovering.

~~~
ryansouza
Even with topic branches, I find staging with -p to be a nice chance to review
the diff piece by piece. Helps me build good commit messages, and makes it
easy to split changes in to a set of commits if I feel thats necessary.

------
Argorak
I love -p, but it still falls short of darcs interactive commit (which it is
definitely inspired by).

darcs interactive commit allows you:

\- to add files in the same process (in two steps: intent-to-add and then the
file itself) \- revert of hunks, file additions, etc.

Yes, you can do both through separate steps (git checkout -p for example), but
darcs interactive was a quick way of incrementally building full patches in
one workflow.

~~~
lelf
And this is how pretty every command in darks works. So it really encourages
you to commit small and independent patches. And after darks will easily do
its magic.

Want to revert some one week-old change?

    
    
      darks rollback -p "That change name"
    

As easy as it gets

~~~
michaelmior
How is this different from git revert? Also, can I assume you mean darcs?

------
killercup
I've been using this for quite some time now, but only because GitX[1] has
nice GUI support for this. (I'm too lazy to learn using this with git's CLI.)

[1]: [http://rowanj.github.io/gitx/](http://rowanj.github.io/gitx/)

~~~
phoboslab
The GitHub App for Mac[1] and Windows[2] as well as msysgit[3] (and probably
others) have this feature as well. I always resort to these GUIs when only
commiting parts of a file, because it's a very visual process and I believe a
GUI is the right tool in this case.

[1] [http://mac.github.com/](http://mac.github.com/)

[2] [http://windows.github.com/](http://windows.github.com/)

[3] [http://msysgit.github.io/](http://msysgit.github.io/)

------
rbonvall
I bet _these_ are the features you're not using yet:

    
    
        git checkout -p
    

for discarding hunks from the worktree, and

    
    
        git reset HEAD -p
    

for unstaging hunks from the index.

~~~
bluetech
Ah, the 'git checkout -p' is great. I always did 'git stash -p; git stash
drop' for that. Putting this under 'checkout' is completely non-obvious.

------
pyre
Don't be so presumptuous! I've been using it for years.

~~~
agumonkey
And it's been suggested in almost every git talk/slides I've seen. But I'll
admit it's so easy to overlook ..

------
bru
The `-p` flag in general is very useful.

With `git add`, sure it allows you to stage part of a file. That not all, when
you're in edit mode (pressed "e" upon the prompt) you just edit a patch. So
you can actually put in your patch lines that are not in the source file. This
can be very convenient e.g. if the 2 sets of change you want to commit
separately are entangled. For example I used it yesterday to correct the level
of indentation.

With `git stash show`, instead of having a short description of files changed
in the stash, you get the patch itself.

etc.

Git man pages are well-written, and I always enjoy reading it. Most of the
time I have one question but end up wiser (at least git-wise).

------
mileswu
There's also a ncurses-based interface for git called tig[1]. I find it a bit
easier to stage chunks/lines than git add -p as it's interactive. When I'm not
on a command line, gitx-dev for OSX [2] also makes it very easy too.

[1] [http://jonas.nitro.dk/tig/](http://jonas.nitro.dk/tig/) [2]
[http://rowanj.github.io/gitx/](http://rowanj.github.io/gitx/)

~~~
andrey-p
+1 for tig. I don't use it for staging and committing (I prefer to do all that
using normal git) but it's ridiculously useful for quickly checking the git
history, blames etc.

------
NikhilVerma
Is it me or does it seem like a lot of people seem to avoid using GUI's for
git ? I mean git-cola, gitk, gitx, sourcetree etc are all great ways to manage
your git repos and don't need complicated command line operations and make
your job much faster/easier.

~~~
colomon
Well, I've got to be able to use git on Windows, Mac, and Linux, the last two
possibly over SSH. Is there a GUI that would work well in all those
environments? Someone just mentioned tig, which might be able to do the job,
but I haven't tested it out yet.

~~~
tomswartz07
You could always SSH +x into your server and run a GUI client that way.

------
0x0
It's pretty useful, but it's also fairly easy to end up with an untested
commit that doesn't really compile.

~~~
mhw
Fortunately git also makes it fairly easy to test only those changes that
you've staged:

    
    
        $ git stash --keep-index
        $ <compile/run tests>
        $ git commit
        $ git stash pop

~~~
pyre
You might want to add this option:

    
    
      -u / --include-untracked
    

You're not testing _only_ the index if you have untracked files lying around
(which could possibly affect things). (Presumably you don't need to use --all
because you're only ignoring files that don't affect things :P).

------
golergka
This feature is the reason I can't completely switch to command-line git from
Sourcetree — it's much more convenient to quickly glance along code changes in
multiple files and decide what to commit with a mouse.

------
Erwin
For Mercurial, there's this "extension":
[http://mercurial.selenic.com/wiki/RecordExtension](http://mercurial.selenic.com/wiki/RecordExtension)

"Extension" in quotes, because it comes with the Hg installation -- it's just
one of those advanced things disabled by default.

~~~
zck
I love mercurial, but you don't get quite the benefit of `git add -p`, because
mercurial has no staging area. If you quit out of `git add -p`, you still get
the benefit of having whatever you added staged. In contrast, if you quit `hg
record` (because you wanted to change something before committing), you have
to re-record all the changes you wanted.

~~~
krupan
if you use mercurial's mq extension you get the equivalent of a staging area
if you really want one[1]. There is qcrecord to get git add -p functionality
with mq.

Personally, I have stopped using queues and I use rebase and histedit
extensively now. I see no need for patch queues or a staging area when you can
edit and rearrange commits easily.

1\. [http://stevelosh.com/blog/2010/08/a-git-users-guide-to-
mercu...](http://stevelosh.com/blog/2010/08/a-git-users-guide-to-mercurial-
queues/)

------
lquist
Check out `git add -e` ([http://pivotallabs.com/git-
add-e/](http://pivotallabs.com/git-add-e/)). Like `git add -p` on steroids.
That said, both have their place depending on what you're trying to do.

------
micampe
Why are people so upset by the title? Is it a cheesy buzzfeed style bait? Yes,
but why should I be offended by a random person on the internet insinuating I
don't know about some random thing?

------
rafekett
hmm. i've used git pretty regularly since 2010, after coming from svn. the
more i use it the more it reminds me of being the administrator of a website
-- sure, i _could_ have a password recovery form, but it's much easier for me
to use this python script i've written that just edits a user's row in the
logins table... that is, i've found git to be incredibly powerful, but in the
process i've forgotten the safe way to do everything.

------
exelib
I use it already. Sometimes. But every time I use it, I think I do something
wrong. Why I use it? Because my changes are more as one commit, because I mess
another changes not related to my actually unit of work.

But if I didn't has git add -p, in svn for example, I create a patch and
revert not related changes, commit, apply patch again. git add -p is just
convient/useful shortcut and not mega-power-answer to all problem nor sources
of problems.

------
aeon10
For the vim users the fugitive plugin by tpope provides a nice way to do this
using vim's vimdiff. It is a delight to use.

Here is a video of vimcasts tutorial. [http://vimcasts.org/episodes/fugitive-
vim-working-with-the-g...](http://vimcasts.org/episodes/fugitive-vim-working-
with-the-git-index/)

------
Munksgaard
I too discovered git add -p some time ago, and it has been very handy!
However, a few days ago, I decided to finally give magit (an emacs extension)
a go, and it really beats everything else I've tried out of the water.

~~~
mziulu
In case you didn't already know, magit exposes `add -p` functionality in a
very emacs-y way: it leverages the region concept by making it so that if you
select the part of a diff you want to stage in your magit-status buffer and
then press s it will only stage that region.

------
jchung
Didn't know about this. Glad you shared it. Thanks John.

------
compay
FYI `git reset -p` works the same way. Sometimes, depending on your work and
workflow it can be easier to add everything and then just exclude a few of the
changes.

~~~
bct
And `git checkout -p` too.

~~~
brandonbloom
And `git log -p` is great for reviewing recent changes by your co-workers too!

------
airblade
For vim users, vim-gitgutter lets you stage/revert individual hunks. I find it
much more useful than I thought I would (even being the plugin's author).

------
chinchang
I always used 'git add -i' for this. That involved long steps before adding
hunks. But this is so shortcut method. Thanks for sharing :)

------
Cthulhu_
Oi I've been using it for years; I feel insulted by your insinuation!
(exaggerated sarcasm)

------
rmc
And if you use git commit -p it combines git add -p and git commit in one
command.

------
king_phil
Why would one use "git add -p" instead of "git add -i"?

------
piyushpr134
I am already using it...everyday. Thanks for your assumption though!

------
cientifico
git commit -p: The most powerful git feature you're not using yet

------
anton_gogolev
Oh come on. Teeny-weeny flag to a Git command ends up on the front page of HN.
Amazing.

------
ascotan
Great video.

