Simultaneously git add -p and git checkout -p Simultaneously git add -p and git checkout -p git git

Simultaneously git add -p and git checkout -p


If you are using Windows or Mac OS you can use free SourceTree GUI app that allows you to stage or discard each hunk (or even select lines from each hunk) in the diff view.

Stage/Discard hunk


how about typing j - leave this hunk undecided, see next undecided hunk for hunks which you don't want to stage,and after all of these, run git checkout --, then those hunks selected will be staged, and other discarded.

when you are using git add -p, typing ? will show the help

Stage this hunk [y,n,q,a,d,/,e,?]?y - stage this hunkn - do not stage this hunkq - quit, do not stage this hunk nor any of the remaining onesa - stage this and all the remaining hunks in the filed - do not stage this hunk nor any of the remaining hunks in the fileg - select a hunk to go to/ - search for a hunk matching the given regexj - leave this hunk undecided, see next undecided hunkJ - leave this hunk undecided, see next hunkk - leave this hunk undecided, see previous undecided hunkK - leave this hunk undecided, see previous hunks - split the current hunk into smaller hunkse - manually edit the current hunk? - print help


Have you had a look at git gui. It is a utility that comes bundled with git these days and is tries to make it easy to do complex operations which are mix of staging and checkout. You can read about it here

You can choose to stage a hunk and discard the rest of changes. By doing ctrl + j or cmd + j you can checkout the selected file.

I know its much faster to use git from shell, but in complex cases like these, switching between different commands becomes a overhead.