Intro to Interactive Rebasing in Git and Customizing Vim Preferences

Intro to Interactive Rebasing in Git and Customizing Vim PreferencesLucas PenzeyMoogBlockedUnblockFollowFollowingApr 10This will be old news to some, but I’ve always been told to write the blog post you wish you had when you started learning about a particular topic.

So here’s what I just learned about interactive rebasing and customizing vim.

Interactive rebasing allows you to rewrite the history of your commits by editing the messages, squashing commits together, or even deleting commits entirely.

Due to this functionality it’s best to proceed with caution.

If it’s your first time with this process I recommend creating a new throwaway branch off of a relatively mature feature branch you’ve been working on to play around in.

It’s also worth noting that you should never make rewrites to a branch that’s already been shared with others.

Interactive rebasing is best used when you want to clean up your personal branch before pushing.

Accessing the Right CommitRunning the command git rebase -i <hash or index here> is how you enter an interactive rebase session, and there are a few ways to tell git which commit you want to work on.

You can either enter the hash of the commit (the yellow string below, found by entering git log –oneline) or you can use the index of the commit based on the current HEAD position.

Find commit hashes with `$ git log — oneline`It’s important to know that the hash number passed into git rebase -i should be the hash of the commit that immediately precedes the commit that you want to modify.

So in the picture above, if I wanted to modify the current commit (with the hash of bcee401) I would enter git rebase -i 36ba290, which launches vim with the preceding commit and your rebase options:You can also access the same commit by passing in the index of the desired commit in relation to the head.

The command git rebase -i HEAD~1 is equivalent to the rebase command above that uses the commit hash.

Interactive rebasing is more useful when you have a branch with a good amount of commits so the above branch is just for explanatory purposes (until recently it had a couple dozen commits, but I squashed most of them (on purpose) in the process of learning about interactive rebasing).

Now that you’ve launched an interactive rebase session you’re presented with a multitude of helpful options for modifying the desired commit(s).

All you do is replace pick before the hash you want to modify with your desired command.

If you’re new to vim and are utterly bamboozled at this point, here’s a handy cheat sheet for the various commands.

Again, if this is all new to you please do yourself a favor and play around with this feature in a branch that you don’t care about and haven’t shared with others.

In addition to rewording commit messages and squashing commits together you also have the power to delete commits entirely, and there is no undo function.

There are many great resources out there on the mechanics of interactive rebasing, so the rest of this post will be about configuring the vim version that your terminal uses as a default editor.

Customizing Vim in the TerminalSo you want to rebase interactively, but stock vim is boring and you want to get fancy with it.

What do you do?.There are a few quick settings that you can implement to make your rebasing life a little simpler and more colorful.

Here’s what the stock vim looks like when you launch an interactive rebase in the terminal:This is your vim on bland modeThis is all well and good, but customization is the spice of life, and there are many helpful settings you can incorporate toYour Config FileTo access and modify the settings for vim in the terminal you need a .

vimrc file in your root/home folder.

To see if you already have one, navigate to your home folder in the terminal and enter ls -a which will list all the files in said directory, even the hidden dot files.

If you don’t see the file, simply create one by entering touch .

vimrc in your root directory.

It’s worth noting that you want to be editing .

vimrc and not .

gvimrc for this tutorial.

The latter is how you customize the preferences for the GUI version of vim, whereas the first will let you customize the terminal version of vim.

Once you’ve opened .

vimrc in a text editor, all you need to do is drop in your chosen settings, but please don’t enter settings without fully understanding them first.

Below are some basic ones for starting off, but there’s a whole world of customization options available to the savvy vim user.

syntax on Pretty self explanatory — it enables syntax highlighting.

autocmd Filetype gitcommit setlocal spellThis enables spellcheck for gitcommit messages and it’s incredibly useful if you’re like me and get a nervous twitch when reading typo-filled commit messages.

set textwidth=72This will set the wrapping length of a line to 72 characters which makes longer commit messages more readable.

set numberThis shows the line number for easier navigation.

set showcmdThis will show the last command you’ve entered in the bottom line.

You can see it in action here when I enter the write command (:w) and “written” shows up.

set cursorlineThis shows an underscore across your editor on the line where your cursor resides for easier navigation.

Again, there are a ton of options to choose from for customizing your vim experience.

Just make sure you understand everything you put into your .

vimrc or you could end up creating more work for yourself down the road.

Happy interactive rebasing!.

. More details

Leave a Reply