Data report"State of code review 2024" is now liveRead the full report

git rebase using HEAD

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


The git rebase command enables developers to modify their commit history in a clean and structured manner. It is especially useful in projects where a clean, linear progression of changes is preferred. This guide will focus on how to use the git rebase command with the HEAD pointer, including common scenarios like rebasing the last few commits.

Rebasing is a process of moving or combining a sequence of commits to a new base commit. git rebase can be highly beneficial for simplifying complex commit logs, resolving conflicts out of band, and ensuring that your work does not diverge too far from the work being done by the rest of your team.

  • HEAD: The HEAD is a reference to the last commit in the current checked-out branch. When you rebase, you're often moving commits to a new parent commit referenced relative to the HEAD.
  • Rebasing vs. merging: While merging takes the contents of two branches and integrates them together, rebasing takes the changes that were made in one branch and replays them on another branch, ensuring a clean, linear commit history. Merging on the other hand creates a "merge commit" each time the merge operation is performed, cluttering the history.

To rebase the current branch’s commits onto a more recent commit, you can use the git rebase command. For instance, to modify the last commit, which HEAD points to, you can execute:

Terminal
git rebase -i HEAD~1

Here, HEAD~1 refers to the commit just before the current HEAD. The -i flag initiates an interactive rebase session, which allows for more detailed control over what happens to each commit during the rebase.

This command opens a text editor with a list of commits starting from the specified commit (HEAD~1 in this case). The editor displays each commit on a new line prefixed with the word pick, and you can change this prefix to different commands to tell Git how to treat each commit.

This interactive process allows you to reshape the history of your branch, making it possible to clean up commit messages, squash unnecessary commits, or split comprehensive changes into smaller, more manageable chunks. By rebasing with HEAD~1, you can directly modify the last commit, which is useful for quick amendments or corrections to the most recent changes in your branch.

If you need to rebase multiple recent commits, you can use:

Terminal
git rebase -i HEAD~N

Replace N with the number of commits you wish to rebase. For example, git rebase -i HEAD~3 starts an interactive rebase for the last three commits.

  1. Start interactive rebase:

    Terminal
    git rebase -i HEAD~3

    This command will open your default text editor with a list of the last three commits from the current branch.

  2. Choose Actions for Each Commit: In the text editor, you will see each commit from the oldest to the newest with the word pick in front of them. You can change pick to other commands like:

    • reword: to change the commit message
    • edit: to amend the commit
    • squash: to combine this commit with the previous commit
    • fixup: to combine this commit's changes into the previous commit without keeping this commit's log message
    • drop: to delete the commit
  3. Save and close the editor: After you have made your changes, save and close the editor. Git will then start rebasing the commits as per your instructions.

If a conflict arises during rebasing, Git will stop and allow you to fix the conflicts manually. Here's how to resolve these conflicts:

  1. Edit the files listed by Git as having conflicts and resolve the issues.
  2. Add the resolved files to the staging area using:
    Terminal
    git add <resolved-file>
  3. Continue the rebase with:
    Terminal
    git rebase --continue
  4. Abort the rebase if you decide that continuing is not the best course of action:
    Terminal
    git rebase --abort
    For a more detailed step-by-step walkthrough, see this guide on resolving merge conflicts.

For more detail on rebasing see the official Git documentation.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Learn more

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2