Integrating Git with Vim (vim-fugitive)

This section assumes that you understand the basics of working with Git. If you don't (or it's been a while), see the Quick and dirty version control and Git introduction section.

Tim Pope's vim-fugitive is a plugin that makes sure you don't need to leave Vim to interact with Git. Since you're editing the files in Vim, you might as well take care of dealing with version control of said edits in the editor. The plugin is available from https://github.com/tpope/vim-fugitive.

If you're using vim-plug, you can install vim-fugitive by adding Plug 'tpope/vim-fugitive' to your .vimrc file and running :w | source $MYVIMRC | PlugInstall.

A lot of the commands vim-fugitive provides are a mirror of external Git commands. However, the output is often a lot more interactive. Take git status, for example, invoked by running the following:

:Gstatus

You'll see the familiar git status output in a split window (you may want to make some changes to the source files without committing them, to have some git status output to work with):

Unlike the git status output, this window is interactive. Move your cursor over one of the files (Ctrl + n and Ctrl + p allow you to cycle through files as well). Try some of the supported commands:

    • - will stage or unstage the file
    • cc or :Gcommit will commit the staged files
    • D or :GDiff will open a diff
    • g? displays help with more commands

:Glog opens a history of commits related to the currently open file. Conveniently, the results are displayed in a quickfix window as follows:

Use :copen to pop open the quickfix window. The :cnext and :cprevious commands navigate the quickfix window by displaying different file versions. See the section, Building and Testing – Quickfix Window, to learn more about a quickfix window.

git blame is a command that lets you quickly figure out who changed every line of the file and when. This way, you can blame other developers (or, most often, yourself in the past) for bugs in your code! :Gblame displays interactive git blame output in a vertical split window:

:Gblame displays relevant commit ID, name of the commit author, and commit date and time (hidden in the screenshot) next to each line in the file.

Some useful shortcuts for :Gblame are as follows:

  • C, A, and D resize the blame window up until the commit, author, and date respectively

  • Enter opens a diff of the chosen commit

  • o opens a diff of the chosen commit in a split window

  • g? displays help with more commands

:Gblame is an extremely useful tool for figuring out when things went wrong.

There are even more really handy wrappers provided in this tool, such as the following:

  • :Gread checks out the file straight into a buffer for a preview
  • :Ggrep wraps around git grep (Git provides a powerful grep command that lets you search through tracked files at any moment in time—see https://git-scm.com/docs/git-grep for details)
  • :Gmove moves the files (while renaming the buffers)
  • :Gdelete wraps git remove commands

Don’t forget to use Vim help (for example, :help fugitive) to learn more about the plugins!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.131.13.194