9 ms·
Git add -p
- pyre 13y agoDon't be so presumptuous! I've been using it for years.
- agumonkey 13y agoAnd it's been suggested in almost every git talk/slides I've seen. But I'll admit it's so easy to overlook ..
- deleted 13y ago[deleted]
- bru 13y agoThe `-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).
- killercup 13y agoI'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 13y agoThe 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/
- graywh 13y agoThe standard git gui includes this, too.
- Argorak 13y agoI 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 13y agoAnd 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 13y agoHow is this different from git revert? Also, can I assume you mean darcs?
- thinxer 13y agoIs "git add -i" you are looking for?
- Argorak 13y agoNo, because that has me navigating a menu.
- terhechte 13y agoI'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/
- orkoden 13y agoUsing source tree is a lot easier than git -p . I really like it.
- mahmoudhossam 13y agoWindows or Mac only, pass.
- mileswu 13y agoThere'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 13y ago+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.
- golergka 13y agoThis 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.
- cientifico 13y agogit commit -p: The most powerful git feature you're not using yet
- NikhilVerma 13y agoIs 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 13y agoWell, 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 13y agoYou could always SSH +x into your server and run a GUI client that way.
- Ygg2 13y agoWho says people don't use it? I use it because command line is kind of non-customized. But being close to your CLI is never bad. After all, the tool might comprehend git badly. Git CLI rarely makes mistakes about Git.
- philwelch 13y agoI am comfortable with the Git command line and SSH into my development environment.
- rbonvall 13y agoI 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 13y agoAh, 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.
- colinbartlett 13y agoThe most powerful git feature John Kary just learned about. These linkbait headlines drive me crazy.
- rplnt 13y agoI really hate these presuming "you don't know about" (a dozens of other variations) titles. This by itself makes me not want to read the article at all. And I didn't use or know about the feature.
- zedpm 13y agoDoes the top comment in _every_ HN post have to be an argument, criticism, or complaint? If you don't find value in a given post, then just move on. I suspect a lot of folks had indeed never heard of this feature before now. I found this introduction to git add -p to be extremely helpful. I've found myself in the position he describes (wanting to logically separate changes I've made to a file) many times, and I've usually handled it in a sub-optimal manner (making one commit that describes multiple changes).
- FrozenCow 13y agoThe 'problem' is that this tip has been posted a lot on HN/Reddit. It becomes annoying for some people to keep reading about a "git feature you're not using yet". That said, the feature isn't well-known for everyone and it is very useful, so you can only expect negative as well as positive comments.
- chaz 13y agoThe upvotes seem to validate the article's value, even if the topic has been posted in the past.
- Killswitch 13y agoI've been a HN user for 701 days (as of today). This is the first time I've seen this topic here.. Sorry not all of us are on HN 24/7. This is new to me, and I appreciate it being posted, because guess what? I've never known of git add -p. Now I do. But wait, what if the person who posted this did indeed see something similar posted here last year and never posted it? Great, now I won't ever have known this great feature. Like stated before. Don't like the content? Don't upvote it. Move on. The complaining in every thread about these things are more annoying than seeing the same thing posted again after a month or two.
- 0x0 13y agoIt's pretty useful, but it's also fairly easy to end up with an untested commit that doesn't really compile.
- mhw 13y agoFortunately 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 13y agoYou 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).
- Cthulhu_ 13y agoOi I've been using it for years; I feel insulted by your insinuation! (exaggerated sarcasm)
- Munksgaard 13y agoI 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 13y agoIn 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.
- rafekett 13y agohmm. 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.
- piyushpr134 13y agoI am already using it...everyday. Thanks for your assumption though!
- aeon10 13y agoFor 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-git-index/ http://vimcasts.org/episodes/fugitive-vim-working-with-the-g...
- ascotan 13y agoGreat video.
- jchung 13y agoDidn't know about this. Glad you shared it. Thanks John.
- micampe 13y agoWhy 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?
- anton_gogolev 13y agoOh come on. Teeny-weeny flag to a Git command ends up on the front page of HN. Amazing.
- Erwin 13y agoFor 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 13y agoI 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 13y agoif 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-mercurial-queues/ http://stevelosh.com/blog/2010/08/a-git-users-guide-to-mercu...
- reinhardt 13y agoAnd an improved (real) extension: http://mercurial.selenic.com/wiki/CrecordExtension http://mercurial.selenic.com/wiki/CrecordExtension
- compay 13y agoFYI `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 13y agoAnd `git checkout -p` too.
- brandonbloom 13y agoAnd `git log -p` is great for reviewing recent changes by your co-workers too!
- lquist 13y agoCheck 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.
- exelib 13y agoI 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.
- rmc 13y agoAnd if you use git commit -p it combines git add -p and git commit in one command.
- king_phil 13y agoWhy would one use "git add -p" instead of "git add -i"?
- airblade 13y agoFor 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 13y agoI always used 'git add -i' for this. That involved long steps before adding hunks. But this is so shortcut method. Thanks for sharing :)