Categories
Git

Git Rebase Interactive: A Step-by-Step Guide

Git Rebase Interactive is a powerful tool for rewriting commit history. Here’s how to use it:

Step 1: Rebase the Branch

git rebase -i "origin/remote-branch"

Step 2: Edit the Commit History

  • pick: Keep the commit as is
  • squash: Squash the commit into the previous commit
  • edit: Edit the commit message
  • drop: Remove the commit

Step 3: Save and Close

Save and close the interactive shell. Git will rebase the branch according to your changes.

Step 4: Push the Updated Branch

git push --force-with-lease origin local-branch

Example Output

$ git rebase "origin/remote-branch" -Xtheirs
$ git rebase -i "origin/remote-branch"
$ git push --force-with-lease origin local-branch

Conclusion

Git Rebase Interactive makes it easy to squash commits, reorder commits, and push the updated branch to the remote repository. Just remember to use git push --force-with-lease to update the remote branch with the new commit history.

Happy Coding 🙂