如果你在上次提交中发现了一个错误,你可以使用以下命令重新提交上次的提交:
git commit --amend
但如果你已经提交了另一个commit,那么你如何重新提交该commit之前的commit?
注意: 假设你已经确认在上游没有其他开发人员使用你的提交,你才应该使用--amend
命令。
如果你在上次提交中发现了一个错误,你可以使用以下命令重新提交上次的提交:
git commit --amend
但如果你已经提交了另一个commit,那么你如何重新提交该commit之前的commit?
注意: 假设你已经确认在上游没有其他开发人员使用你的提交,你才应该使用--amend
命令。
使用以下命令提交你的修复:
git commit -a -m "fix commit(此提交将向上移动一行)"
(提交信息不重要,完成后它将过时)
现在是魔法时间:
将 HEAD 变基到倒数第二个提交,但在此之前编辑提交信息,使其交换最后两行:
git rebase -i HEAD~3
编辑器将显示最后 3 次提交,如下所示:
pick 2a06f16 this was the last commit pick 0dbc5ce the last-but-one commit pick 2e30418 fix commit (this one will be shifted up one line)
# Rebase 011f3d0..2e30418 onto 011f3d0 # …
交换最后两行,使提交信息看起来像这样:
pick 2a06f16 this was the last commit fixup 2e30418 fix commit (this one will be shifted up one line) pick 0dbc5ce the last-but-one commit
# Rebase 011f3d0..2e30418 onto 011f3d0 # …
使用以下命令检查日志:
git log HEAD~3
使用+
将更正的提交行强制推送到已存在的分支:
git push origin +branchname
git commit -a --fixup=HEAD^
(或要修复的任何提交),然后运行git rebase -i HEAD~3
,您无需更改任何内容,只需关闭编辑器即可,git
将自动处理修复操作。 - aragaergit log -3
吗?我不明白如何使用你提供的命令来检查我的日志。 - iron9git rebase -i HEAD~2
对于VIM编辑器:
一个编辑器将会打开,显示以下细节
pick 4f4f96f Added git ignore
pick d01e18c Added sample Blog
# Rebase 60e1cd3..d01e18c onto 60e1cd3 (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
pick
更改为r
或reword
,类似下面这样pick 4f4f96f Added git ignore
r d01e18c Added sample Blog
esc + : + wq
另一个窗口将打开,更改提交信息esc + : + wq
对于记事本或任何其他编辑器:git push -f
git commit -a --fixup=HEAD^ #(or whatever commit to be fixed)
git rebase -i HEAD~3
你不需要进行任何更改,只需关闭编辑器,git
就会自动处理 fixup。
注意:这在我的 Ubuntu 系统上无法使用!
temp
的分支master
重置到要编辑的提交之前的提交temp
分支的所有提交并将其应用到 master
,如果需要可以编辑提交信息使用 +
强制推送更正后的提交行以强制将其推送到已存在的分支上
git push origin +master