• Stars
    star
    689
  • Rank 65,628 (Top 2 %)
  • Language
    Lua
  • Created over 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

git-worktree.nvim

A simple wrapper around git worktree operations, create, switch, and delete. There is some assumed workflow within this plugin, but pull requests are welcomed to fix that).

Known Issues

There are a few known issues. I'll try to be actively filing them in the issues. If you experience something, and it's not an issue, feel free to make an issue! Even if it's a dupe I am just happy for the contribution.

Dependencies

Requires NeoVim 0.5+ Requires plenary.nvim Optional telescope.nvim for telescope extension

Getting Started

First, install the plugin the usual way you prefer.

Plug 'ThePrimeagen/git-worktree.nvim'

Next, re-source your vimrc/init.vim and execute PlugInstall to ensure you have the plugin installed.

Setup

Repository

This repository does work best with a bare repo. To clone a bare repo, do the following.

git clone --bare <upstream>

If you do not use a bare repo, using telescope create command will be more helpful in the process of creating a branch.

Debugging

git-worktree writes logs to a git-worktree-nvim.log file that resides in Neovim's cache path. (:echo stdpath("cache") to find where that is for you.)

By default, logging is enabled for warnings and above. This can be changed by setting vim.g.git_worktree_log_level variable to one of the following log levels: trace, debug, info, warn, error, or fatal. Note that this would have to be done before git-worktree's setup call. Alternatively, it can be more convenient to launch Neovim with an environment variable, e.g. > GIT_WORKTREE_NVIM_LOG=trace nvim. In case both, vim.g and an environment variable are used, the log level set by the environment variable overrules. Supplying an invalid log level defaults back to warnings.

Troubleshooting

If the upstream is not setup correctly when trying to pull or push, make sure the following command returns what is shown below. This seems to happen with the gitHub cli.

git config --get remote.origin.fetch

+refs/heads/*:refs/remotes/origin/*

if it does not run the following

git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"

Options

change_directory_command: The vim command used to change to the new worktree directory. Set this to tcd if you want to only change the pwd for the current vim Tab.

update_on_change: Updates the current buffer to point to the new work tree if the file is found in the new project. Otherwise, the following command will be run.

update_on_change_command: The vim command to run during the update_on_change event. Note, that this command will only be run when the current file is not found in the new worktree. This option defaults to e . which opens the root directory of the new worktree.

clearjumps_on_change: Every time you switch branches, your jumplist will be cleared so that you don't accidentally go backward to a different branch and edit the wrong files.

autopush: When creating a new worktree, it will push the branch to the upstream then perform a git rebase

require("git-worktree").setup({
    change_directory_command = <str> -- default: "cd",
    update_on_change = <boolean> -- default: true,
    update_on_change_command = <str> -- default: "e .",
    clearjumps_on_change = <boolean> -- default: true,
    autopush = <boolean> -- default: false,
})

Usage

Three primary functions should cover your day-to-day.

The path can be either relative from the git root dir or absoulut path to the worktree.

-- Creates a worktree.  Requires the path, branch name, and the upstream
-- Example:
:lua require("git-worktree").create_worktree("feat-69", "master", "origin")

-- switches to an existing worktree.  Requires the path name
-- Example:
:lua require("git-worktree").switch_worktree("feat-69")

-- deletes to an existing worktree.  Requires the path name
-- Example:
:lua require("git-worktree").delete_worktree("feat-69")

Telescope

Add the following to your vimrc to load the telescope extension

require("telescope").load_extension("git_worktree")

Switch and Delete a worktrees

To bring up the telescope window listing your workspaces run the following

:lua require('telescope').extensions.git_worktree.git_worktrees()
-- <Enter> - switches to that worktree
-- <c-d> - deletes that worktree
-- <c-f> - toggles forcing of the next deletion

Create a worktree

To bring up the telescope window to create a new worktree run the following

:lua require('telescope').extensions.git_worktree.create_git_worktree()

First a telescope git branch window will appear. Presing enter will choose the selected branch for the branch name. If no branch is selected, then the prompt will be used as the branch name.

After the git branch window, a prompt will be presented to enter the path name to write the worktree to.

As of now you can not specify the upstream in the telescope create workflow, however if it finds a branch of the same name in the origin it will use it

Hooks

Yes! The best part about git-worktree is that it emits information so that you can act on it.

local Worktree = require("git-worktree")

-- op = Operations.Switch, Operations.Create, Operations.Delete
-- metadata = table of useful values (structure dependent on op)
--      Switch
--          path = path you switched to
--          prev_path = previous worktree path
--      Create
--          path = path where worktree created
--          branch = branch name
--          upstream = upstream remote name
--      Delete
--          path = path where worktree deleted

Worktree.on_tree_change(function(op, metadata)
  if op == Worktree.Operations.Switch then
    print("Switched from " .. metadata.prev_path .. " to " .. metadata.path)
  end
end)

This means that you can use harpoon or other plugins to perform follow up operations that will help in turbo charging your development experience!

Made with fury

All plugins are made live on Twitch with love and fury. Come and join!

More Repositories

1

harpoon

Lua
6,501
star
2

vim-be-good

vim-be-good is a nvim plugin designed to make you better at Vim Movements.
Lua
3,200
star
3

init.lua

Lua
3,166
star
4

.dotfiles

Perl
2,971
star
5

refactoring.nvim

The Refactoring library based off the Refactoring book by Martin Fowler
Lua
2,816
star
6

kata-machine

TypeScript
1,267
star
7

ts-rust-zig-deez

Java
605
star
8

htmx-lsp

its so over
Rust
602
star
9

anime

The repo that everyone deserves
546
star
10

tyrone-biggums

Clearly a repo about websockets and their comparison...
ReScript
462
star
11

vim-royale

Because Nano sucks
Rust
410
star
12

neovimrc

Lua
341
star
13

vim-apm

Vim APM, Actions per minute, is the greatest plugin since vim-slicedbread
Lua
324
star
14

undefined

A project to turn a file of JSON responses into TypeScript types
TypeScript
282
star
15

fem-algos

FrontEnd Master algorithms!
JavaScript
271
star
16

CHADstack

Dockerfile
257
star
17

ansible

Dockerfile
244
star
18

vim-with-me

Go
242
star
19

keyboards

239
star
20

rust-for-typescript-devs

JavaScript
212
star
21

vim-fundamentals

JavaScript
184
star
22

ThePrimeagen

149
star
23

fem-htmx-proj

Go
139
star
24

primestack

Rust
135
star
25

dev

my next gen build for starting my system
Shell
121
star
26

vmrss

Shell
92
star
27

web3-smart-contracts

JavaScript
84
star
28

BunSpreader

We spread the buns
Zig
73
star
29

fem-algos-2

The Last Algorithm Class You Want
JavaScript
70
star
30

htmx_golang

Go
67
star
31

jvim.nvim

A simple json traverser for NeoVim
Lua
67
star
32

aoc

2020
Rust
65
star
33

fem-git

JavaScript
64
star
34

vimrc

64
star
35

yew-have-ligma

Elixir
62
star
36

orgwasm

CSS
61
star
37

2-simple-steps

Its literally that simple
TypeScript
59
star
38

js-perf-example

TypeScript
53
star
39

neural-js

A kick-ass neural network for javascript
JavaScript
47
star
40

vim-nav-playground

C
43
star
41

coin-toss-me-daddy

Rust
42
star
42

htmx-class-template

The starter template with server in go or rust
Rust
40
star
43

lsp-debug-tools.nvim

this probably isn't the droid you are looking for
OCaml
35
star
44

leftPadDeez

nuts
JavaScript
35
star
45

yt

All my yt videos that require to have some codes.
TypeScript
34
star
46

fem-htmx

JavaScript
33
star
47

game-of-life-vwm

Go
33
star
48

rusty-arduino

Rust
31
star
49

titty-sprinkles

Yes... This is the name for my NodeConfEU project
TypeScript
30
star
50

htmx-subscribe

HTML
30
star
51

test-these-besties

Go
30
star
52

htmx

Rust
28
star
53

jpegdegens

TypeScript
26
star
54

public-edging

Rust
26
star
55

beat-me-daddy

I put my sonic in my pi
Rust
26
star
56

ts-go-rust

JavaScript
26
star
57

go-vs-rust

The greatest cli comparison ever created
Elixir
26
star
58

uhh

When you keep forgetting those sweet sweet sweet sweet commands.
Go
25
star
59

best-of-stackoverflow

A DRAMATIC READING OF STACK OVERFLOW
24
star
60

objects-to-buffer

TypeScript
23
star
61

bun-vs-node

TypeScript
22
star
62

tmux-sessionizer

Shell
21
star
63

vim-arcade

21
star
64

fem-jsperf

JavaScript
20
star
65

rust-wasm-serverless

Rust
20
star
66

json-vs-proto

TypeScript
20
star
67

gspot

Rust
18
star
68

no-flap-november

the greatest
Go
18
star
69

ts-go-rust-projector

TypeScript
17
star
70

ocaml-aoc

OCaml
16
star
71

i-fixed

16
star
72

shooter-js

TypeScript
15
star
73

vim-deathmatch

Rust
15
star
74

milo

TypeScript
15
star
75

big-chungus

is amungus
TypeScript
14
star
76

real-prog-dvorak-zmk

Rust
14
star
77

zig-deez-structures

Zig
14
star
78

this-isnt-rust

There is no rust in this suppository
TypeScript
13
star
79

git-bisect

JavaScript
13
star
80

tree-navigation

Lua
12
star
81

more-htmx-eploration

Rust
12
star
82

javascwipt-performance

suck it piq
TypeScript
12
star
83

your-first-plugin

Example NeoVim Lua Plugin
Lua
11
star
84

projector

Project my config into your prebuild
Go
11
star
85

why-are-promises-slow

they are
11
star
86

cargo-chadr

Rust
11
star
87

chat-js

Rust
10
star
88

tier-list

HTML
10
star
89

he-uses-tabs

Rust
8
star
90

neovim-irc

C
8
star
91

jest-mem-test

JavaScript
8
star
92

the-great-sonnet-test

Go
7
star
93

rxjs-examples

JavaScript
7
star
94

ansible-dev-prod

Dockerfile
7
star
95

rfceez

Lua
7
star
96

todo

yes
C++
6
star
97

first-nvim-plugin

The template for writing your first nvim plugin
6
star
98

first-deno-project

JavaScript
6
star
99

fizzbuzz

A real programmers fizzbuzz
JavaScript
6
star
100

99-ocaml-problems

6
star