Git worktree wrapper for neovim
After using git-worktree.nvim plugin for quite some time I decided to make my own git worktree plugin with a different api and flow/usage.
- neovim nightly (0.7+)
- plenary.nvim
- telescope.nvim (optional)
Install the plugin and dependencies with prefered plugin manager
use("nvim-lua/plenary.nvim")
use({
"Juksuu/worktrees.nvim",
config = function()
require("worktrees").setup()
end,
})
All the commands and functions this plugin provides utilizes the vim.fn.input function to ask users for required or optional parameters. Optional parameters are indicated with (optional) in the input prompt
New worktree can be created using the provided command GitWorktreeCreate
:GitWorktreeCreate
or with lua
:lua require("worktrees").new_worktree()
If a file is open in a buffer when switching, the plugin will try to find the file in the other worktree, if it exists it will change the buffer to correspond to the new worktree file. Otherwise Ex is opened (will most likely change this to be configurable)
Switching can be done using the provided command GitWorktreeSwitch
:GitWorktreeSwitch
or with lua
:lua require("worktrees").switch_worktree()
Creating worktree for existing branch can be done with the provided command GitWorktreeCreateExisting
:GitWorktreeCreateExisting
or with lua
:lua require("worktrees").new_worktree(true)
The extension can be loaded with telescope
require("telescope").load_extension("worktrees")
require("telescope").extensions.worktrees.list_worktrees(opts)
-- <Enter> - switches to that worktree
This plugin provides logging to a file which can be used to debug bugs etc.
The log file resides in neovims cache path and the logging level can be changed by changing the log_level
option in setup. Status logs in neovim messages can also be toggled in the options
require("worktrees").setup({
log_level = <one of vim.log.levels> -- default vim.log.levels.WARN,
log_status = <boolean> -- default true
})
For this plugin to work correctly the upstream fetch config needs to be setup correctly. This seems to not be the case when using bare repositories. To check if it is setup correctly run the following command and check that it returns as shown below
git config --get remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
If not run the following command to fix it
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
- Possibly use vim.fn.input in commands and functions instead of parameters
- Adding telescope prompt for selecting base branch or branch to track (would work well with using vim.fn.input)
- Options to customize behavior