我在承诺信中写错了。

我怎么能改變訊息呢?承諾尚未推動。


当前回答

要改變以前的承諾,做你想要的變化,並進行這些變化,然後執行

git commit --amend

这将打开一个文件在你的文本编辑器代表你的新的承诺消息. 它开始与文本从你的旧承诺消息。 改变承诺消息,如你想要,然后保存文件,然后停止你的编辑器完成。

要更改以前的承诺,并保持相同的日志消息,运行

git commit --amend -C HEAD

修复以前的承诺,通过完全删除它,运行

git reset --hard HEAD^

如果你想编辑多个承诺消息,运行

git rebase -i HEAD~commit_count

此命令启动您的编辑器. 标记第一个命令(您想要更改的命令)为“编辑”而不是“选择”,然后保存和输出您的编辑器。

git commit --amend
git rebase --continue

注意: 您也可以从 git commit 打开的编辑器“做您想要的更改” - 更改

其他回答

git rebase -i HEAD~5

这个命令会带你到Vim,你可以“编辑”你的承诺历史,你会看到你的最后五个承诺在顶部如下:

pick <commit hash> commit message

然后Vim将通过你想要重新编写的每一个承诺消息,在这里你实际上可以改变承诺消息,你将通过进入插入模式,改变承诺消息,进入命令模式,并保存和停止。

然后,如果你已经推了你的错误的命令,你需要 git push - 强迫过写它们. 记住, git push - 强迫是相当危险的事情,所以确保没有人从服务器,因为你推了你的错误的命令!

使用

git commit --amend

要详细理解,一个很好的帖子是 4. 重新写 Git 历史. 它也谈论什么时候不使用 git commit --amend。

我喜欢使用以下内容:

git status git add --all git commit -am “消息走到这里关于变化” git pull <origin master> git push <origin master>

我添加了 Aliases reci 和 recm for recommit (amend) 它. 现在我可以用 git recm 或 git recm -m:

$ vim ~/.gitconfig

[alias]

    ......
    cm = commit
    reci = commit --amend
    recm = commit --amend
    ......

更改最新承诺消息

git commit --amend

git commit --amend -m "New commit message"

...任何人,这可能会使多线承诺消息或小纠正更有吸引力进入。

确保您在做此之前没有任何工作复制变更,或者它们也会被承诺(未进行的变更不会被承诺)。

git push <remote> <branch> --force
# Or
git push <remote> <branch> -f


// n is the number of commits up to the last commit you want to be able to edit
git rebase -i HEAD~n

一旦你错过了你的承诺,选择e/r来编辑消息:

如果你使用 git rebase -i HEAD~n 可以有更多的 n 命令. Git 将“收集”最后 n 命令中的所有命令,如果在这个范围之间有某个合并,你会看到所有的命令,所以结果将是 n + 。

好提示:


git-commit(1) 手动 页 git-rebase(1) 手动 页 git-push(1) 手动 页