Changing a commit message - git修改commit信息

本文详细指导如何在Git中修正提交消息,包括本地修改、未推送到GitHub的改动,以及如何处理已推送历史。涉及使用`git commit --amend`、force push及其风险,以及交互式rebase进行多 commits 消息修改的方法。

Changing a commit message

If a commit message contains unclear, incorrect, or sensitive information, you can amend it locally and push a new commit with a new message to GitHub. You can also change a commit message to add missing information.

Rewriting the most recent commit message

You can change the most recent commit message using the git commit --amend command.

In Git, the text of the commit message is part of the commit. Changing the commit message will change the commit ID–i.e., the SHA1 checksum that names the commit. Effectively, you are creating a new commit that replaces the old one.

Commit has not been pushed online

If the commit only exists in your local repository and has not been pushed to GitHub, you can amend the commit message with the git commit --amend command.

  1. On the command line, navigate to the repository that contains the commit you want to amend.

  2. Type git commit --amend and press Enter.

  3. In your text editor, edit the commit message, and save the commit.

The new commit and message will appear on GitHub the next time you push.

You can change the default text editor for Git by changing the core.editor setting. For more information, see “Basic Client Configuration” in the Git manual.

Amending older or multiple commit messages

If you have already pushed the commit to GitHub, you will have to force push a commit with an amended message.

We strongly discourage force pushing, since this changes the history of your repository. If you force push, people who have already cloned your repository will have to manually fix their local history. For more information, see “Recovering from upstream rebase” in the Git manual.

Changing the message of the most recently pushed commit

  1. Follow the steps above to amend the commit message.
  2. Use the push --force-with-lease command to force push over the old commit.
    git push --force-with-lease example-branch
    

Changing the message of older or multiple commit messages

If you need to amend the message for multiple commits or an older commit, you can use interactive rebase, then force push to change the commit history.

  1. On the command line, navigate to the repository that contains the commit you want to amend.

  2. Use the git rebase -i HEAD~n command to display a list of the last n commits in your default text editor.

    # Displays a list of the last 3 commits on the current branch
    $ git rebase -i HEAD~3
    

    The list will look similar to the following:

    pick e499d89 Delete CNAME
    pick 0c39034 Better README
    pick f7fde4a Change the commit message but push the same commit.
    # Rebase 9fdb3bd..f7fde4a onto 9fdb3bd
    #
    # Commands:
    # p, pick = use commit
    # r, reword = use commit, but edit the commit message
    # e, edit = use commit, but stop for amending
    # s, squash = use commit, but meld into previous commit
    # f, fixup = like "squash", but discard this commit's log message
    # x, exec = run command (the rest of the line) using shell
    #
    # These lines can be re-ordered; they are executed from top to bottom.
    #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    #
    # However, if you remove everything, the rebase will be aborted.
    #
    # Note that empty commits are commented out
    
  3. Replace pick with reword before each commit message you want to change.

    pick e499d89 Delete CNAME
    reword 0c39034 Better README
    reword f7fde4a Change the commit message but push the same commit.
    
  4. Save and close the commit list file.

  5. In each resulting commit file, type the new commit message, save the file, and close it.

  6. When you’re ready to push your changes to GitHub, use the push --force command to force push over the old commit.

    $ git push --force example-branch
    

For more information on interactive rebase, see “Interactive mode” in the Git manual.

As before, amending the commit message will result in a new commit with a new ID. However, in this case, every commit that follows the amended commit will also get a new ID because each commit also contains the id of its parent.

If you have included sensitive information in a commit message, force pushing a commit with an amended commit may not remove the original commit from GitHub. The old commit will not be a part of a subsequent clone; however, it may still be cached on GitHub and accessible via the commit ID. You must contact GitHub Support with the old commit ID to have it purged from the remote repository.

现有commit-msg如下格式,要求修改git hook脚本,能够统一更改commit-msg格式: (1)中英文标题行保持不变,Signed-off-by和change-id行保持不变 (2)将英文内容挪到----------分割线之前,中文内容挪到----------分割线之后 [mobile] Modify the automatic PIN release conditions for SIM cards [mobile]修改sim卡自动解PIN条件 [问题背景] 用户开启auto-unlock PIN后,当尝试次数>1次时设备会用保存的PIN码尝试解PIN 若用户换卡后,反复插拔会导致只剩一次解PIN尝试次数,用户输错PIN码会导致锁PIN [修改内容] 当剩余尝试次数=3(次数上限)时,才进行自动解PIN [自测] 手动设置错误auto-unlock PIN,反复插拔,只会自动解PIN一次 ------------------------------ [Background of the problem] After the user activates the auto unlock PIN, if the number of attempts exceeds 1, the device will attempt to unlock the PIN using the saved PIN code If the user repeatedly plugs and unplugges the card after changing it, it will result in only one attempt to unlock the PIN. If the user enters the wrong PIN code, it will cause the PIN to be locked [Modified Content] Automatic PIN release will only occur when the remaining attempts are 3 (maximum limit) [Self testing] Manually setting an incorrect auto unlock PIN, repeatedly plugging and unplugging, will only automatically unlock the PIN once Signed-off-by: xx <xx@xx.com> Change-Id: I04c53d9f9ae72173a6f4cb5e93b31df2a9dbb22d
最新发布
11-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值