Git Commit Log Modification Method Instance Summary

  • 2021-08-31 09:49:25
  • OfStack

Situation 1: Last submitted and not push

Execute the following command:

git commit --amend

git will open the $EDITOR editor, which will load the log submitted this time, so that we can edit it and save it to complete the modification.

Case 2: Last committed and push to server

Execute the following command:

git commit --amend
git push origin master --force

And practice 1 in case 1. To push to a remote server using push, you need to add--force to have the server update the history.

It should be noted that the modified log is forced to push to Git server. If other people's local copies are modified, they may not be synchronized, so it is best to check with them.

Scenario 3: Old submitted and not pushed

Assuming that commit is the penultimate commit, this can be viewed using git log,

$ git log
commit b1b451d218cc23b6c769f373164f2b89cf54d0aa
Author: clcaza < clcaza@sina.cn >
Date: Sat Mar 10 19:09:08 2018 +0800

Add content d

commit 04f0d1809d5d31cc6e930efcba47a5f3f7e93319
Author: clcaza < clcaza@sina.cn >
Date: Sat Mar 10 19:08:24 2018 +0800

Add content c

commit 94fc8feb916442d56b558d5c370f18f057298921
Author: clcaza < clcaza@sina.cn >
Date: Sat Mar 10 19:07:08 2018 +0800

Add content a

commit fd517efa9faf6a5ec71d0eac38fbcfa0cd689f40
Author: clcaza < clcaza@sina.cn >
Date: Sat Mar 10 19:06:21 2018 +0800

init

Execute rebase

git rebase -i HEAD~3

It opens an editor, which displays the last three submissions, similar to:

pick 94fc8fe Addition a
pick 04f0d18 Add content c
pick b1b451d Added d

You will see that it is displayed in the order in which it was submitted, contrary to the order shown in git log. Navigate to the line where you want to edit the log, change pick to edit, and save.

The next step is to modify the log contents

git commit --amend

When you finish editing the log, remember to execute:

git rebase --continue

The purpose of Rebase is to open the history of submissions, allowing you to select what you want to modify. Git lets you modify content in a new branch. git rebase-continue takes you back to the previous branch.

Case 4: Old committed and push to server

The previous operation of editing the log is the same as Case 3:

git rebase -i HEAD~X
git commit --amend
git rebase --continue

X denotes the last few submissions.

When you finish editing the log, execute push:

git push origin master --force


Related articles: