How to Manage Git in Emacs with Magit

Emacs Magit 00 Featured Image

Git is a brilliant tool. It simplifies the version control process and it allows users to easily share and contribute code to each other. Git is also system and content agnostic. This means that it can run on any platform, which makes it a highly useful and sought after tool for programmers and casual users alike.

One issue with Git, however, is that it requires you to use a command-line utility to fully use its functions. This means that in order to use Git you need to learn a number of commands specific to it. However, if you are using Emacs there can be a better way.

Magit is a powerful front-end for Git in Emacs. It allows you to easily and seamlessly use Git in Emacs to create, manipulate and save Git repositories through simple Emacs keybindings. Not only that, Magit also automatically adapts existing Git repositories through its interface. This means that you can install Magit right now and immediately use it to manage version control.

How Magit Works

Magit is, at its core, a porcelain for Git in Emacs. It behaves as a container program that translates the various Git functions to either keybindings or visual cues. This means that Magit allows a user with little to no experience on using Git to properly use it for version control.

Emacs Magit 04 Sample Magit Commit

Also read: How to Use Emacs to Play Music with EMMS

Installing Magit

Knowing that, adding Magit to your Emacs client is incredibly easy. At the moment, Magit is available in the MELPA repository as a package. This means that in order to install it, you will need to tell Emacs to look at that repository first. To do that, you can add the following Lisp code to your init.el file:

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

From there, you can press Alt + X then type “package-install”. This will bring up a small prompt where you can type the name of the Emacs package that you want to install. In this case, you need to type “magit” and press Enter. That will, in turn, download the Magit’s source code then compile and install it for you.

Emacs Magit 05 Magit Installed

Also read: 5 Hidden Features You Can Use to Improve Emacs

Using Magit

Using Magit is relatively straightforward. One of the most important keybinding that you will use with it is Ctrl + X, then G.

This will open the Magit’s Status Screen and it will display the current status changes for a particular repository. For example, I can press this Ctrl + X, then G on a repository and it will display the latest status as well as some of the commits that I have made recently.

Emacs Magit 06 Git Directory

Adding and Committing Files

One of the key features of Magit is that it actively looks at all of the files in the current Git repository. This means that unlike traditional Git methods, you can easily add untracked files to version control. For example, I can press Ctrl + X, then G to display the changes that I have made in the current repository.

Emacs Magit 10 Unstaged Git Files

Further, doing it this way allows you to review and only select the files that you want to either add or commit. For example, if you have two new files and you only want to add one of them. You can easily select the file that you want and press S to stage it in Magit. This is similar to doing a git stage ./file in the command line.

Emacs Magit 11 Partial Staged Git Files

Once a file is in staging, you can now commit those changes to the Git filesystem. To do that, you can just press C twice to tell Magit that you want to commit the staged changes to Git. This will bring up the commit message buffer where you can type a brief summary about your commit. From there, you can then press Ctrl + C twice to save your commit.

Emacs Magit 12 Sample Git Commit

Also read: How to Use Email within Emacs

Creating a New Git Repository

However, Magit’s functions are not only limited to existing Git repositories. In that, it is also possible to create a new repository from scratch through this program.

For example, you can open a non Git directory by pressing Ctrl + X, then D and typing that directory’s path. This will tell Emacs to load that particular folder as a Dired buffer.

Emacs Magit 07 Regular Directory

Once done, you can press Ctrl + X, then G to run Magit. This will tell the program to look for any existing Git files in the current directory. You can press Enter here to force the program to create a new repository in the current folder.

Emacs Magit 08 Create New Git Repo

From there, Magit will generate a status screen that will display all of the files in the current directory. You can then press S on the “Untracked Files” header and C to add and commit those files to the Git filesystem.

Emacs Magit 09 New Git Repo Blank

Also read: 5 Useful Emacs Packages for Better Productivity

Creating and Switching Git Branches

Another brilliant feature of Magit is the ability to manipulate Git branches. This means that you can easily create and move between a repository’s branches from within the program.

Similar to the commands above, you can access this function by pressing Ctrl + X, then G. You can then press B to tell Magit that you want to access its Branch feature.

Emacs Magit 13 Branch Feature

From here, you can do a number branch-related Git functions. For example, I can press N to tell Magit to create a new branch for the current repository. It will then ask me where I want the branch to start from as well as a short name for it.

Emacs Magit 14 Git Branch From

In my case, this will be the first branch that I will make for this repository. As such, I can just press Enter on the first question and provide “Branch-1” for its name.

Emacs Magit 15 Git Branch Name

Once done, you can now switch to the branch that you have just made. To do that, you can press Ctrl + X, then G to bring the status screen and press B to open the Branch function.

You can then press B again to tell Magit that you want to change to a different Git branch. In my case, I will type the name “Branch-1” to switch to the branch that I made.

Emacs Magit 16 New Git Branch

From here, you are now working on a different Git branch. As such, you can also use the add and commit functions discussed above to make branch-specific changes to your repository.

Emacs Magit 17 New Branch Commit

Pushing and Pulling from a Git Remote

Another feature that you can use in Magit is the ability to communicate with an online Git server. In that, the program allows you to both push and pull from a particular Git remote. This can be incredibly useful if you are using Git for a collaborative project with other people.

Emacs Magit 18 Sample Remote Repository

Adding a new online remote for a local repository is relatively straightforward. To do that, you need to first press Ctrl + X, then G to bring up the status screen. From there, you then need to press Shift + M to open Magit’s Add function.

Emacs Magit 19 Git Add Feature

Once done, you can press A to add a new Git remote. Magit will then ask for the name and the URL of the remote that you want to add. In my case, I will use my Github account to link my local repository to an online one.

Emacs Magit 20 Remote Link

With that, you can now do the initial push to your online repository. To do that, you need to go back to the status screen and press Shift + P. This will bring up Magit’s Push function and you can press P here to push the current Git tree online.

Emacs Magit 21 Push Feature

On the other hand, it is also possible to get all the recent changes in a remote repository. Similar to a git pull, you can also use Magit to fetch the updates for a specific branch and repository.

To do that, you need to go back to Magit’s status screen and press Shift + F. You can then press P again to pull from a remote’s Master branch.

Emacs Magit 22 Pull Feature

Merging Git Trees

Lastly, one of the most notable features of Magit is that it also allows you to easily merge Git trees from within Emacs. This can be especially useful if you are trying to fix a bug in your code and you want to have a quick way to implement changes from an experimental branch.

Emacs Magit 23 Branch Ahead Of Master

One important thing to note is that in order to merge a Git tree, you need to make sure that the branches that you want to merge are currently clean. For example if I want to merge my Branch-1 to Master, I need to stage and commit all changes between those two branches.

Emacs Magit 24 Switching Back To Master

Knowing that, merging a Git branch using Magit is relatively easy. To do that, you need to press Ctrl + X, then G to go to Magit’s status screen. From here, you then need to press M to tell Magit that you want to merge Git trees.

Emacs Magit 25 Git Merge Feature

Once there, you can press M again to start the merging process. In this, Magit will ask you for the branch that you want to merge to the main tree. In my case, I will be typing “Branch-1” to specifically tell Magit that I want that branch to merge with the Master tree.

Emacs Magit 26 Master Merged With Branch

Also read: How to Use Sed in Linux for Basic Shell Tasks

Frequently Asked Questions

Is it possible to unstage a file in Magit?

Yes! It is possible to unstage a file in Magit. Similar to staging a file, you need to press Ctrl + X, then G to go to Magit’s status screen. From there, you can then select the file that you want and press U to unstage it.

Further, you can also press Shift + U while in this screen to unstage all the currently staged files in Magit’s buffer.

Can Magit create code patches similar to git patch?

Yes! You can easily use Magit to create code patches from your code commits. In order to do that, you need to press Ctrl + X, then G and press Shift + W. This will tell Magit that you want to either create or apply a patch in this code repository.

From here, you need to make sure that you are in the branch that you want to create a patch out of. Once there, you then need to press C twice to create a patch from the latest commit out of this branch.

Is it possible to cancel a Magit function while it is running?

Yes! Just like all Emacs functions, you can easily cancel any action under Magit by pressing Ctrl + G. This includes all of the primary and secondary menus in Magit as well as any prompts that you go into.

Not only that, you can also forcefully stop a running function by pressing Ctrl + G thrice. This can be especially helpful if you are in a slower machine and a Magit function is slowing your computer down.

Image credit: Unsplash

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Ramces Red
Ramces Red - Staff Writer

Ramces is a technology writer that lived with computers all his life. A prolific reader and a student of Anthropology, he is an eccentric character that writes articles about Linux and anything *nix.