Edit Git project history
Git allows you to edit your project history. This is useful when you're working on a feature branch and want to clean it up and make it look the way you want before you share it with others. For example, you can edit commit messages, squash together smaller commits related to the same functionality, or split a commit that contains unrelated changes into separate commits, add changes to a previous commit, and so on.
Edit a commit message
If the only thing you need to change is a commit message, you can edit it before you push this commit.
Right-click the commit whose message you want to edit in the Log tab of the Git tool window Alt+9 and select Edit Commit Message from the context menu, or press F2.
In the dialog that opens, enter a new commit message and click OK.
Amend the previous commit
Sometimes you may commit too early and forget to add some files, or notice an error in the last commit that you want to fix without creating a separate commit.
You can do this by using the Amend commit option that appends staged changes to the previous commit. As a result, you end up with a single commit instead of two different ones.
In the Commit tool window Alt+0, select the modified files containing the changes you want to add to the previous commit.
Select the Amend checkbox so that the Commit button changes to Amend Commit and click it.
Amend any earlier commit
If you need to add changes to any earlier commit instead of committing them separately, you can do this by using the fixup
or the squash
action. Both commands append staged changes to the selected commit, but handle commit messages differently:
squash
adds the new commit message to the original commitfixup
discards the new commit message, leaving only the message from the original commit
In the Commit tool window Alt+0, select the modified files containing the changes you want to append to an earlier commit.
In the Log tab of the Git tool window Alt+9, right-click the commit that you want to modify with your local changes and select Fixup or Squash Into from the context menu.
Modify the commit message if necessary if you've chosen to squash changes.
Click the arrow on the Commit button and choose Commit and Rebase.
Squash commits
If you need to meld any two commits related to the same functionality, you can squash them into one for the sake of cleaner branch history.
In the Log tab of the Git tool window Alt+9 select the commits that you want to combine into one and choose Squash Commits from the context menu.
In the dialog that opens, edit the commit message (by default, it contains the messages from both commits) and click OK.
Push Ctrl+Shift+K the changes to the remote branch.
Drop a commit
You can discard a pushed commit in the current branch without creating an additional commit that reverts the changes.
Select a commit you want to discard in the Log view and choose Drop Commit from the context menu.
Edit project history by performing interactive rebase
With Git integration in IntelliJ IDEA, you can edit project history for the sake of making it linear and meaningful by performing interactive rebase. This allows you to clean up the commits history by altering individual commits, changing their order, squashing commits into one, skipping commits that contain extraneous changes, and so on before you integrate changes from your feature branch to another branch.
Edit the history of the current branch
IntelliJ IDEA allows you to edit the commits history in the current branch before you apply the changes to a different branch.
Open the Git tool window Alt+9 and switch to the Log tab.
Filter the log so that it only displays commits from the current branch:
Select the oldest commit in the series of commits that you want to edit, right-click it and choose Interactively Rebase from Here.
The Interactive Rebase dialog will be displayed containing the list of all commits in the current branch that were made after the selected commit:
If the Interactively Rebase from Here option is disabled, this may be due to one of the following reasons:
the selected commit has several parents
the selected commit is not in the current branch
the selected commit is pushed to a protected branch
To identify the reason, hover the action in the context menu and look for the message in the status bar:
You can perform the following changes to the branch history:
Change the order in which commits should be applied: use the and buttons to move commits up and down the list.
Pick a commit: this is the default state for all commits. If you need to undo an action you've already taken on a commit, click Pick so that this commit is applied as is.
Edit: click Stop to Edit so that when you start the rebase, you stop at this commit to be able to edit it.
When rebase is stopped at a commit, a notification pops up in the bottom-right corner of the IntelliJ IDEA window letting you continue or abort the rebase:
You can modify this commit using the context actions (such as Revert, Undo, Amend, and so on before continuing the rebase. If you don't perform any actions, this commit will be applied as is.
If you've closed the notification, from the main menu choose Git | Continue rebase to resume it.
Reword the commit message: click Reword or double-click a commit and edit the text in the mini-editor that opens.
Combine two commits into one: select the commit you want to meld into the previous one and click Squash or the arrow next to the Squash button and then Fixup.
If you click Squash, by default the messages from the two commits will be combined, so if you don't modify the resulting commit message this action will be reflected in the branch history.
If you click Fixup, the commit message of the fixup commit will be discarded, so this change will be invisible in the branch history.
In both cases, you will be able to edit the commit message in the mini editor that opens when you apply one of these actions.
Ignore a commit: click Drop so that the changes from the selected commit are not applied.
Undo all changes: click Reset to discard all actions you've applied to the commits.
As a result, the Rebasing Commits dialog shows a graph illustrating all actions you've applied to commits in your branch, so that you can review them before starting the rebase:
Click Start Rebasing.
Edit a branch history and integrate it into another branch
IntelliJ IDEA allows you to rebase a branch on top of another branch and edit the source branch history before you apply the changes.
From the main menu select
:Click Modify options and choose --interactive.
From the list, select the target branch onto which you want to rebase the current branch:
If you need to rebase the source branch starting from a particular commit instead of rebasing the entire branch, click Modify options and choose --onto. In the source branch field, enter the hash of the commit starting from which you want to apply the current branch to the new base:
If the branch you want to rebase is not currently checked out, click Modify options, click Select another branch to rebase, and choose a branch from the list that appears:
IntelliJ IDEA will check out this branch before starting the rebase operation.
If you want to rebase all commits reachable in the branch, click Modify options and choose --root (for more information on this option, see git-rebase).
If you need to keep empty commits, which are commits that do not change anything from their parent, click Modify options and choose --keep-empty (for more information on this option, see git-rebase).
Click Rebase.
The Interactive Rebase dialog will be displayed containing the list of all commits in the current branch that were made after the selected commit.
You can perform the following changes to the branch history:
Change the order in which commits should be applied: use the and buttons to move commits up and down the list.
Pick a commit: this is the default state for all commits. If you need to undo an action you've already taken on a commit, click Pick so that this commit is applied as is.
Edit: click Stop to Edit so that when you start the rebase, you stop at this commit to be able to edit it.
When rebase is stopped at a commit, a notification pops up in the bottom-right corner of the IntelliJ IDEA window letting you continue or abort the rebase:
You can modify this commit using the context actions (such as Revert, Undo, Amend, and so on before continuing the rebase. If you don't perform any actions, this commit will be applied as is.
If you've closed the notification, from the main menu choose Git | Continue rebase to resume it.
Reword the commit message: click Reword or double-click a commit and edit the text in the mini-editor that opens.
Combine two commits into one: select the commit you want to meld into the previous one and click Squash or the arrow next to the Squash button and then Fixup.
If you click Squash, by default the messages from the two commits will be combined, so if you don't modify the resulting commit message this action will be reflected in the branch history.
If you click Fixup, the commit message of the fixup commit will be discarded, so this change will be invisible in the branch history.
In both cases, you will be able to edit the commit message in the mini editor that opens when you apply one of these actions.
Ignore a commit: click Drop so that the changes from the selected commit are not applied.
Undo all changes: click Reset to discard all actions you've applied to the commits.
As a result, the Rebasing Commits dialog shows a graph illustrating all actions you've applied to commits in your branch, so that you can review them before starting the rebase:
Click Start Rebasing.