How to modify commit history
When you use a versioning system such as Git, you may need to modify the commit history. This can be necessary to fix a spelling error, remove sensitive information, or reorganize commits. This topic describes how to modify your commit history.
Background information
The Codeup platform provides a service that uses custom rules to check commits before they are pushed.
After you enable this service, a push can fail because of an incorrect commit comment format, an invalid email setting, or a code owner mismatch.
If the code owner is invalid, see Push rule settings.
If the commit comment format or email information is incorrect, see the following sections to learn how to modify the commit history.
1. Modify commit messages
Modify the last commit
To modify only the message for the last commit, run the following command:
$ git commit --amendThis command opens your text editor. Modify and save the message to update the last commit.
Modify multiple commit messages
Git does not have a dedicated tool for changing history. However, you can use rebase to modify a series of commits.
To run rebase interactively, add the -i option to the git rebase command. You must specify how far back in the history you want to rewrite.
For example, to modify the last three commit messages, pass `HEAD~3` as an argument to the git rebase -i command.
$ git rebase -i HEAD~3Remember that this is a rebase command. Every commit in the `HEAD~3..HEAD` range is rewritten, regardless of whether you change its message. Do not rebase commits that you have already pushed to a remote server. Doing so creates two versions of the same change, which can confuse other contributors.
Note that these commits are listed in the reverse order of the `git log` command. The `git log` command shows output similar to the following:
$ git log --pretty=format:"%h %s" HEAD~3..HEAD
a5f4a0d added cat-file
310154e updated README formatting and added blame
f7f3f6d changed my name a bitThe interactive rebase starts from the commit that you specified (`HEAD~3`). It then replays the changes from each commit in the range, from top to bottom.
To modify a commit message, change the word `pick` to `edit` for that commit.
For example, to modify only the third commit message, edit the file as follows:
edit f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-fileAfter you save and exit the editor, Git displays the commit message:
$ git rebase -i HEAD~3
Stopped at f7f3f6d... changed my name a bit
You can amend the commit now, with
git commit --amend
Once you’re satisfied with your changes, run
git rebase --continueNext, enter the following:
$ git commit --amendChange the commit message, exit the editor, and then run this command:
$ git rebase --continueThis command automatically applies the other two commits, and the rebase is complete.
If you marked multiple commits with `edit`, repeat these steps for each one.
Use a tool for quick modification
For installation and usage instructions, see git-filter-repo.
2. Modify commit authors
Modify the author of the last commit
Commit message for a modification similar to the one described in paragraph 1:
$ git commit --amend --reset-authorModify authors of multiple commits
You can run git rebase interactively by adding the -i option. You must specify how far back in the commit history you want to rewrite. This process is similar to modifying commit messages. The command is as follows:
$ git commit --amend --reset-author3. Modify commit emails
To quickly make modifications, see the installation and usage tutorial for git-filter-repo.
4. Set up local pre-check rules
To avoid modifying commit history, we recommend using a local Git pre-receive hook to check for commit rule compliance and detect non-compliant commits.
What are Git hooks?
Git lets you automatically trigger custom scripts when specific actions occur. Client-side hooks are triggered by operations such as committing and merging. Server-side hooks run for operations such as receiving pushed commits. You can use these hooks to set up any checks that you need.
How to install hooks
Hooks are stored in the hooks subdirectory of your Git directory. In most projects, the path is .git/hooks.
When you run git init, Git populates the `hooks` directory with example scripts. These scripts show the parameters that are passed to each hook. The examples are written as shell scripts, but you can use any executable script, such as a Ruby or Python script. The example files have a `.sample` extension. To enable a hook, remove the `.sample` extension from the filename.
To activate a hook, place a correctly named, executable file in the hooks subdirectory of your Git directory.
The pre-receive hook
The pre-receive hook is the first script that runs when handling a push operation. It receives a list of the commits that are being pushed. If this script exits with a non-zero value, the entire push is rejected.
You can use this hook locally to run rule checks on all commits in a push to prevent rejection by the remote repository for non-compliance.
For more information about hooks, see Git - Git Hooks.