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:
:Gread
- fugitive clears the current buffer and reads in the contents from the index
- Result: The buffer now has the same contents as the index. The working tree file is not changed.
- 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:
:Git checkout -- %
- Git copies the version of the file in the index to the file in the working tree.
- Vim notices that the file has been changed outside the editor and prompts you to ignore or reload it.
- You tell Vim to reload the file.
- 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.
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.670anyone with edit privilege, kindly edit it to X? – chriz – 2019-09-08T13:07:13.430