1

Windows 10, Emacs 25.1 , Magit

I have many git repositories.

Does the notion of the currently selected repository exist in Magit?

  • 2
    Magit automatically acts upon the repository which is appropriate for the currently selected buffer. There is no such thing as the "currently selected repository" if you invoke a Magit command from a buffer which is not associated with a repository. – phils Oct 19 '17 at 10:45
7

Yes, Magit knows what the current repository is. That means that you can, for example, have multiple status buffers, each showing the status of a certain repository. When you invoke magit-status, Magit will automatically show the status for the current repository, either creating a new buffer or switching to an existing buffer for that repository.

The current repository is determined by asking Git what the repository for the buffer-local value of default-directory is. So you can invoke a Magit command and it will always do its thing in the right repository, i.e. in the directory the current buffer "is in". Like in a shell where git also acts on the current repository based on the shell's working directory ($PWD).

If the default-directory is not located inside a Git repository then you will likely get an error. Some commands will instead ask about creating a new repository or switching to an existing repository.

If you need that information programmatically, then you can use the function magit-toplevel to get the top-level directory of the current repository's working directory. Or you can use magit-git-dir to get the location of the .git directory (which might be located outside the working tree).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.