How to use vim-fugitive with a git bare repository?
Asked Answered
S

2

5

Environment

Setup

I use a git bare repository to version my config files. I can use same commands as if I were using a normal git repository just have to include some flags:

git --git-dir=/home/kunzaatko/.cfg/ --work-tree=/home/kunzaatko/ __command__

instead of

git __command__

Usage

I make use of vim-fugitive with normal git repositories mainly for making a big change and adding it in many different commits by staging partially (only a discrete set of hunks/changes) and committing them separately . I use :Gdiff for this for the nice and productive interface I can make use of.

Desire

I want to do this with my config git bare repository.

What I tried:

  1. Renaming the repository to .cfg.git. This didn't make any change. issue that suggests this should work

  2. I tried to change the b:git_dir internal variable of git-fugitive:

:let b:git_dir=/home/kunzaatko/.cfg/
  1. Changing the working directory to the git directory for fugitive to recognize that it is a git repo:
:chdir /home/kunzaatko/.cfg/

What would be worthy of trying if I knew how:

  1. I think that there may be a way to use the git submodule command to put the bare repo into scope. The problem with that is where to put the root of the git repository... issue that I base this possibility of of

Question

Is there a way to use a git bare repository with git-fugitive?

(or any other suggestion that would solve my use-case)

Safeconduct answered 26/11, 2020 at 11:26 Comment(0)
P
8

I agree with @okket, but there is a problem when I use that method.

When in Gstatus window, I cannot get the status of changed files. And I find the reason is that fugitive gets the core.worktree attribute from the GIT_DIR repository.

So for me, the viable way to do this is as follows:

  1. Same as @okket says, set GIT_DIR env variable when using vim/nvim command (GIT_WORK_TREE may be omitted):
GIT_DIR=$HOME/.cfg GIT_WORK_TREE=$HOME [n]vim

For users of fish shell (like me), should use env command:

env GIT_DIR=$HOME/.cfg GIT_WORK_TREE=$HOME [n]vim
  1. Set core.worktree for your git repository:
git --git-dir=$HOME/.cfg --work-tree=$HOME config --local core.worktree $HOME

You can make sure you get it right with the following command:

git --git-dir=$HOME/.cfg --work-tree=$HOME config --local core.worktree

The output should be your HOME path.

  1. If you get the git repository by git clone --bare ..., you need to unset core.bare to avoid git status error warning: core.bare and core.worktree do not make sense:
git --git-dir=$HOME/.cfg --work-tree=$HOME config --unset core.bare

And there is one more thing to notice for this method. You cannot use fugitive outside of $HOME directory where you will get fugitive: working directory does not belong to a Git repository error.

PS: I'd like to comment on @okket 's answer, but due to low reputation I can only post a new answer here.

Pseudaxis answered 14/3, 2021 at 12:8 Comment(0)
C
3

The environment variables $GIT_DIR and $GIT_WORK_TREE let you point git and fugitive to the dotfile directory when using a bare repository. This solution works with the caveat, that fugitive/git will ignore other repositories while these variables are set.

To limit this environment pollution start vim like this:

GIT_DIR=$HOME/.cfg GIT_WORK_TREE=$HOME vim [file(s)]

In case the syntax is unfamiliar: it is possible to prepend setting environment variables before calling a binary in most (all?) shells. This limits redirecting git through the environment variables to this specific vim instance.

A quality of life improvement would be to put this line into a shell alias or use a full featured dotfile manager like yadm, which is a wrapper around a git bare repository.

Cysticercus answered 10/2, 2021 at 16:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.