Sling Academy
Home/DevOps/Git Rebasing: A Practical Guide (with Examples)

Git Rebasing: A Practical Guide (with Examples)

Last updated: January 28, 2024

Introduction

Understanding the intricacies of version control is pivotal for streamlining development workflows. Git, as a distributed version control system, provides a plethora of powerful tools, one being rebasing. This guide is intended to offer practical insights into Git rebasing along with coherent examples to help improve your code management skills.

Why Rebase?

Rebasing is a Git process that integrates changes from one branch into another. Unlike merging, which creates a merge commit in the process, rebasing rewrites the project history by applying commits from one branch onto another, resulting in a linear project history.

Rebasing can make your project’s history cleaner and simpler. It’s often used before merging a feature branch into the main branch to simplify potential conflicts and to tidy up a series of otherwise messy commits.

How to Perform a Rebase

Let’s go through the basic steps to perform a rebase.

Step 1: Update the Base Branch

git checkout main
 git pull origin main

It’s essential to ensure that the branch you’re rebasing onto is updated to the latest version.

Step 2: Start the Rebase

git checkout feature-branch
 git rebase main

This command sequence rebases the feature branch onto the main branch. Resolving any conflicts manually might be necessary.

Resolving Conflicts

If there’s a conflict, Git will pause the rebase so that you can resolve it. Once resolved, you can continue the rebase:

git add .
 git rebase --continue

If at any point you’d like to halt the rebase, you can do so with the command:

git rebase --abort

Interactive Rebase

An interactive rebase allows you to modify commits in the process. To start an interactive rebase:

git rebase -i HEAD~3

This command lets you manipulate the last three commits. You could squash commits, rewrite messages, or drop them altogether.

The Golden Rule of Rebasing

Never rebase commits that exist outside your repository. If you rebase commits that have been pushed and then push them again, the repository history will diverge, causing issues for others collaborating on the project.

Examples in Action

Let’s illustrate with examples:

Example 1: Basic Rebasing

#!/bin/bash
 # Assuming main is the base branch
 git checkout feature-branch
 git rebase main
 # Resolve any conflicts
 git add .
 git rebase --continue

Example 2: Interactive Rebase

#!/bin/bash
 git rebase -i HEAD~5
 # An editor opens showing the last 5 commits
 # You can now choose to squash, edit, or drop commits

Example 3: Skipping a Commit

#!/bin/bash
 git rebase -i HEAD~5
 # In the interactive list, change 'pick' to 'drop' for commits you want to remove

Conclusion

Rebasing can simplify your project’s history and streamline the merge process. That said, its power should be handled with care—especially when dealing with a shared repository. With practice, rebasing will become a natural extension to your Git toolset, enhancing your collaborative and individual coding efforts.

Always remember the golden rule of rebasing, and happy coding!

Next Article: How to Clean Up Git History with Interactive Rebase

Previous Article: Working with the Readme.md file in Git and GitHub

Series: Git & GitHub Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide