A Magit Tutorial: Part II

Saturday, November 26, 2011

In Part I, we saw how to open up the Magit status buffer, view unstaged changes and untracked files, stage changes and commit them. The next thing you should learn is how to view the git log.

The Magit Log

To view a terse history of your repository (à la git log --graph --oneline --abbrev-commit, which I’ve aliased to git pretty), press l l in any magit buffer. To view the log in verbose form, use l L instead. Initially, only the last 100 commits are shown (to see 100 more, press e, and to see all commits, press C-u e).

Operations on commits in the Magit log

Press RET to view any commit in the log.

Applying and reverting commits

Pressing a on a commit will apply it to your current branch (a “cherry pick”, in git-speak), although the changes won’t be automatically committed (use A instead to apply and commit in one go).

On the other hand, v will revert the commit at point (again, you need to commit the changes yourself).


You can compare any 2 commits or the working tree and a commit right from Magit. Let’s see how.

With the commit log

Let’s say you want to compare 2 commits in the log. Mark the first commit by navigating to it and pressing .. Then go to the second and press =, and your diff will come up in a new buffer. Similar to the previous section, you can selectively apply parts of the diff to your current branch by using a and A, and reverse-apply them using v.

Without the commit log

To compare your working directory with a specific commit, use d. To compare any 2 arbitrary commits, use D. As usual, you can use a and v to apply and reverse diffs / chunks of diffs.

Resetting and discarding changes to the working tree

You can reset to a previous commit in the branch by typing x and entering a commit name (this will not affect your working tree or staging area in any way). This does a soft reset, meaning that the changes from the discarded commits are put back in your working tree, and you can commit them again. Be forewarned that rewriting history in this way is not preferable if you have already published it (instead do a normal revert using v on the defective commits from the log).

To discard only the changes in your working tree and staging area (i.e., to reset to the state of the most recent commit), press X. This will get rid of all uncommited changes, but will not affect the commit log.

Pushing and Pulling

Remember the Magit status buffer we talked about in Part I, which you get by running M-x magit-status? After you’ve made your commit with C-c C-c, Magit will thoughtfully display an Unpushed Commits section in the status buffer, meaning that you haven’t yet published those commits to a remote repository. There’s not much point in committing from Magit if you can’t do your git push from it too. So, to push the current branch to a remote, type P P. If you have no default origin repository configured, you will be prompted for it. If you want to push to a different remote, give a prefix argument to P P.

If you want to run a git pull, press F F. If the current branch is not configured to pull from some remote branch, you will be prompted for one. Magit, again very thoughfully, displays an Unpulled Changes section in the status buffer.

This is the end of Part II. Look out for Part III, which will cover the intermediate to advanced stuff, like branching and merging.

blog comments powered by Disqus