Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to interactive rebase in Git

📅 2020-Jun-19 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ git, rebase ⬩ 📚 Archive

The interactive rebase in Git is one of its coolest features. It allows you to reorder, squash, delete and rename commits.

Since this feature allows you to change history, make sure to use it to only modify sections of the local DAG that have not yet been pushed to a remote. Do not use it to change parts of the DAG that are already in the remote and possibly already fetched by other people.

Interactive rebase requires a revision from you as input. Here are some examples:

$ git rebase -i HEAD~3
$ git rebase -i foobar_branch
$ git rebase -i foobar_tag
$ git rebase -i c35d286

Git will open your editor and display all the commits newer than the revision that you provided. These are the commits that you wil be allowed to manipulate.

Here is an example of what Git might show you:

pick 0083bd5 Some third commit
pick a0303c4 This is fourth commit
pick 19bfef8 I guess this is fifth commit

Remember that the commits are ordered from old to new as you go from top to bottom.

Here are the operations you can do on this commit list and then save-quit the file:

You can repeat and mix and match any of the above operations any number of times to do plastic surgery on your DAG until it looks perfect like you want it to.

Tried with: Git 2.17.1 and Ubuntu 18.04

© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘📧