Streamlining Git Operations in Emacs: From Eshell to Magit

Magit. This is one of the major reason why I keep using Emacs.

The Problem with Git in Eshell

While Eshell provides a built-in way to use Git in Emacs, it still has limitations:

  1. Need to type full Git commands like in terminal

    ~/projects/myapp $ git add .
    ~/projects/myapp $ git commit -m "Update feature"
    ~/projects/myapp $ git push origin main
    
  2. Complex operations require multiple commands

    ~/projects/myapp $ git stash
    ~/projects/myapp $ git checkout main
    ~/projects/myapp $ git pull
    ~/projects/myapp $ git checkout -b feature
    ~/projects/myapp $ git stash pop
    
  3. No visual interface for:

    • Staging specific hunks
    • Reviewing changes
    • Resolving conflicts

Solution: Pretty Hydra and Magit

First, we’ll set up pretty-hydra to create an intuitive interface:

(leaf major-mode-hydra
  :doc "Use pretty-hydra to define template easily"
  :url "https://github.com/jerrypnz/major-mode-hydra.el"
  :ensure t
  :require pretty-hydra)

Then integrate Magit with a pretty interface:

(leaf magit
  :doc "Complete text-based user interface to Git"
  :url "https://magit.vc/"
  :ensure t
  :init
  (setq magit-auto-revert-mode nil))

(leaf *hydra-git
  :bind
  ("M-g" . *hydra-git/body)
  :pretty-hydra
  ((:title " Git" :color blue :quit-key "q" :foreign-keys warn :separator "╌")
   ("Basic"
    (("w" magit-checkout "checkout")
     ("s" magit-status "status")
     ("b" magit-branch "branch")
     ("F" magit-pull "pull")
     ("f" magit-fetch "fetch")
     ("A" magit-apply "apply")
     ("c" magit-commit "commit")
     ("P" magit-push "push"))
    ""
    (("d" magit-diff "diff")
     ("l" magit-log "log")
     ("r" magit-rebase "rebase")
     ("z" magit-stash "stash")
     ("!" magit-run "run shell command")
     ("y" magit-show-refs "references")))))

Understanding the Configuration

Pretty Hydra Setup

  • major-mode-hydra provides the foundation for creating structured menus
  • :require pretty-hydra loads the core functionality
  • Used to create organized command palettes

Hydra Menu Structure

  1. Title and Options

    • :title " Git" - Displays at the top
    • :color blue - Menu stays until you quit
    • :quit-key "q" - Exit with ‘q’
    • :separator "╌" - Visual divider
  2. Command Groups

    • “Basic” section for common operations
    • Empty section ("") for visual spacing
    • Commands organized by function
  3. Key Bindings

    • Single letter shortcuts (e.g., “s” for status)
    • Descriptive labels for each command
    • Logical grouping of related functions

How It Improves Your Workflow

Before (with Eshell)

# Stage and commit changes
~/projects/myapp $ git add .
~/projects/myapp $ git status
~/projects/myapp $ git commit -m "Update feature"

# Check and push changes
~/projects/myapp $ git log
~/projects/myapp $ git push origin main

After (with Pretty Hydra + Magit)

  1. Press M-g to open Git menu
  2. See all available commands at once
  3. Execute with single key press
  4. Menu stays open for multiple operations

Key Features

  1. Visual Menu

    • All commands visible at once
    • Organized categories
    • Clear descriptions
  2. Efficient Operation

    • Single key execution
    • Persistent menu
    • Logical grouping
  3. Enhanced UX

    • No command memorization needed
    • Consistent interface
    • Quick access to all Git operations

Conclusion

While Eshell remains useful for general command-line operations, the combination of Pretty Hydra and Magit provides a more efficient and user-friendly interface for Git operations.

The state of init.el at the conclusion of this article can be found here: https://github.com/Rindrics/.emacs.d/blob/f15a592ff38e34cd9919d584a65ca02a1dbcc829/init.el

References


comments powered by Disqus