How to change repo with magit emacs?
You can call magit-status
with prefix-arg (C-u), it will prompt you for git repository. So if you keybinding for magit-status
is C-xg, you can do C-uC-xg, it will prompt for git repo.
You can also set the variable magit-repo-dirs
to a directory (do C-hfmagit-repo-dirs
RET to know more) and it will prompt you for all git repositories in magit-repo-dirs
.
If you use projectile you can use the fact that it remembers your project directories and use it populate magit-repo-dirs
, I have this in my init file to achieve this
(eval-after-load "projectile"
'(progn (setq magit-repo-dirs (mapcar (lambda (dir)
(substring dir 0 -1))
(remove-if-not (lambda (project)
(file-directory-p (concat project "/.git/")))
(projectile-relevant-known-projects))))
(setq magit-repo-dirs-depth 1)))
The code above is executed after projectile
is loaded. It gets the list of projects known to projectile
by doing (projectile-relevant-known-projects)
, iterates through them and adds the projects that have .git/
folder to magit-repo-dirs
, it also sets magit-repo-dirs-depth
to 1 so magit looks for git repos only in the top directories.
I've found that if you use projectile, the easiest is to just set the following:
(setq projectile-switch-project-action 'projectile-vc)
Now you can use C-p
p
s
(projectile-switch-project
) and it will then immediately call projectile-vc
, which if you have magit
installed just calls magit-status
in the project root. Simple!
There is a nice package by John Wiegley called "Springboard" that might be useful for you.
https://github.com/jwiegley/springboard
It depends on helm
and/or ido
, and it allows you to execute arbitrary commands on the basis of files that might be in a different directory to the whichever file is currently in the active buffer. It's a little difficult to illustrate it in a non-verbose way (the README explains it well), but just to use your question as an example:
Assume you are working on a file
1.el
that is part of a git repo but you want to runmagit-status
for a separate file2.el
which is in another repo altogether.Using
M-x springboard
forhelm
orido-switch-buffer
forido
gives you a list of candidate files.By focussing on
2.el
(but not selecting it) you can then invoke
arbitrary commands as if you were working within that file's directory.So, if you were to focus on
2.el
and then callmagit-status
from the candidate list, you would get the normalmagit-status
window for the repo associated with the file2.el
and then once you have finished working with that you can just exit magit and1.el
will be the active buffer and everything will be as it was beforespringboard
was invoked.
It is extremely useful.
Magit uses current working directory of your current buffer when you execute magit-status. If other answers fail, here is inefficient but always working alternative:
- Close magit current buffers (if any) (
q
) - Change directory to the one with your repo (
M-x cd
) - Start magit again (
M-x magit-status
)
(Inefficient because magit will be restarted)