Subscribe to working life
Get technology and equipment stories delivered to your inbox
Some developers find Git's rebase function mysterious, even dangerous! While a wrong reshuffle can cause annoying problems for your teammates, a reshuffle done correctly is perfectly safe. It's also a handy way to clean things up before moving them to the origin or merging them back to the master.
But "mysterious"? There is no need.
In this post, I will give a brief overview of Rebase and its cousin, the Interactive Rebase. Then we dive into using interactive rebaseQuellbaum(a free UI tool for working with Git commands) and the operations you can perform while doing it: Squash, Edit, Delete, Reword, and ReOrder.
Above
Don't have Sourcetree yet? Take a tour and download it for free atsourcetreeapp.com
Rebase against interactive rebase
You may have heard terms like "rewrite history" or "repeat your commits" associated with rebasing, which can be confusing if you're new to Git. Essentially, rebase is a way to change your commit history.
But why call the operation "Rebase"? This is because rebase allows you to choose a new base commit to use as the starting point for your feature branch. You can also rebase against a commit on your current branch and then reapply (or "replay") other commits, like this:
new base baseit's a good idea if you're about to merge with master and master has progressed since you split from it. By reapplying all commits from your branch to a different base commit, you remove any merge conflicts before starting the merge. And if you test with your reorganized branch (which you certainly would, because you're thorough, right?), you may discover integration issues before you push your changes back to source.
interactive rearrangementIt is a variant with which youCleanYour commit history before merging or moving to origin. When you change the commit history, you can apply 20/20 retrospectively to the description of the steps you took to get to that change. Git has a special file calledgit-rebase-allThis is simply a text file containing a list of the commits you are working on. Interactive trimming gives you the ability to edit this file and gives you some control over exactly what happens when you play back those commits.
warning
Although it may seem like you're just moving your commits to a different point in your repository's history, Git is actually creating new commits undercover, each with a new one.SCHA, so don't push commits you've already sent to the origin unless you're coordinating closely with your teammates. They then need to update their local copies of the repository.
Interactive rebase with Sourcetree
When you perform an interactive rebase from the command line, Git opens an editor where you can issue the commands that generate changesgit-rebase-all– this is something like a runbook that Git uses when it runs the rebase. Sourcetree accesses it in a similar waygit-rebase-alland edit it. The difference is in the user experience: you can interact with the rebase through a point-and-click user interface instead of having to memorize the commands and their syntax.
Above
learn more aboutthe internal mechanisms of interactive rebaseon our free Git tutorials page.
There are two ways to initiate an interactive reorganization in Sourcetree. The first is to right click (or context click) on a commit and select itInteractively rebase the children of <sha>.The second is to pull down.Repositorymenu and selectInteractive pass.
From there, you have the option to rewrite your repository's history with a few operations. Let's go over each one.
Squeeze
Smashing is a good way to clean up after a series of "panic commits": as you write a line of code, you're overwhelmed by fear of a sudden, widespread power outage, resulting in the loss of your work. Panic confirmations tend to be small and trivial in isolation. With squashing, you can combine small but related commits into a single, meaningful commit.
To use the squash function in Sourcetree, drag and drop rows on top of each other. Or you can use thoseSquash con anterioroption by right-clicking or using the button at the bottom of the dialog.

Editing Confirmation Content
Some confirmations are simply not quite correct. Butgit commit -modify commandFor example, you can change the commit message or add cascading changes to a previous commit.
To use this function, check the"Change commitment?"Check the box next to the confirmation you want to edit. If the reorganization continues, it falls back to Sourcetree, so you can do whatever you want before continuing.
Rephrase commit messages
When you're in the middle of solving a problem, you don't know how the whole story will read. But now that you figured it out, you do. By reframing engagement messages, you can tell the story in a way that your coworkers (and your future self) can understand.
On the interactive reorder screen, double-click the Description column. A window will appear allowing you to customize or replace the confirmation message entirely.
Rephrasing commit messages is also useful after adding staged changes to an existing commit. In fact, in such cases, you really should update the commit message so that it's clear to everyone what the commit now contains.
extinguish
This should be used with caution. But if you absolutely need to remove a commit (and you're absolutely sure it won't wreak havoc on your repository), you can do it as part of an interactive reorganization in Sourcetree.
As with squash and rephrase, double-click and select the commit description you want to remove"Clear confirmation". Y*blew it*- he went away.
reordering
Once upon a time, I got into trouble because a repository's commit history clearly showed that I was working on feature Y before feature X, against the project plan. I've also seen commit histories where the commits for feature X were scattered and nested with the commits for feature Y, making it difficult to get a sense of where feature X is in its development.
Whether you want to guard against references or group-related commits for easier assimilation, reordering commits is easy. Just drag and drop them into Sourcetree's"Rearrange and Supplement"Ventana.
Avoid confusion during interactive reorganization
"my" vs. "she"
Suppose you removed a commit, and Git now replays subsequent commits. Let's also assume that one of those later commits affects a file that was changed as part of the commit you removed. Git doesn't know which version of this file will reflect the other commits, so it has to ask you:Mineoare?
In this case,Minerefers to the changeset just before the commit you removed, andarerefers to the changeset right after the commit you removed. if you chooseMine, you will lose all changes made to that file after removing the commit. In this situation, act wisely and do not rush. But even if you make a mistake here, you can still reset your repositoryas long as you don't push the changes.
Upstream rebase acknowledgments
I've said it before, but it needs to be repeated: never reflow commits that have already been sent. This can lead to dangerous and confusing situations.
When you reorganize the committed commits, Sourcetree prompts you to deploy the changes after the reorganization is complete. This is really confusing when you know you've already made all the changes. Again, as long as you don't push those changes, you can save your team from this confusion by reverting your repository.
pass with confidence
Interactive rebase can be really useful, especially if you tend to commit locally all day and push your changes along the way. Until pressed, you can compress, delete, or edit the message in your commits to make the history easier for everyone to understand.
Give it a try next time you're working on a feature or fix it quickly. And tweet us@sourcetree if you run into any issues or have feedback for us. Happy coding!
To learn more about rebase, workflows, and other Git commands, visit our free tutorial site. You will find advanced tips for experts and tips for beginners.
Explore free Git tutorials and articles