How to use Magit to manage Git projects

Emacs' Magit extension makes it easy to get started with Git version control.
173 readers like this.
Out of the trash and into the classroom

Opensource.com

Git is an excellent version control tool for managing projects, but it can be hard for novices to learn. It's difficult to work from the Git command line unless you're familiar with the flags and options and the appropriate situations to use them. This can be discouraging and cause people to be stuck with very limited usage.

Fortunately, most of today's integrated development environments (IDEs) include Git extensions that make using it a lot easier. One such Git extension available in Emacs is called Magit.

The Magit project has been around for 10 years and defines itself as "a Git porcelain inside Emacs." In other words, it's an interface where every action can be managed by pressing a key. This article walks you through the Magit interface and explains how to use it to manage a Git project.

If you haven't already, install Emacs, then install Magit before you continue with this tutorial.

Magit's interface

Start by visiting a project directory in Emacs' Dired mode. For example, all my Emacs configurations are stored in the ~/.emacs.d/ directory, which is managed by Git.

Use Dired mode to navigate a Git project

If you were working from the command line, you would enter git status to find a project's current status. Magit has a similar function: magit-status. You can call this function using M-x magit-status (short for the keystroke Alt+x magit-status). Your result will look something like this:

Magit status popup window

Magit shows much more information than you would get from the git status command. It shows a list of untracked files, files that aren't staged, and staged files. It also shows the stash list and the most recent commits—all in a single window.

If you want to know what has changed, use the Tab key. For example, if I move my cursor over the unstaged file custom_functions.org and press the Tab key, Magit will display the changes:

Use Tab key to view changes

This is similar to using the command git diff custom_functions.org. Staging a file is even easier. Simply move the cursor over a file and press the s key. The file will be quickly moved to the staged file list:

Use the key 's' to stage a file

To unstage a file, use the u key. It is quicker and more fun to use s and u instead of entering git add -u <file> and git reset HEAD <file> on the command line.

Commit changes

In the same Magit window, pressing the c key will display a commit window that provides flags like --all to stage all files or --signoff to add a signoff line to a commit message.

Magit commit popup window

Move your cursor to the line where you want to enable a signoff flag and press Enter. This will highlight the --signoff text, which indicates that the flag is enabled.

Enable signoff

Pressing c again will display the window to write the commit message.

Magit commit message popup window

Finally, use C-c C-c (short form of the keys Ctrl+cc) to commit the changes.

Commit changes

Push changes

Once the changes are committed, the commit line will appear in the Recent commits section.

The new commit will appear in the 'Recent commits' section

Place the cursor on that commit and press p to push the changes.

I've uploaded a demonstration on YouTube if you want to get a feel for using Magit. I have just scratched the surface in this article. It has many cool features to help you with Git branches, rebasing, and more. You can find documentation, support, and more linked from Magit's homepage.

Tags
psachin
Sachin is passionate about Free and Open source software. He is avid GNU Emacs user and likes to talk and write about open source, GNU/Linux, Git, and Python. He has previously worked on OpenStack, ManageIQ/CloudForms & Red Hat Insights. He also likes to explore Swift Object Storage in his spare time. He can be reached on IRC as psachin@{Libera.Chat, Freenode, OFTC, gnome}.

2 Comments

Thanks for this article, right timing for my usage. BTW, can we see your ``init.el`` file?

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.