8000 git commit --amend 踩坑 · Issue #79 · hawtim/hawtim.github.io · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
git commit --amend 踩坑 #79
Open
@hawtim

Description

@hawtim

简介 —amend 参数

git commit --amend 是一个用于修改上一次 Git 提交的命令。当您想要编辑上一个提交消息或添加新更改到最后一次提交时,这个命令将非常有用。--amend 选项允许您修改最后一次提交而不创建新的提交。

踩坑记录

对已经存在的文件 afile.js,修改错别字,比如

// a File
const kyes = ['key1', 'key2']; 
// 修改为
const keys = ['key1', 'key2']; 

dev 分支

git commit -am "fix: typo"

合入 feature 分支,发现有问题,回到 dev 分支,把 afile.js 的改动还原回来

git commit -a --amend

git push -f

此时往 feature 合,afile.js 不会有变动。即无法通过 amend 的方式把 feature 分支上的改动撤回来。

现象分析

整体流程如下图

image

即当把 afile.js 的改动还原回来并 amend 替换原本位置的 commit 之后,此时 afile.js 相对 base 这个 commit 是没有变动的。

再往 feature 分支合,即 feature 和 dev 分支匹配差异,就会认为 afile 文件没有修改。

解决办法

  1. 在 dev 分支再把还原的改动还原回来(具体可以用 amend 或者 commit,但用 amend 可以少一个 commit),然后通过 git commit -am 的方式创建一个新的 commit(推荐)
  2. 在 feature 分支拉个临时分支出来,还原改动(不推荐)

总结

如果改动后 amend 会导致某个文件回到跟上个版本相比完全没有变更的情况,则不能用 amend(对应上面的情况)

其他不建议使用 amend 的情况

如果要 amend 的改动已经推送到了远端,则不用 amend,用 git commit -am 更加稳健,对应有多人在同一个分支协作的场景。

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0