Code Monkey home page Code Monkey logo

git-worktree-wrapper's Introduction

git-worktree-wrapper

This wrapper script features an API for git-worktree commands in order to easily create, switch and delete worktrees of bare repositories using commands you already know: git checkout and git branch.

Quiet built-in checkouts are made after each overridden checkout to trigger post-checkout hooks.

Table of Contents

Installation

Wrapper script

Clone this repository

git clone https://github.com/lu0/git-worktree-wrapper
cd git-worktree-wrapper

Link git-wrapper-script to your local PATH

ln -srf git-worktree-wrapper.sh ~/.local/bin/git-worktree-wrapper

Add the following to your ~/.bashrc or ~/.bash_aliases

alias git="source git-worktree-wrapper"

Restart your terminal or re-run bash

bash

Completion rules

Check if your current completion rules autocomplete git after installing the wrapper script. Try git checko + TAB

If your git commands are no longer autocompleted, install complete_alias@3fc67e8.

sudo apt install bash-completion
git clone https://github.com/cykerway/complete-alias ~/.complete-alias
cd ~/.complete-alias
git checkout 3fc67e8
echo ". ${PWD}/complete_alias" >> ~/.bash_completion

Inherit git's completion rules by pasting the following in your ~/.bashrc or ~/.bash_aliases

alias git="source git-worktree-wrapper"
complete -F _complete_alias git

__compal__get_alias_body() {
    local cmd="$1"
    local body; body="$(alias "$cmd")"

    # Overrides
    case "$cmd" in
        "git") body="git"
    esac

    echo "${body#*=}" | command xargs
}

Setup default editor

Set the environment variable EDITOR in your ~/.bashrc, git-worktree-wrapper will try to open worktree directories using this editor.

# Example using vscode
export EDITOR=code

Or set DISABLE_GIT_WORKTREE_EDITOR=1 to disable usage of editors.

export DISABLE_GIT_WORKTREE_EDITOR=1

Usage

Clone and setup a bare repository

Try with this repo!

git clone --bare https://github.com/lu0/git-worktree-wrapper
cd git-worktree-wrapper.git

Setup fetch rules from the remote

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

Open an existing branch, tag or worktree

Switch to branch master:

git checkout master

You can also switch to an existing tag:

git checkout v1.0.0

The script will automatically create the worktree, if it does not exist, and cd into it, even if you are cd'd into another worktree/branch/tag:

Comparison with vanilla git-worktree

Next commands should be issued to achieve the same functionality described above when git-worktree-wrapper is not installed:

If the branch/tag exists but the worktree doesn't.

cd /path/to/the/root/of/the/bare/repository
git worktree add master
cd master

When the worktree exists:

cd /path/to/the/root/of/the/bare/repository
cd master

Create a new branch or worktree

To create a new branch, just issue the command you already know:

git checkout -b new_branch <from_branch (optional)>
# or use -B to force reset

The script will automatically create a new worktree and cd into it, even if you are cd'd into another worktree/branch/tag:

Comparison with vanilla git-worktree

Next commands should be issued to achieve the same functionality described above when git-worktree-wrapper is not installed:

When both the branch and worktree don't exist

git branch new_branch
cd /path/to/the/root/of/the/bare/repository
git worktree add new_branch
cd new_branch

When branch or worktree already exist and you want to reset it as git checkout -B would do:

cd /path/to/the/root/of/the/bare/repository
cd new_branch
git checkout -B new_branch <from_branch (optional)>

Delete a branch and its worktree

To delete a branch, just issue the command you already use for "normal" repositories:

git branch -d new_branch # or -D to force removal

The script will delete both the branch and its worktree.

If you are cd'd into the worktree/branch you are deleting, the script will cd you into the root directory of the bare repository.

Comparison with vanilla git-worktree

Next commands should be issued to achieve the same functionality described above when git-worktree-wrapper is not installed:

cd /path/to/the/root/of/the/bare/repository
git worktree remove new_branch
git branch -d new_branch # or -D to force removal

git-worktree-wrapper's People

Contributors

lu0 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

kedwards

git-worktree-wrapper's Issues

`git branch -d` doesn't seem to delete branch/worktree from git cache

Describe the bug
Title

To Reproduce
Steps to reproduce the behavior:

  1. Run git branch -d old_branch or git branch -D old_branch from git root or from the worktree directory.
  2. This doesn't seem to delete the branch and worktree from the git cache, doing git checkout old_branch re-creates the worktree directory with the files of the 'deleted' branch.

Expected behavior
git checkout old_branch should raise an error after such branch has been deleted.

Screenshots
N/A

Desktop (please complete the following information):

  • OS: Linux Mint 20, but tested on Windows 10 and Windows 11, too.
  • Git version: 2.25.1
  • Shell: Bash 5.0.16
  • Repository version: v1.1.0

Additional context
git branch -d/-D old_branch does indeed delete the worktree directory, but it's kept in the cache.

Create worktree if branch already exists

When I want to create a new worktree, the script tries to create a new branch and fails if the branch exists (locally or in the remote).

Update the script to skip branch creation and fetch from the existing branch.

Can't checkout to new fetched branch

Describe the bug
Cannot checkout/switch to branches fetched from the remote, these branches were not created locally.

To Reproduce
Steps to reproduce the behavior:

  1. Let's assume we have an existing repository, cloned as a bare one.
  2. Let someone else (or you from another machine) create and push a new branch, e.g new-from-remote/test.
  3. Fetch all changes into the local repository git fetch --all .
  4. Try to checkout to the new branch git checkout new-from-remote/test.
  5. Throws the following:
    • When checkedout from the root directory of the local repository:
    fatal: this operation must be run in a work tree
    • When checkedout from an existing worktree, lets say master:
    Branch 'new-from-remote/test' set up to track remote branch 'new-from-remote/test' from 'origin'.
    Switched to a new branch 'new-from-remote/test'
    But the current directory stays the same (master).

Expected behavior
After git checkout new-from-remote/test the script should fetch new-from-remote/test, create the new worktree in <repo's root dir>/new-from-remote.test, and cd into it; even from the repo's root directory.

Desktop (please complete the following information):

  • OS: [Linux Mint 20]
  • Git version: [2.25.1]
  • Shell [Bash 5.0.16]
  • Repository version [v1.0.0]

Additional context
I think is a matter of looking for all existing local and remote references, but will need to parse only the names and not the remote specifiers (remote/heads/...)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.