Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过它的SHA1散列重写Git提交?_Git_Git Rebase_Git Commit - Fatal编程技术网

如何通过它的SHA1散列重写Git提交?

如何通过它的SHA1散列重写Git提交?,git,git-rebase,git-commit,Git,Git Rebase,Git Commit,我创建了一个提交,其中包含一条要修改的提交消息。我还没有发布提交,所以我可以安全地重写历史。我可以使用git日志查找,因此我知道它的sha1哈希。如何快速编辑提交?您可以签出有问题的提交,修改其消息并手动重新设置分支的基础: $ git checkout FIRST_COMMIT_SHA $ git commit --amend $ git rebase HEAD THE_BRANCH_YOU_CAME_FROM 此git别名将自动执行此过程: reword = "!f() { branch=

我创建了一个提交,其中包含一条要修改的提交消息。我还没有发布提交,所以我可以安全地重写历史。我可以使用git日志查找,因此我知道它的sha1哈希。如何快速编辑提交?

您可以签出有问题的提交,修改其消息并手动重新设置分支的基础:

$ git checkout FIRST_COMMIT_SHA
$ git commit --amend
$ git rebase HEAD THE_BRANCH_YOU_CAME_FROM
此git别名将自动执行此过程:

reword = "!f() { branch=`git symbolic-ref --short HEAD`; git checkout $1; git commit --amend; git checkout $branch; }; f"
要将其添加到您的
~/.gitconfig

$ git config alias.reword "!f() { branch=`git symbolic-ref --short HEAD`; git checkout $1; git commit --amend; git checkout $branch; }; f"
然后像这样使用:

$ git reword SHA1_OF_THE_COMMIT_TO_BE_REWORDED
学分:


或者,可以通过使用rebase命令并提供
--root
标志来修改初始提交消息。此外,您需要指定交互模式,并在第一次提交时使用
edit
,如下所示:

git rebase -i -root

// Specify 'edit' for the first commit.

// Amend first commit message here.
git commit --amend
有关
--root
标志的更多详细信息,请参阅


此外,如果您要修改的消息的提交位于您正在处理的分支中,那么您也可以通过交互式重新基址轻松解决这个问题。只需找到相应的SHA-1并指定
edit
,以允许修改其提交消息。

仅当要重写的提交是根提交时,此操作才有效。因为OP(me)没有具体说明可能是这样,也可能不是。