This is the 30th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

In our daily work, there is usually a need to update historical commit information for some reason. Generally, there are the following scenarios:

  • Example Modify the latest commit information of the current branch
  • Example Modify the commit information of a historical COMMIT in the current branch
  • Modify all commit information for the current branch

For the current commit, we can display the specified submitter information.

git commit -m “Initial commit” –author=”mn [email protected]

After adding staging content to the local repository with the git commit command, Git generates the corresponding commit ID. We can then perform operations on the specified COMMIT ID, such as backing up to a commit ID, pulling the code specified by the COMMIT ID, and so on.

Let’s talk about the three cases above.

Modify the last commit information

Git commit –amend –author=” “[email protected]

Enter git commit –amend, enter edit mode, modify the commit information, and press wq to save and exit.

If you do not want to modify the submission information, add –no-edit as follows:

Git commit –amend –author=” amend “–no-edit

Git commit — The amend command only modifies the last commit, which requires git rebase

Modify the previous commit information

git rebase -iDescription:

Git rebase -i can compress and merge multiple commits.

Git rebase -i [startPoint] [endpoint]

[startPoint] [endpoint] specifies an editing interval. If [endpoint] is not specified, By default, the end of the interval is the COMMIT that the current branch HEAD points to.

First, we use Git rebase -i to select which commits to get repositioned.

For example, merge the last three commits:

git rebase -i HEAD~3

Alternatively, merge commit from current head to 15f745b(COMMIT ID) :

git rebase -i 15f745b

Then, change the beginning of pick to Edit in the list, and repeat the following command until complete:

git commit –amend –author=”mn [email protected]

After that, return to the normal state with the continue command.

git rebase –continue

Modify a specific commit information previously

View the log to find the commit ID of the last commit

git log

Then, git rebase to the commit before the commit you want to modify.

Git rebase fc8a3686bf5fcf4527873e075703a9998c127 – interactive, 928 (with the above git rebase – I like)

Change pick to edit in vi, save wq and exit, modify the content, git add and git commit –amend.

Modify all commit information

Prerequisite: Reset the user information of this project

git config user.name 'jelly'
git config user.email '[email protected]'
Copy the code

Git rebase

Let’s go back to the first commit of the current branch.

git rebase -i –root

Then, the editor pops up, and at the commit, change “Picked” to edit, then wq, and exit vi.

git commit –amend –reset-author

After that, return to the normal state with the continue command.

git rebase –continue

View logs to check whether the modification is successful

git log

Finally, force the push to the remote repository

git push origin master -f

Git filter-branch

Git filter-Branch is a fast and convenient way to use Git rebase.

For example, change the submitter [email protected] to [email protected]

# !/bin/sh

git filter-branch --env-filter 'OLD_EMAIL="[email protected]" CORRECT_EMAIL="[email protected]" if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ] then export GIT_COMMITTER_NAME="$CORRECT_NAME" export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL" fi if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ] then export GIT_AUTHOR_NAME="$CORRECT_NAME" export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL" fi ' --tag-name-filter cat -- --branches --tags
Copy the code

conclusion

Modify the commit information scenario Operation command
Example Modify the latest commit information git commit --amend
Example Modify the commit information of a historical COMMIT git rebase -i father_commitId, includingfather_commitIdIndicates the previous commitId
Modify the commit information for the first COMMIT git rebase -i --root

Note:

If you want to synchronize the modification information to a remote repository, you can run the git push -f command to forcibly synchronize the modification information. This operation overwrites the submission history of the remote branch.

Reference documentation

  • How do I change the commit information for the first COMMIT
  • Modify author and email in Git commit history
  • How do I modify git Commit author information
  • Git modifies author information in the commit history
  • Git rebase -I merge multiple commits