How can I discard file change from fugitives status window?

When in the fugitive-plugin status window, accessed using :Gstatus, it's possible to diff changes for a file using D and toggle files for commit using -.

Is there any similar shortcut for discarding changes, with the discard I mean the equivalent of git checkout -- filename?

Update:

Found a feature request on fugitives github page Issue #97: Shortcut to checkout/remove files

According to that the preferred way is using :Gread :w

Update 2:

Since June 2014 it's possible using U as answered by Anson below.

Update 3: Since 3 Jan 2019 the keybinding is mapped to X


Solution 1:

As of 2019:

This functionality is mapped to X. Here's what :h fugitive-staging-maps says about it:

X                       Discard the change under the cursor.  This uses
                        `checkout` or `clean` under the hood.  A command is
                        echoed that shows how to undo the change.  Consult
                        `:messages` to see it again.  You can use this during
                        a merge conflict do discard "our" changes (--theirs)
                        in the "Unstaged" section or discard "their" changes
                        (--ours) in the "Staged" section.

For historical context:

This functionality was added in June 2014 and was by default mapped to U.

Feature request and discussion:
https://github.com/tpope/vim-fugitive/issues/97

Commit:
https://github.com/tpope/vim-fugitive/commit/061a81f247538aeb61e165e1551355f289d52f63

Solution 2:

You can use fugitive’s Gread command to replace the contents of a buffer with various alternate versions of the buffer’s file (i.e. this must be done from a file’s buffer, not from the :Gstatus buffer).

  • :Gread (with no argument) will use the version of the file from the index.
  • :Gread - will use the version of the file from the HEAD commit.

See the documentation at :help fugitive-revision for the list of other revision specifications that fugitive supports (the two above are probably the most immediately useful ones).

The :Gread workflow proceeds like this:

  1. :Gread
  2. fugitive clears the current buffer and reads in the contents from the index
  3. Result: The buffer now has the same contents as the index. The working tree file is not changed.
  4. You can follow up with :w to save the file to the working tree (or use :Gread|w if you know that you will want to save it right away).

The :Git checkout -- % workflow proceeds like this:

  1. :Git checkout -- %
  2. Git copies the version of the file in the index to the file in the working tree.
  3. Vim notices that the file has been changed outside the editor and prompts you to ignore or reload it.
  4. You tell Vim to reload the file.
  5. Result: Both the working tree file and the buffer now have the contents from the index.

Summary: :Gread avoids the “file has changed since editing started” prompt and lets you decide when you want to modify the file in working tree.


When the buffer represents an index stage of the file instead of the file from the working tree, :Gread reads from the contents of the file as it exists on disk in the working tree instead of stage 0 of the index.

Solution 3:

Mapping for gstatus to revert file:

au FileType gitcommit nmap <buffer> U :Git checkout -- <c-r><c-g><cr>