# 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](apply-changes-from-one-branch-to-another.html) 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.

> **Warning:**
> Avoid modifying the history for a remote branch with multiple contributors unless absolutely necessary, for example, if you accidentally pushed some sensitive data.
>
>
>
> Pushing modifications that rewrite a branch history to the remote repository will be rejected to prevent data loss, so you will have to use [force push](commit-and-push-changes.html#force-push).
>
>
>
> You cannot modify the history for protected branches where `git push --force` is not allowed (configure protected branches in  the Version Control | Git settings page `Ctrl+Alt+S` (Windows), `⌘ Comma` (macOS), `⌘ Comma` (IntelliJ IDEA Classic (macOS)), `⌘ Comma` (macOS System Shortcuts), `Ctrl+Alt+S` (XWin), `Ctrl+Alt+S` (GNOME), `Ctrl+Alt+S` (KDE), `Ctrl+Alt+S` (Emacs), `Ctrl+Alt+S` (Sublime Text), `⌘ Comma` (Sublime Text (macOS)), `Ctrl+Alt+S` (NetBeans), `Ctrl+Alt+S` (Visual Studio), `⌘ Comma` (Visual Studio (macOS)), `Ctrl+Alt+S` (Eclipse), `⌘ Comma` (Eclipse (macOS)) . Note that if a branch is marked as protected on GitHub, IntelliJ IDEA will automatically mark it as protected when you check it out.
>
>
>
> Also, you cannot perform actions that modify a branch history for commits that are not contained in the branch currently checked out.

## 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.

Procedure:

1. Right-click the commit whose message you want to edit in the  Log tab of the   Git tool window  `Alt+9` (Windows), `⌘ 9` (macOS), `⌘ 9` (IntelliJ IDEA Classic (macOS)), `⌘ 9` (macOS System Shortcuts), `Alt+9` (XWin), `Alt+9` (GNOME), `Alt+9` (KDE), `Alt+9` (Emacs), `Alt+9` (Sublime Text), `⌘ 9` (Sublime Text (macOS)), `Alt+9` (NetBeans), `Alt+9` (Visual Studio), `⌘ 9` (Visual Studio (macOS)), `Alt+9` (Eclipse), `⌘ 9` (Eclipse (macOS))   and select Edit Commit Message from the context menu, or press `F2` (Windows), `F2` (macOS), `F2` (IntelliJ IDEA Classic (macOS)), `F2` (macOS System Shortcuts), `F2` (XWin), `F2` (GNOME), `F2` (KDE), `F2` (Emacs), `F2` (Sublime Text), `F2` (Sublime Text (macOS)), `Ctrl+R` (NetBeans), `Ctrl+R, R` (Visual Studio), `F2` (Visual Studio (macOS)), `Alt+Shift+R` (Eclipse), `F2` (Eclipse (macOS)).

2. In the dialog that opens, enter a new commit message and click OK.

> **Note:**
> You can also modify the commit message right after committing by clicking Edit Commit Message in the notification about the successful commit.

## Amend local commits

Sometimes you may commit too early and forget to add some files, or notice an error in one of your unpushed commits 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 chosen local commit. As a result, you end up with a single commit instead of two different ones.

Procedure:

1. In the Commit tool window `Alt+0` (Windows), `⌘ 0` (macOS), `⌘ 0` (IntelliJ IDEA Classic (macOS)), `⌘ 0` (macOS System Shortcuts), `Alt+0` (XWin), `Alt+0` (GNOME), `Alt+0` (KDE), `Alt+0` (Emacs), `Alt+0` (Sublime Text), `⌘ 0` (Sublime Text (macOS)), `Alt+0` (NetBeans), `Alt+0` (Visual Studio), `Alt+0` (Visual Studio (macOS)), `Alt+0` (Eclipse), `⌘ 0` (Eclipse (macOS)), select the modified files containing the changes you want to add to one of your local commits.

2. Select the Amend option and click ![the Down icon](https://resources.jetbrains.com.cn/help/img/idea/2026.2/app.expui.general.chevronDownLarge.svg) to choose the local commit you want to modify.

3. Click Amend Commit.

## Amend any earlier commit

> **Tip:**
> For amending local commits, refer to [Amend local commits](#amend-commit).

If you need to add changes to any earlier commit that has already been pushed, 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 commit

* `fixup` discards the new commit message, leaving only the message from the original commit

> **Note:**
> Both commands require a [rebase](apply-changes-from-one-branch-to-another.html#rebase-branch) because they change the commit hashes.

Procedure:

1. In the Commit tool window `Alt+0` (Windows), `⌘ 0` (macOS), `⌘ 0` (IntelliJ IDEA Classic (macOS)), `⌘ 0` (macOS System Shortcuts), `Alt+0` (XWin), `Alt+0` (GNOME), `Alt+0` (KDE), `Alt+0` (Emacs), `Alt+0` (Sublime Text), `⌘ 0` (Sublime Text (macOS)), `Alt+0` (NetBeans), `Alt+0` (Visual Studio), `Alt+0` (Visual Studio (macOS)), `Alt+0` (Eclipse), `⌘ 0` (Eclipse (macOS)), select the modified files containing the changes you want to append to an earlier commit.

2. In the  Log tab of the   Git tool window  `Alt+9` (Windows), `⌘ 9` (macOS), `⌘ 9` (IntelliJ IDEA Classic (macOS)), `⌘ 9` (macOS System Shortcuts), `Alt+9` (XWin), `Alt+9` (GNOME), `Alt+9` (KDE), `Alt+9` (Emacs), `Alt+9` (Sublime Text), `⌘ 9` (Sublime Text (macOS)), `Alt+9` (NetBeans), `Alt+9` (Visual Studio), `⌘ 9` (Visual Studio (macOS)), `Alt+9` (Eclipse), `⌘ 9` (Eclipse (macOS))  , right-click the commit that you want to modify with your local changes and select Fixup or Squash Into from the context menu.

> **Tip:**
> You can also `fixup` or `squash` your changes when [reviewing file history](investigate-changes.html#file-history) in the History tab of the   Version Control tool window  `Alt+9` (Windows), `⌘ 9` (macOS), `⌘ 9` (IntelliJ IDEA Classic (macOS)), `⌘ 9` (macOS System Shortcuts), `Alt+9` (XWin), `Alt+9` (GNOME), `Alt+9` (KDE), `Alt+9` (Emacs), `Alt+9` (Sublime Text), `⌘ 9` (Sublime Text (macOS)), `Alt+9` (NetBeans), `Alt+9` (Visual Studio), `⌘ 9` (Visual Studio (macOS)), `Alt+9` (Eclipse), `⌘ 9` (Eclipse (macOS)) .

3. Modify the commit message if you've chosen to squash changes.

4. 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.

Procedure:

1. In the  Log tab of the   Git tool window  `Alt+9` (Windows), `⌘ 9` (macOS), `⌘ 9` (IntelliJ IDEA Classic (macOS)), `⌘ 9` (macOS System Shortcuts), `Alt+9` (XWin), `Alt+9` (GNOME), `Alt+9` (KDE), `Alt+9` (Emacs), `Alt+9` (Sublime Text), `⌘ 9` (Sublime Text (macOS)), `Alt+9` (NetBeans), `Alt+9` (Visual Studio), `⌘ 9` (Visual Studio (macOS)), `Alt+9` (Eclipse), `⌘ 9` (Eclipse (macOS))   select the commits that you want to combine into one and choose Squash Commits from the context menu.

2. In the dialog that opens, edit the commit message (by default, it contains the messages from both commits) and click OK.

3. Push `Ctrl+Shift+K` (Windows), `⌘ ⇧ K` (macOS), `⌘ ⇧ K` (IntelliJ IDEA Classic (macOS)), `⌘ ⇧ K` (macOS System Shortcuts), `Ctrl+Shift+K` (XWin), `Ctrl+Shift+K` (GNOME), `Ctrl+Shift+K` (KDE), `Ctrl+Shift+K` (Emacs), `Ctrl+Shift+K` (Sublime Text), `⌘ ⇧ K` (Sublime Text (macOS)), `Ctrl+Shift+K` (NetBeans), `Ctrl+Shift+K` (Visual Studio), `⌘ ⇧ K` (Visual Studio (macOS)), `Ctrl+Shift+K` (Eclipse), `⌘ ⇧ K` (Eclipse (macOS)) 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](undo-changes.html#revert-commit).

Procedure:

* Select a commit you want to discard in the Log view and choose Drop Commit from the context menu.

## Extract selected changes

If you want to split the already committed changes into several commits, you can extract the changes in selected files into a separate commit.

Procedure:

1. In the  Log tab of the   Git tool window  `Alt+9` (Windows), `⌘ 9` (macOS), `⌘ 9` (IntelliJ IDEA Classic (macOS)), `⌘ 9` (macOS System Shortcuts), `Alt+9` (XWin), `Alt+9` (GNOME), `Alt+9` (KDE), `Alt+9` (Emacs), `Alt+9` (Sublime Text), `⌘ 9` (Sublime Text (macOS)), `Alt+9` (NetBeans), `Alt+9` (Visual Studio), `⌘ 9` (Visual Studio (macOS)), `Alt+9` (Eclipse), `⌘ 9` (Eclipse (macOS))  , select the local branch and then the commit you want to split.

2. In the Changed Files pane on the right, select the files with the changes you want to extract, right-click them, and select Extract Selected Changes to Separate Commit.

3. In the dialog that opens, enter the new commit message if necessary.

As a result, there will be two commits with new hashes.

> **Tip:**
> If the commit that you want to split was already pushed, it is still possible to split it. However, you will have to either [use force push](commit-and-push-changes.html#force-push) (cannot be performed in protected branches) or [update your project](sync-with-a-remote-repository.html#update-git-branch) first to sync the local branch with the remote tracked branch and then push the merge commit.

