如何使用最新提交进行交互式变基

时间:2019-07-12 23:50:32

标签: git

有没有一种方法可以使用交互式rebase编辑git中的最新提交? (就像更改提交消息或压缩提交一样,最好使用交互式变基)我要压缩最近的提交以及之前的提交。这是为了使代码更简洁,因为我在提交时不小心取消了文件的选择。

我目前正在尝试使用此功能: git rebase head~2

但是它没有我最近的提交。我认为这与HEAD有关,但我仍在学习git。

编辑:

它确实具有最新的提交,但是在提交的底部,这从github桌面到github命令行令人困惑。由于github桌面从上至下而不是从下至上列出它们。

2 个答案:

答案 0 :(得分:2)

不要打扰,要挤压一些提示提交,只需重置您的父母并提交:

git reset --soft @~2
git commit

或者您可以使用

保留上一次提交的消息以及当前提交的内容
git reset --soft @~
git commit --amend

如果您想混合匹配内容和消息,则可以选择从您喜欢的任何提交中获取提交消息,请参阅git commit文档。

答案 1 :(得分:0)

您需要添加notinline标志以进行交互式变基:

-i

这将显示如下所示的交互式屏幕:

git rebase -i HEAD~2

您的提交从最旧到最新列出在文件顶部。 pick 596d1e1 fix default image url pick 924430f fix about page link # Rebase 0f3ffa3..924430f onto 0f3ffa3 (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 # 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>] # . create a merge commit using the original merge commit's # . message (or the oneline, if no original merge commit was # . specified). Use -c <commit> to reword the commit message. # # 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 是我的最新承诺。

如果要将最新提交压缩到先前的提交中,请编辑最新提交行的开头以读取924430f而不是squash

pick