gitemacsmagit

How to change repo with magit emacs?


I need to work with a few different git repositories. How to switch between them using magit? Magit allows me to choose a directory when I just start it.. But it is not clear for me how to switch to another repo if required. I've looked at magit cheatsheet and docs but haven't found an answer. Thanks.


Solution

  • magit have changed a few things, so here is an updated version that worked for me in 2024 of the accepted answer

    (let ((depth 1)) ;; set the depth of where .git will be at the deepest to 1, i.e. somesubproject/.git/ and .git/ will be searched. if set to 0, only .git/ directly after root will be listed
    (eval-after-load "projectile"
      '(progn (setq magit-repository-directories (mapcar (lambda (dir)
                                                           (cons (substring dir 0 -1) depth)
                                                           )
                                           (remove-if-not (lambda (project)
                                                            (file-directory-p (concat project "/.git/")))
                                                          projectile-known-projects))))
             )))
    

    The things changed:

    edit: I changed from (projectile-relevent-known-projects) to projectile-known-projects. the former don't include some projects, such as the current one.