In reviewing the magit project and its code, clearly the magit project opinion is to not set global configuration of the git directory, and with clear reasons. (#2939, #3516)
That said, this is your Emacs, and RMS et al want you to hack it. If you are running an Emacs session exclusively for the purpose of editing a repository with a nonstandard git directory, then magit's magit-git-global-arguments
configuration provides a suitable hook.
;;; Configure magit to use .myconf (and not .git) as the git
;;; directory when a .myconf directory is found in the current
;;; working directory (which Emacs calls its `default-directory'
;;; per buffer) and there is no .git directory.
;;;
;;; For example, if dotfiles are tracked with a git directory at
;;; $HOME/.myconf, and the Emacs process starts from $HOME (and
;;; nothing in its configuration changes its `default-directory'
;;; in the current buffer), this will configure magit to use
;;; .myconf as the git directory.
;;;
;;; NOTE: This setting will apply for the entire Emacs process,
;;; regardless of magit invocation in other directories.
(unless (boundp 'myconf-magit-hook?)
;; Only run this hook once, even if Emacs reloads configuration.
(eval-after-load 'magit
'(let ((myconf-path (expand-file-name ".myconf")))
(when (and (file-exists-p myconf-path)
(not (file-exists-p ".git")))
;; Insert git directory and working tree into magit's git
;; global arguments, while preserving magit's existing
;; command-line settings; `add-to-list' adds to the
;; beginning of the list.
(add-to-list 'magit-git-global-arguments
(format "--work-tree=%s"
;; Drop trailing slash.
(directory-file-name
;; Get directory part (`dirname`).
(file-name-directory myconf-path))))
(add-to-list 'magit-git-global-arguments
(format "--git-dir=%s" myconf-path)))))
(setq myconf-magit-hook? t))
I tested this on GNU Emacs 26.1 with Magit 2.90.1, and I made it a point to avoid any Emacs Lisp features added in the past few years.
It would be convenient to configure magit to look for a nonstandard .git directory in the current buffer's default-directory
, but short of monkeypatching (or a fork / pull request), I don't know of a way to configure that (given how global variables are used). Note that magit's design is intended to support multiple repositories within the same Emacs process, and has first-class interactions for a list of repositories.
I have been tracking $HOME
files with git and a nonstandard git directory for 9 years. The use case: It's extremely common to run git
in a subdirectory of $HOME
and a nonstandard git directory for dotfiles will ensure that git
never accidentally runs against the $HOME
repository when there's no intermediate .git
directory when git
walks up the directory tree. It's clear from the OP's alias command that this is exactly the use case.
Personally, I run an Emacs session per project.
magit-git-global-arguments
as a directory-local variable may be an option.magit
mv .cfg .git
, use Magit,mv .git .cfg
.