Magit – A use case

Tags: emacs technical magit

I have been using magit for some time at a more superficial level. From what I can see, it is remarkable; at least I don’t have to type a lot of git commands. Command line is cool and everything, but typing a command again and again will waste the time and just might start a pain in your hand. Since most of my coding happens in emacs, I am delighted that magit exists and makes common and most repetitive things only a matter of few keystrokes.

(As a sidenote, emacs is all about keystrokes and once I started using the other excellent tools (like projectile, helm), I tried to cut-down on my keystrokes at other apps as well. For instance, searching around on the internet, I found z for the command line which replaces cd ~/coding/projects/language/project_name with z project_name, and is almost always correct.)

Here are a few things that I do on a regular basis:

M-x magit-init starts a new project. Although when running around in the command line, git init is as short as it can get.

C-x m gets the magit buffer for the current project.

Once on the buffer, these are the commands for the usual operations:

Staging

The buffer gives the list of files which are either untracked or staged. n and p (like dired) move up and down the list of files.

Pressing s on any filename stages it, and pressing u on the staged file unstages it.

S will stage all the files.

Pushing and Pulling

F is for pulling the code from remote. -d once on the magit buffer will switch on the dry-run option. Otherwise u pushes to the origin/master or p can be used to set another upstream branch. So usually, it is just 3 keystrokes C-x m F u to pull the latest changes.

f is for fetching the code from remote.

P is for pushing to remote. Again, it uses the same options as pulling and pushes the code to remote in 3 keystrokes: C-x m P u

Branches

b is the key for branching related operations.

b n creates a new branch. This is git branch branchname. b b checks out a branch. This is git checkout branchname. b c creates and checks out the branch. Equivalent to git checkout -b branchname. b m renames and b x deletes the branch.

Commiting

c is for commit popup.

c c will commit and ask for a commit message. c a will amend the last commit.

Others

Pressing r on the magit buffer gives the rebasing options. d is the option of diffing the changes, this gives the option of diffing unstaged, staged changes, as well as looking at the last commit.

l shows the log which can be enhanced by using options like -g and -d to show the graph and decorate options.

These are just a subset of commands, and the ones I use on a regular basis. There are many, many more commands, since magit essentially presents the entire git command line in emacs. The excellent documentation is the first thing to look in to find more about the commands.