Interactive Rebase: Clear up your Commit Historical past

No Comments

This text is a part of our “Superior Git” collection. You should definitely comply with Tower on Twitter or join their e-newsletter to listen to in regards to the subsequent articles.

Interactive Rebase is the Swiss Military knife of Git instructions: a number of use instances and many prospects! It’s actually an amazing addition to any developer’s instrument chain, as a result of it enables you to revise your native commit historical past—earlier than you share your work with the remainder of the staff.

Let’s see what you are able to do with an interactive rebase after which take a look at some sensible examples.

Superior Git collection:

Half 1: Creating the Excellent Commit in GitHalf 2: Branching Methods in GitHalf 3: Higher Collaboration With Pull RequestsHalf 4: Merge ConflictsHalf 5: Rebase vs. MergeHalf 6: Interactive Rebase (You’re right here!)Half 7: Cherry-Selecting Commits in Git (Coming quickly!)Half 8: Utilizing the Reflog to Restore Misplaced Commits

Rewriting your commit historical past

Briefly, interactive rebase lets you manipulate your commit historical past. It’s meant for optimizing and cleansing up. You’ll be able to…

change commit messagescombine a number of commitssplit and edit current commitsreorder commitsdelete commits

Remember that an interactive rebase rewrites your commit historical past: all the concerned commits get a brand new hash ID. Additionally, a fast reminder: commit IDs are there to determine commits—they’re SHA-1 checksums. So, by altering that hash, you technically create utterly new commits. Because of this you shouldn’t use an interactive rebase on stuff that you simply’ve already pushed to a shared distant repository. Your colleagues may need primarily based their work on these commits—and while you use interactive rebase to rewrite commit historical past, you might be altering these base commits.

All of which means an interactive rebase is supposed that can assist you clear up and optimize your individual native commit historical past earlier than you merge (and probably push) it again right into a shared staff department.

Interactive rebase workflow

Earlier than we take interactive rebase for a take a look at drive, let’s take a look at the final workflow. That is at all times the identical, it doesn’t matter what precisely we’re doing—deleting a commit, altering a commit message, combining commits… the steps are an identical.

Step one is to find out the vary of commits you wish to manipulate. How far again in time do you wish to go? After you have the reply, you can begin your interactive rebase session. Right here, you could have the possibility to edit your commit historical past. For instance, you may manipulate the chosen commits by reordering, deleting, combining them, and so forth.

In your first step, you might be at all times going to take a look at the present state of the commit historical past. You need to use the git log command to look at a undertaking’s historical past and present the commit log.

Right here’s the little instance repository we’re going to make use of all through this text:

Be aware that I’m utilizing the Tower Git desktop GUI in a few of my screenshots for simpler visualization.

After you’ve examined the checklist, it’s time to start out the work. Let’s do that step-by-step. Within the examples of this text, we are going to do the next issues:

First, we modify an previous commit’s message.Secondly, we mix two previous commits.After that, we break up one commit.Lastly, we delete a commit.

Change a commit message

In lots of instances, you’ll wish to change the latest commit. Remember that there’s a shortcut for this state of affairs which doesn’t contain interactive rebase:

$ git commit –amend

This command can modify each the content material and the message of the latest commit, and it opens your default textual content editor. Right here you can also make your modifications, save them, and stop the editor. This is not going to solely replace the commit message, however will successfully change the commit itself and write a brand new one.

Once more, please watch out and don’t amend your final commit should you’ve already pushed it to the distant repository!

For some other commit (something older than the latest one), it’s a must to carry out an interactive rebase. To run git rebase interactively, add the -i possibility. 

Step one is to find out the bottom commit: the mum or dad commit of the one you wish to change. You’ll be able to obtain this through the use of the commit’s hash ID or by doing slightly little bit of counting. To vary the final three commit messages (or at the least considered one of them), you may outline the mum or dad commit like this:

$ git rebase -i HEAD~3

An editor window opens and you’ll see all three commits you chose (and by “chosen” I imply a spread of commits: from HEAD all the way in which all the way down to HEAD~3). Please discover the reverse order: not like git log, this editor reveals the oldest commit (HEAD~3) on the prime and the latest on the backside.

On this window you don’t really change the commit message. You solely inform Git what sort of manipulation you wish to carry out. Git presents a collection of key phrases for this—in our case, we modify the phrase choose to reword which permits us to vary the commit messages. After saving and shutting the editor, Git will present the precise commit message and you’ll change it. Save and exit once more, that’s it!

Combining two commits

On this subsequent instance, we’ll mix the 2 commits—“7b2317cf Change the web page construction” and “6bcf266 Optimize markup”—in order that they develop into one single commit. Once more, as a primary step it’s good to decide the bottom commit. And once more, we’ve got to return to at the least the mum or dad commit:

$ git rebase -i HEAD~3

The editor window opens once more, however as a substitute of reword, we’ll enter squash. To be actual, we substitute choose with squash in line 2 to mix it with line 1. This is a vital bit to bear in mind: the squash key phrase combines the road you mark up with the road above it!

After saving the modifications and shutting the window, a brand new editor window pops up. Why’s that? By combining two commits we’re creating… effectively… a brand new commit! And this new commit needs a commit message. Enter the message, save and shut the window… and also you’ve efficiently mixed the 2 commits. Highly effective stuff!

Lastly slightly “professional tip” for these of you working with the “Tower” Git desktop GUI: to carry out a squash, you may merely drag and drop commits onto one another, proper within the commits view. And if you wish to change a commit message, merely proper click on the commit in query and choose “Edit commit message” from the contextual menu.

Deleting a commit

We’re bringing within the huge weapons for our closing instance: we’re going to delete a revision from our commit historical past! To do that, we’re utilizing the drop key phrase to mark up the commit we wish to do away with:

drop 0023cdd Add easy robots.txt
choose 2b504be Change headlines for about and imprint
choose 6bcf266 Optimizes markup construction in index web page

That is in all probability a very good second to reply a query you may need had for a while now: what are you able to do should you’re in the midst of a rebase operation and assume, “Oh, no, this wasn’t such a good suggestion in spite of everything”? No drawback—you may at all times abort! Simply enter the next command to show again to the state your repository was in earlier than you initiated the rebase:

$ git rebase –abort

Altering the previous

These had been only a few examples of what an interactive rebase can do. There are many different prospects to manage and revise your native commit historical past.

If you wish to dive deeper into superior Git instruments, be happy to take a look at my (free!) “Superior Git Equipment”: it’s a set of quick movies about subjects like branching methods, Interactive Rebase, Reflog, Submodules and far more.

Completely happy rebasing and hacking—and see you quickly for the following half in our collection on “Superior Git”!

Superior Git collection:

Half 1: Creating the Excellent Commit in GitHalf 2: Branching Methods in GitHalf 3: Higher Collaboration With Pull RequestsHalf 4: Merge ConflictsHalf 5: Rebase vs. MergeHalf 6: Interactive Rebase (You’re right here!)Half 7: Cherry-Selecting Commits in Git (Coming quickly!)Half 8: Utilizing the Reflog to Restore Misplaced Commits

The publish Interactive Rebase: Clear up your Commit Historical past appeared first on CSS-Tips. You’ll be able to help CSS-Tips by being an MVP Supporter.

    About Marketing Solution Australia

    We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

    Request a free quote

    We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

    Subscribe to our newsletter!

    More from our blog

    See all posts

    Leave a Comment