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