Create and Checkout a Git Branch in Emacs with Magit

As you probably are already aware, Magit for Emacs is Git magic. Magit works inside of your Emacs buffer to bring all of the power of Git to you without ever having to leave Emacs. In this article, you will learn how to do a Git branch and merge your branches in Magit.

If you are not familiar with the Git branching process in standard operation, you should check out our full guide on the basics of Git branches.

Branching in Git is incredibly powerful and flexible. You can create as many branches as you want. You can iterate on your files, completely change anything you want in the project without affecting any other branches or the project itself. You can leave your changes in your separate branch forever, or you can “merge” or “rebase” your changes onto the master branch.

Don't ruin the game with lag! Get a server that offers the best gaming experience, performance, and processing power with Bare Metal Hosting!

check markTop-Tier Performance check markPerfect for Gaming check markCustomizable check markDedicated Resources

Game Server Hosting

Using Git Commands In Magit

Many of the commands you would run in Git, whether on command line or in a Git GUI application, are available at the click of a key command in Magit.

In this case, we will be using the branching actions, which are bound to the b key.

Creating a New Git Branch

A branch is basically a divergent path from another reference point in your Git project history. This means you are not always required to branch from wherever the branch is pointing. You can branch from wherever the HEAD reference marker is pointing, which is most often aimed at master — but not necessarily so. You can also branch from other branches.

But for the sake of simplicity, in this tutorial we will create a new branch from the master branch.

First, create your new branch. From with the project working directory, launch Magit with C-x g. Press the b key to open the branch options. Press c to create a new branch.

Checkout to the New Branch

Now that you have created and named your new branch, you will notice that you are still on the master branch. To checkout your new branch, you will use the b key to open branching options and press b again to select the branch you want to checkout.

Merge The New Branch With Master

You have a few different options when it comes to merging the divergent branch into master. But before merging, remember to checkout the branch you want to merge your divergent branch into. For example, if you want to merge the divergent branch into master then first checkout master before attempting a merge.

Press the m key to open your merging options. Here are the merging options:

  • m A basic merge will bring all changes and commits on the divergent branch into master.
  • e Creates a commit for the merge but allows you to edit the message.
  • n Merge content but don’t create a commit.
  • a Absorb the branch, brings in all changes and commits and deletes the divergent branch. A nice way to keep everything clean.

In most cases, the m option for a basic merge that brings in all the commits and history of the other branch is probably what you want, but you should test out all of them and see which one works best for your workflow.

If you don’t need cPanel, don't pay for it. Only pay for what you need with our scalable Cloud VPS Hosting.

check markCentOS, Debian, or Ubuntu check markNo Bloatware check markSSH and Root Access

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Was this article helpful? Join the conversation!

Questions about our MailChannels Deployment? We have answers and are here to help!Learn More
+