Tutorial on Interactive Rebasing

Tutorial on Interactive RebasingHow to perform an interactive rebaseQuintessa AndersonBlockedUnblockFollowFollowingMay 24Photo by Andrew Neel from PexelsIntroductionLets face it, “git can get really tricky really fast”.

These are words echoed by one of my mentors during a conversation we had about how to utilize git commands.

Currently, I am learning how to rebase my commit messages on my local branches so that I have one linear commit history and would like to share my experience with you thus far.

Intended AudienceThis tutorial is intended for anyone who is new to git, wants an introduction to the rebase command and anyone who wants to learn how to change their commit history using Vim.

What is Git?Simply put, git is a super useful tool used to manage changes made within applications over time.

It is designed for coordinating work among programmers, yet it can also be used to track changes to any set of files during software development.

There are many commands that I am already familiar with such as git status, git add, git commit, git checkout -b, git checkout, git push, and git pull.

One command that I am not so familiar with is the rebase command which I will discuss next.

What is Git Rebase?In git, the rebase command integrates changes from one branch into another.

It is an alternative to the better known merge command.

The merge command, however, will essentially integrate two branches into one branch.

“So what is the difference between git rebase and git merge?”I recall asking my mentors this question, as the two commands seem quite similar by definition.

Both commands share the same purpose which is to integrate changes from multiple branches into one branch.

The difference between the two commands is the process in which this is achieved.

For example, git rebase will rewrite the commit history to produce a straight and linear commit history.

On the contrary, git merge will keep the committed git history and in chronological order.

Unlike merging, however, rebasing will flatten the commit history and will transfer the completed work from one branch to another.

How to use Git Rebase in VimNext we will go over how to rebase a feature branch into the master branch using a VIM interface.

You can also rebase your commits using automated rebasing, however, interactive rebasing helps to streamline this process so it is less confusing.

It gives you complete control over the feature branch’s commit history.

First, open your terminal and navigate to the root directory of your project and type:git checkout name-of-feature-branchTo see a printed list of the past commit messages for this branch and all of your branches type:git log –onelineThis will print out something that looks like this:In this example we will rebase the commits associated with the features/addFooter branch.

The series of numbers and letters in yellow are the SSH keys.

We will use these keys to perform our rebase.

In order to retrieve all the commits for the feature branch type:git rebase -i f8e1242This SSH key (f8e1242) does not return itself or anything before it, but rather returns all the commits above it.

orgit rebase -i HEAD~3Starting at the HEAD of the feature branch there are 3 commits associated with this branch.

Next, you should see the interactive console in which you can simply press “i” to go into insert mode and follow any of the commands that we see listed below.

In this case we will squash our commits so we can have on linear commit history.

It’s important to know that once in VIM the order of the commits are reversed.

In addition, the commit message at the top cannot be squashed, but the following can use squash (s) command.

Simply replace the proceeding pick with squash or you can abbreviate it using s.

To exit out of insert mode simply press esc followed by :wq or :wthen :q after which will bring you to this screen.

After entering insert mode we can make changes to the messages, add to them, and even ignore them.

After you are satisfied with the changes you have made, you will notice an error occurs when you try to push the changes to your feature branch.

Because we are rewriting the commit history, we have to flag our push with –fgit push features/addFooter –forceso that git knows this is the correct commit history that will now be associated with this branch.

When we check out the commit history on Github we’ll see our commit history is now uniform and more focused.

And there you have it!.You have just performed your first rebase!.Congratulations!ConclusionIf you find that you are stuck, try going through this tutorial again.

There is more than one way to rebase your commits on your feature branches and interactive rebasing is just one of them.

Granted in this example we just rebased a few commits, rebasing can prove to be even more effective when you have a relatively long history of commits.

I found interactive rebasing to be the most user friendly for me as I learned this process and I hope you will too!If you get stuck I have provided some helpful resources below:Intro to Interactive Rebasing in Git and Customizing Vim PreferencesThis will be old news to some, but I’ve always been told to write the blog post you wish you had when you started…medium.

comAn introduction to Git merge and rebase: what they are, and how to use themAs a Developer, many of us have to choose between Merge & Rebase.

With all the references we get from the internet…medium.

freecodecamp.

orgRebasing in Style: How to Customize Vim PreferencesThis is a continuation of my Intro to Interactive Rebasing in Git post from a few days ago, so start there if you want…medium.

comThe Git Rebase Introduction I Wish I'd HadOne of the most important (and confusing) git features in my new job was rebasing.

Looking back now, the worst part was…dev.

to.. More details

Leave a Reply