Today, I will show you how to organize git workflow in your Vim/Neovim editor. It doesn’t have any features for version control out of the box, but we can easily expand it with plugins.
First of all, upgrade your Vim/Neovim to the latest version and install the package manager. I will use vim-plug.
Let’s start with vim-fugitive plugin. For installation, add it to the plugins list.
After this, save and reload the file. You can use source ~/.config/nvim/init.vim
command. Once the file is reloaded, run the :PlugInstall
command.
This plugin allows us to interact with git using command mode. For example, we want to run git status
command.
Instead of opening the terminal using :ter
or opening a new window with the terminal, we can easily run :G
. In the same way, we can run :Gw
for adding changes and :Gcommit
for commit.
One more advantage of this plugin is the convenient management of windows. For example, we want to see who committed to the file and when. You can run :Gblame
and get a list of authors. List of all commands you can find in the repo.
The next plugin is airblade/vim-gitgutter. It shows which lines of code were modified, removed, or added. It has more features, and you can read about them in the documentation.
Just add this line of code to install plugin.
Then, you can try :GitGutterEnable
command to activate plugin and show information. For convenience, you can add autocmd BufWritePost * GitGutter
to our configuration. After saving the file, the information will be updated automatically.
Let’s consider a tiny plugin that allows us to show git information — rhysd/git-messenger.vim.
It shows the history of commits under the cursor in a popup window. By default, it shows only the last commit, but you can quickly see older commits or even diff. You can install this plugin like the previous one. Just add this line to the config.
For more information, visit official documentation.
The following plugin is mobile/undo tree. It’s a simple and small plugin that allows us to move around buffer history. It’s a really straightforward plugin, and we will not stop here. You can explore documentation if you want to dive deep into this plugin.
To install the plugin, you should put this line into your configuration.
Before we continue, let’s add this line to our config file nnoremap <F5> :UndotreeToggle<cr>
.
After this, you can press F5 to open the window with buffer history.
The last plugin we will look at is vim-airline/vim-airline. It has many features but should be familiar to everybody who uses a modern code editor or IDE. It creates a line at the bottom with git-related information, such as the current git branch. The best way to understand all the features is to explore the documentation.
These plugins will improve your git workflow, but we still have an unresolved conflict topic. Modern editors have it out of the box, but Vim doesn’t.
To solve this problem, you can install a console tool like Vimdiff
or use a third-party GUI, such as P4Merge
. You should install it and add it to the global git config. If you use MacOS or Ubuntu, you can use the following commands
Then, try to create a conflict and pull changes. If everything is specified correctly, the p4merge window will open, where you can eliminate conflicts.