Getting geeky with Git #6. Interactive Rebase

Getting geeky with Git #6. Interactive Rebase

We explore how to perform interactive rebasing to modify existing commits. In the previous part of this series, we’ve looked into how rebase can improve our merges. It is a lot more powerful, though, and can come in handy in other situations. In this article, we explore how to perform interactive rebasing.

Introduction to the interactive rebase tool

When doing an interactive rebase, we can modify existing commits. This includes those that we’ve already pushed to our remote repository.

The above can prove to be useful in various scenarios. One of the most simple ones is fixing a typo in a commit message. Another might be adding a file that we forgot about. We might have also made a mistake in our code that we would like to fix without creating a new commit.

No matter what the cause is, we need to remember the golden rule of rebasing. Since it involves overwriting history, we should avoid doing that to branches used by other developers. Not following this principle will cause various issues to your teammates.

To start an interactive rebase, we need to use the   - i flag. The second thing to indicate is how far we want to go back when rebasing.

In the third part of this series, we’ve learned that usually, HEAD points to the same commit that the branch that we currently use. We can use this piece of information when performing an interactive rebase.

git rebase - i HEAD ~ 4

The above means that we will be able to modify four last commits.

You can also use  git __rebase--root to rebase all of your commits

Modifying existing commits

Let’s try the above on the repository for the NestJS series. When we do that, a text editor opens up.

pick d5e397d feat ( Files ) : configure AWS SDK

pick 535fa30 feat ( Files ) : upload files to Amazon S3

pick b54aa70 feat ( Users ) : link uploaded Files to the user

pick a1d4bc6 feat ( Users ) : add the possibility to delete avatars

An important thing to notice is that the above commits are in the reverse order. This is because when we run the above command, Git removes those commits and applies the included changes once again from the oldest to the newest. The useful thing is that we can change the commits in this process.

If we lave the   pick word at the beginning of the line, the commit will not change in the process of rebasing. We have a few options here, some of them being:

p , pick < commit > = use commit

r , reword < commit > = use commit , but edit the commit message

e , edit < commit > = use commit , but stop for amending

s , squash < commit > = use commit , but meld into previous commitf

Aside from choosing one of the possibile keywords, we can also remove commits or change their order

git interactive rebase tool

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Git Rebase Tutorial and Comparison with Git Merge

In this article, I will explain to you a few differences between git merge, git rebase, and the git interactive rebase.I will tell a bit about what pros...

Best Practices for Using Git

Git has become ubiquitous as the preferred version control system (VCS) used by developers. Using Git adds immense value especially for engineering teams where several developers work together since it becomes critical to have a system of integrating everyone's code reliably.

Do You Know the Difference Between Git Merge and Git Rebase

In this article we’ll be discussing git merge and git rebasecommands and when we should use them. git rebasedoes the same job as a git mergethat is merging the changes from one branch to another branch. The difference is that they do it very differently.

Tutorial how does Git Rebase work and compare with Git Merge and Git Interactive Rebase

<p>This article was originally published <a rel="canonical" href="https://www.blog.duomly.com/git-rebase-tutorial-and-comparison-with-git-merge/">Here</a></p> <h3 class="p1"><span class="s1"><b>Intro</b></span></h3> <p class="p1"><span...

Git Rebase | Git Rebase vs Git Merge | Rebase in Git Tutorial

Git Rebase tutorial will show you a different way of approaching git branching/merging with your master branch using the rebase command. What is Git Rebase? Why Rebase? Rebase Commands. Rebase Configuration Options. Pitfalls of Rebase