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-dirsRET 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 run magit-status for a separate file 2.el which is in another repo altogether.

  • Using M-x springboard for helm or ido-switch-buffer for ido
    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 call magit-status from the candidate list, you would get the normal magit-status window for the repo associated with the file 2.el and then once you have finished working with that you can just exit magit and 1.el will be the active buffer and everything will be as it was before springboard 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:

  1. Close magit current buffers (if any) (q)
  2. Change directory to the one with your repo (M-x cd)
  3. Start magit again (M-x magit-status)

(Inefficient because magit will be restarted)

Tags:

Emacs

Git

Magit