Magit is a comprehensive text-based user interface for Git, it is a convenient and efficient way to interact with Git repositories. It bridges the gap between the Git command-line interface and various graphical user interfaces (GUIs) and is complete enough to allow users to perform both simple and complex version control tasks directly from within Emacs. Without further ado, let’s get started!

Prerequisites

Emacs

As mentioned earlier, magit is integrated with the Emacs text editor. For this tutorial, we will be using vanilla Emacs.

Git

Seeing as it is an interface for git, you must have git installed.

Please refer to the relevant documentation for installation and getting started instructions:

Getting started

Install Magit

  • We will be installing magit as a package
  • From within the home/<user>/.emacs.d/ directory, add an init.el file with:
# package initialization

(require 'package)
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/") t)

# key bindings

(global-set-key (kbd "C-x g") 'magit-status)

# install magit

(use-package magit
  :ensure t
  :bind (("C-x g" . magit-status)))
  • Now, from within the buffer (the page of init.el):
M-x eval-buffer RET
  • This will load the magit package and use C-x g as the keybinding for magit status

Setting up the Github Repo

  • Now that magit is setup, let’s create a repository to push code to, we will use Github for this example
    • This assumes that you are connected to Github via SSH, if not you can follow the instructions for setting it up here
  • In Github, click on the Repositories tab and go to New, name your repo and click on Create Repository
  • From this screen:

createrepo

  • In your terminal, from the directory of the project that you want to push to Github, we will follow the instructions under …or push an existing repository from the command line

  • If the project has not already been initialized, use:

git init
  • Add the remote origin
git remote add origin git@github.com:Lisa-Stats/example.git
  • Update the branch to be main
git branch -M main
  • Make sure that the name and email associated with your Git commits are correct by using:
git config -l

# If they are not, then you can update it for the repo using these commands:

git config user.name <username-for-github>
git config user.email <email-for-github>

# if you want to set it globally for every repo on your computer, use:

git config --global user.name <username-for-github>
git config --global user.email <email-for-github>

Initial Commit

  • Let’s start by bringing up magit status with the keybinding that we created earlier - C-x g

  • Unlike the Git command line, nearly every visible piece of information you see in a Magit buffer can be acted on

    • Regardless of were you are in the buffer, you can almost always show more details about the thing at point without having to type or copy-paste any info, as you often would have to do on the command line
  • To begin, click on Untracked files

initial-untracked-file

  • Press s to stage those untracked files

  • Click c to show the available commit commands and arguments in a buffer at the bottom of the frame

initial-commit-commit-buffer

  • Press c again to commit the changes

  • Now two buffers will appear, one is for writing the commit message and the other shows a diff with the changes that you are about to commit

    • Write a message and then type C-c C-c to create the commit
  • Since it is the initial commit, we will be using the last command that we saw in the Setting up the Github Repo to push the commit to the repo

git push -u origin main
  • Go back to your magit status buffer and click g, this will refresh the current status buffer
    • It should now look like this:

initial-commit-push

Conclusion

As you can see, Magit provides a level of control and functionality that is not commonly found in Git interfaces. Since it operates by running Git commands in the background, this allows a high amount of transparency. Which in turn, can allow users to learn the Git cli and enhance their proficiency in version control as a whole.

Join me for Part 2, as I dive deeper into how Magit can make typical workflows seem effortless!