• Stars
    star
    613
  • Rank 70,687 (Top 2 %)
  • Language
    Lua
  • Created about 3 years ago
  • Updated 4 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
5,055
star
2

init.lua

Lua
2,727
star
3

vim-be-good

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

.dotfiles

Perl
2,611
star
5

refactoring.nvim

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

kata-machine

TypeScript
1,105
star
7

ts-rust-zig-deez

Java
520
star
8

htmx-lsp

its so over
Rust
454
star
9

tyrone-biggums

Clearly a repo about websockets and their comparison...
ReScript
450
star
10

anime

The repo that everyone deserves
448
star
11

vim-royale

Because Nano sucks
Rust
406
star
12

vim-apm

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

neovimrc

Lua
261
star
14

CHADstack

Dockerfile
252
star
15

fem-algos

FrontEnd Master algorithms!
JavaScript
249
star
16

keyboards

239
star
17

undefined

A project to turn a file of JSON responses into TypeScript types
TypeScript
231
star
18

rust-for-typescript-devs

JavaScript
207
star
19

ansible

Dockerfile
203
star
20

primestack

Rust
135
star
21

ThePrimeagen

109
star
22

vmrss

Shell
83
star
23

web3-smart-contracts

JavaScript
83
star
24

BunSpreader

We spread the buns
Zig
74
star
25

vim-with-me

Rust
73
star
26

fem-algos-2

The Last Algorithm Class You Want
JavaScript
68
star
27

jvim.nvim

A simple json traverser for NeoVim
Lua
67
star
28

aoc

2020
Rust
63
star
29

vimrc

62
star
30

yew-have-ligma

Elixir
61
star
31

htmx_golang

Go
57
star
32

orgwasm

CSS
55
star
33

2-simple-steps

Its literally that simple
TypeScript
54
star
34

fem-htmx-proj

Go
47
star
35

coin-toss-me-daddy

Rust
44
star
36

js-perf-example

TypeScript
40
star
37

vim-nav-playground

C
39
star
38

htmx-class-template

The starter template with server in go or rust
Rust
39
star
39

lsp-debug-tools.nvim

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

neural-js

A kick-ass neural network for javascript
JavaScript
34
star
41

leftPadDeez

nuts
JavaScript
34
star
42

yt

All my yt videos that require to have some codes.
TypeScript
32
star
43

rusty-arduino

Rust
31
star
44

titty-sprinkles

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

htmx-subscribe

HTML
28
star
46

htmx

Rust
27
star
47

public-edging

Rust
26
star
48

jpegdegens

TypeScript
26
star
49

beat-me-daddy

I put my sonic in my pi
Rust
26
star
50

ts-go-rust

JavaScript
26
star
51

go-vs-rust

The greatest cli comparison ever created
Elixir
26
star
52

uhh

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

best-of-stackoverflow

A DRAMATIC READING OF STACK OVERFLOW
23
star
54

objects-to-buffer

TypeScript
23
star
55

bun-vs-node

TypeScript
22
star
56

fem-htmx

JavaScript
22
star
57

rust-wasm-serverless

Rust
21
star
58

json-vs-proto

TypeScript
20
star
59

no-flap-november

the greatest
Go
19
star
60

gspot

Rust
18
star
61

fem-jsperf

JavaScript
18
star
62

ts-go-rust-projector

TypeScript
17
star
63

ocaml-aoc

OCaml
16
star
64

i-fixed

16
star
65

shooter-js

TypeScript
15
star
66

vim-deathmatch

Rust
15
star
67

milo

TypeScript
15
star
68

zig-deez-structures

Zig
14
star
69

big-chungus

is amungus
TypeScript
13
star
70

this-isnt-rust

There is no rust in this suppository
TypeScript
13
star
71

your-first-plugin

Example NeoVim Lua Plugin
Lua
12
star
72

tree-navigation

Lua
12
star
73

more-htmx-eploration

Rust
12
star
74

javascwipt-performance

suck it piq
TypeScript
12
star
75

why-are-promises-slow

they are
11
star
76

real-prog-dvorak-zmk

Rust
11
star
77

tier-list

HTML
11
star
78

projector

Project my config into your prebuild
Go
10
star
79

dev

my next gen build for starting my system
Shell
10
star
80

cargo-chadr

Rust
10
star
81

test-these-besties

Go
10
star
82

chat-js

Rust
9
star
83

he-uses-tabs

Rust
8
star
84

neovim-irc

C
8
star
85

git-bisect

JavaScript
8
star
86

rxjs-examples

JavaScript
7
star
87

jest-mem-test

JavaScript
7
star
88

todo

yes
C++
6
star
89

first-deno-project

JavaScript
6
star
90

fizzbuzz

A real programmers fizzbuzz
JavaScript
6
star
91

ansible-dev-prod

Dockerfile
6
star
92

99-ocaml-problems

6
star
93

neovim-irc-ui

Lua
5
star
94

first-nvim-plugin

The template for writing your first nvim plugin
5
star
95

the-hoff

9000
TypeScript
5
star
96

mini-wasm-env

When full wasms just wont due
C
4
star
97

crypto-legends

Like Apex Legends. Just more, better, faster, and less recoil
Go
4
star
98

keyboard

Simple keyboard
JavaScript
4
star
99

fem-vim

Vim Script
4
star
100

flatbuffers-benchmarks

The hello server that will take in either flatbuffs or json and add one to the `count` field, then resend the data back through the socket connection.
JavaScript
4
star