How can I discard file change from fugitives status window?

34

3

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

tidbeck

Posted 2012-02-20T10:22:01.803

Reputation: 1 365

Answers

28

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

This piece of information is located in the help file under :h :Gstatus:

     U     |:Git| checkout HEAD

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

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

Anson

Posted 2012-02-20T10:22:01.803

Reputation: 396

If that's undocumented it could be construed unsafe. Wouldn't it be tough to find out you clobbered your local changes because of an undocumented magic keystroke? – sehe – 2015-02-07T23:50:50.437

22019 Update now It is X – Oguz Bilgic – 2019-02-08T21:55:14.670

anyone with edit privilege, kindly edit it to X? – chriz – 2019-09-08T13:07:13.430

15

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.

Chris Johnsen

Posted 2012-02-20T10:22:01.803

Reputation: 31 786

Mapping for gstatus to revert file : au FileType gitcommit nmap <buffer> U :Git checkout -- <c-r><c-g><cr> – majkinetor – 2014-03-31T11:16:41.090

1

Mapping for gstatus to revert file:

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

majkinetor

Posted 2012-02-20T10:22:01.803

Reputation: 510

this works great, only downside is that I will get the dialog that the file has changed on disk and if I want to reload it. Would be nice if it was silent. – tidbeck – 2014-04-01T09:09:12.733

1That is even better IMO as you get confirmation that the file is actually reverted. But automatic silent reload can be done with 'autoread' option (:help autoread). Other then that you could bufdo e within above au. – majkinetor – 2014-04-01T11:27:51.757

0

Well, did you try :help fugitive?

Apparently no, there's no shortcut for this Git feature.

romainl

Posted 2012-02-20T10:22:01.803

Reputation: 19 227

Yes I did. There have been times I have not found a feature in the help that have been present never the less. Is your opinion that you should never ask about a feature that is not in the help for the program/plugin? – tidbeck – 2012-02-20T16:23:00.783

Yes it happened to me as well: there was a function that I was able to :call but didn't have a command associated. I don't remember the name of the plugin… Align? whatever, my opinion is that one should read the docs before asking around. Which you did. So that's cool. But the logical second step, after reading the docs, would be IMO to look at the source of the plugin and see if there's some useful function there. Which you should do. But, AFAIK, fugitive doesn't deal with git checkout at all so your chances are slim. You'll probably need to fall back to :!git checkout -- filename. – romainl – 2012-02-20T18:23:35.650

Ok, thank you for the clarification. I think you make a valid point regarding taking a look at the source code but at the same time if the information only is available in the source code it could be a valid QA here. Will accept your answer and continue using :Git checkout -- %. – tidbeck – 2012-02-20T19:23:55.440

0

Though it's got nothing to do with vim, thought of telling you that I sometimes do:

$ git status
...
#
#   modified:   .rvmrc
#   modified:   app/views/admin/base/index.html.erb
#   modified:   config/routes.rb
#
...

$ # mouse-copy the files i want to reset, and paste them into the next command

$ cat | xargs git checkout
app/views/admin/base/index.html.erb
config/routes.rb
^D

finishing the job quickly.

Valer

Posted 2012-02-20T10:22:01.803

Reputation: 101