Using Git in EMACS with Magit

In this post we are going to learn how to use Magit in EMACS to perform git operations within EMACS.  This is going to be a quick introduction to show you the basics of using the most common git commands with Magit.  Magit is very encompassing with its git command coverage.  You can do almost everything with git that you can do in the command line.

Installing Magit

Magit is available in MELPA.  Open EMACS and list all the packages available:

M-x package-list-packages RET

Then search for magit

C-s magit

You may have to repeat the search a few times to get to the right one, which will have magit on its own:

Hit the ‘i’ key then ‘x’ then ‘y’ for yes to install it.  Exit out of EMACS.  Now you will need to edit your .emacs file:

$ emacs ~/.emacs

Add this line in your configuration:

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

Save and exit emacs again.

Using Magit

Go to one of your local git repositories and open it in EMACS.  Open up Magit using this command:

C-x g

This will open the Magit window where you will see your last commit.   Edit your files like normal and save the file but stay in EMACS.  For this tutorial, I will be editing a file called  Substitute your filename where appropriate.

Git Add

Now we have a file that we would like to add to a git commit.  Normally this is done with:

$ git add

To do this in Magit make sure you have the Magit status window open with

C-x g

If you already have it open use the same command to update it. You will notice our status has changed:

Using the arrow keys go to the line where it says ‘modified’ and hit the ‘s’ key to stage the file.  If you want to do multiple files you can mark them just like you mark lines in source files with C-Space then typing the ‘s’ key.

Our file is now staged. And ready to commit.

Git Commit

Now we need to create a commit like we normally would with:

$ git commit

To do a commit in Magit after you have staged files hit the ‘c’ key.  Next hit the ‘c’ key again to make a commit.  This will change the view a bit.  On the left side is the diff of the commit.  On the right-hand side you will be able to edit the git commit message.  Start typing in your message then use this command to save the commit.

C-c C-c

It should now show you your most recent comments including your last one.  Now we are ready to push those updates to your remote git repository.

Git push origin master

Here we want to push back to our remote git repository.  This will push the changes you have made in the local branch to the matching branch on the remote repository.  For example, if we made the change in the master branch of our local repo then this command will try to push to the master branch of our remote repository.

While still in the Magit window looking at our commits hit the Shift-P (capital P) which will bring up the push menu.  Hit the ‘m’ key to push to matching branches.  After a few seconds it will push your changes.  Verify on your remote git repo.  In my case this is GitHub.

As you can see the git commit id (highlighted) matches what we had in Magit.

Git Pull

In order to do a

$ git pull origin master

You will need to set the default upstream for your master branch.  From the Magit status screen press the ‘b’ key then the ‘u’ key.  Hit enter to select the default of origin/master.  Now to do a pull hit the ‘F’ key then the ‘u’ key.

I hope that you have enjoyed this post.  If I left out any steps that you would like to see in this tutorial then please leave me a comment.


I hope you have enjoyed this article, if so please leave a comment below.  For more articles, please signup for the AdminTome Blog below.  Also please feel free to share the article to your friends using the buttons to the left.  Thanks again for reading this post.