This is a fast introduction to Magit, an Emacs mode for Git. It improves productivity a lot by integrating with the best text editor ever and saving you the hassle of typing git commands. Magit has a gazillion features so we’re gonna explore just the real basics. The most common commands on an average Git workflow.

Let’s start by installing Magit. Normally you’d do it via Melpa (you know, M-x package-list-packages), but you can also get it by their GitHub repository.

Alright, the most common thing you’ll do is run M-x magit-status, so you better bind it to a key.

(global-set-key (kbd "C-c m") 'magit-status)

When running it, Magit opens a status buffer with some info about your current situation. If there’s no git repository here, it prompts to create one. The buffer shows current branch, HEAD message, staged/unstaged files, local/remote, etc. This buffer is where you’ll do Git actions. To quit it, press q.

Suppose you’ve altered a file. As soon as you go M-x magit-status (or type g into that buffer) the git information is refreshed. Now you can add things, unstage changes and commit. Let’s do just that.

By typing n and p you can move around the files. Press s to stage or u to unstage files. When you’re done, do c to commit. Do another c to confirm and there you are at the commit message.

To cancel, press C-c C-k and to confirm the commit, C-c C-c. As soon as you do that it takes you back to that buffer screen, showing off the latest information.

With this sprint, we’ve covered the following git commands with simple Emacs shortcuts:

$ git init
$ git status
$ git add
$ git rm --cached
$ git commit

Now, here’s some extras (remember, all commands apply inside Magit’s buffer only):

  • To ignore a file (add to .gitignore), press i when hovering it.

  • S stages everything and U unstages everything.

  • l followed by l gives you a short history of the repository.

  • t tags a selected commit.

  • M followed by a adds a new remote.

For a full-fledged introduction to Magit (with tons of features described) go to Mastering Emacs. And here’s a CheatSheet!

Leave a reply

required

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">