A Guide to Git Rebase with Practical Examples

Oleg Sucharevich
Level Up Coding
Published in
4 min readJun 7, 2022

--

Git rebase is a powerful tool that allows developers to rewrite history. Also, It is used to change the order of commits in a way that is not possible with git merge.

It’s worth mentioning that this tool can be used for more than just rewriting history. It also has many other use cases, such as

  1. Reordering commits
  2. Updating commits
  3. Deleting commits

Examples

Clone repository (https://github.com/facebook/react) — in order to avoid fetching all the commits we can clone the last 10 commits for our examples:

git clone — depth=100 https://github.com/facebook/react

After every example, your history will be different from origin/main. To align run:

git reset — hard HEAD~10
git clean -df
git pull origin main — rebase

. Reorder

Change Commit Order

Let’s start with an easy example. You are preparing your branch for code review. The code is OK, but something in the order of the commits does not look good, it might be just the order of the commits.

git log # find the commits you want to reorder
git rebase -i HEAD~5 # swap commits
git log # see the changes

Avoid “merge” commits

When working on a new feature branch it is common that at some point in time it will become behind the main branch. So we want to get all the recent changes, most of the time we just do git pull casing this “Merge” commit. It looks like this in Github pull request view:

This is fine since this is exactly what pull does, git fetch, and just after that git merge. Instead, we can avoid it using rebase “git pull — rebase”.

Update commit message

If we want to update the last commit message we can just use git commit — amend to update it.

When the update refers to an older commit the easiest way is to use interactive rebase.

First, find the git sha git log.

Then rebase

git rebase -i 52c434b~ and chose the commit for message update with “reword” command.

The next step is to update the commit message.

Use git log once again to see the change

Add/Update file to commit

Similar to the previous section when adding/updating files that should be added to the last commit, git commit — amend — no-edit to add change to the last commit without any message edit.

When a change should update a commit back in history use interactive rebase,git rebase -i 52c434b~, and using the edit command. Use git commit -a — amend — no-edit after you have changed all the files. To complete the process run git rebase — continue.

Notice: In some cases, the process will stop and ask to resolve conflicts.

Delete Commit

Merge two commits

Use the squash command to merge 2 or more commits that come in sequential order.

Discard commit with changes

Finally, sometimes deleting a bad commit is the only choice.

Start with git rebase HEAD~3 -i and just remove the line with the commit (one or more)

Summary

Rebasing is a git command that changes the order of commits in a branch. A rebase can be particularly helpful if you want to combine multiple commits into one or if you want to edit a commit’s message before pushing it to the remote repository.

Rebasing is often used when developing new features or making significant changes to the codebase and then sharing them with others on your team. This allows everyone to see the latest changes without having to merge their work with yours.

Avoid rebasing public branches such as main or long-living branches. Remember that there is no way back after pushing your rebased branch.

Level Up Coding

Thanks for being a part of our community! Before you go:

🚀👉 Join the Level Up talent collective and find an amazing job

--

--