30

I've noticed that in Tower (Git client for the Mac) the user can discard changes even line by line. I wonder how could this be done using the command line? or maybe is something special of Tower?

I frequently find myself in this case:

@@ -391,7 +392,7 @@ extern BOOL validateReceiptAtPath(NSString *path);

       NSURL *url = [self fileURL];
        if (url != nil) {
                NSRect readFrame = [self _readPreferenceOfFileAtURL:url];
-               
+
                for (NSScreen * screen in [NSScreen screens]) {
                        NSRect screenVisibleRect = [screen visibleFrame];
                        ...

See how I have one + and one - ? I would like to discard it so my commit has the minimum changes (hence less possibilities of conflicts and easier review)

:)

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
nacho4d
  • 43,720
  • 45
  • 157
  • 240
  • I can understand not wanting them for a review, but I would still recommend checking in whitespace corrections like that so your code base will improve. If they really bug you that much, there's probably a setting in your editor to avoid doing it in the first place (something like "remove whitespace at end of line"). – Karl Bielefeldt Jun 01 '11 at 16:08

4 Answers4

35

This is called interactive staging and can be done using git add -i or git add -p. See the git-add manpage, pro git and the Git Community Book for more information.

EDIT:

To interactively unstage a file, you can use:

git checkout -p HEAD

Also see this SO question: Undo part of unstaged changes in git

Community
  • 1
  • 1
igorw
  • 27,759
  • 5
  • 78
  • 90
  • it's incredible that i've never yet used `git checkout -p `. thanks for leading me to this. – WEBjuju Jul 20 '20 at 14:54
  • This is the correct answer for the OP's question. Just wanted to point something out... This is a great answer if you're trying to undo any changes; however, I'd take caution with this. This will undo the changes in your local file (within hunks, of course). If you intended to **unstage**, but keep the changes in your local file, use [Robert Siemer's answer](https://stackoverflow.com/a/38891603/3993154) instead. – Sometowngeek Jan 20 '22 at 19:58
7

To undo hunks use

git reset --patch

It’s a very hidden feature. You can stage hunk by hunk with git add --interactive, but you can’t unstage this way. git add also features the option --patch which is like --interactive, but goes directly to the “patch” menu point (otherwise you have to hit pENTER).

git reset does not mirror the --interactive option, but has --patch.

Robert Siemer
  • 32,405
  • 11
  • 84
  • 94
2

You can use git add -e to edit your file right before staging it.

Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
1

To interactively unstage a file use:

git add -i

then you will get following options -

*** Commands ***
  1: status       2: update       3: revert       4: add untracked
  5: patch        6: diff         7: quit         8: help

Use 3rd option revert to unstage files

Read more here - https://git-scm.com/book/en/v2/Git-Tools-Interactive-Staging

Hrishikesh Kadam
  • 35,376
  • 3
  • 26
  • 36