Git 如何使用最新提交进行交互式重设基础

Git 如何使用最新提交进行交互式重设基础,git,Git,有没有一种方法可以使用交互式重基编辑git中最近的提交?(如更改提交消息或挤压提交,最好使用交互式重定基址)我想挤压最近的提交和之前的提交。这是为了使代码库更干净,因为我在提交时意外地取消选择了一个文件 我目前正在尝试使用以下方法: git重基头~2 但它没有我最近的承诺。我想这与头部有关,但我仍在学习git 编辑: 它确实有最新的提交,但是在提交的底部,从github桌面到github命令行很混乱。由于github desktop从上到下而不是从下到上列出它们。您需要添加-i标志以执行交互式重

有没有一种方法可以使用交互式重基编辑git中最近的提交?(如更改提交消息或挤压提交,最好使用交互式重定基址)我想挤压最近的提交和之前的提交。这是为了使代码库更干净,因为我在提交时意外地取消选择了一个文件

我目前正在尝试使用以下方法:
git重基头~2

但它没有我最近的承诺。我想这与头部有关,但我仍在学习git

编辑:


它确实有最新的提交,但是在提交的底部,从github桌面到github命令行很混乱。由于github desktop从上到下而不是从下到上列出它们。

您需要添加
-i
标志以执行交互式重新基址:

git-rebase-i头~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

您需要添加
-i
标志来执行交互式重基:

git-rebase-i头~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

不必麻烦,对于压缩一些提示提交,只需重置您的父项并提交:

git reset --soft @~2
git commit
或者,您可以使用保留上一次提交的消息和当前提交的内容

git reset --soft @~
git commit --amend

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

不要麻烦,压缩一些提示提交只需重置您的父级并提交:

git reset --soft @~2
git commit
或者,您可以使用保留上一次提交的消息和当前提交的内容

git reset --soft @~
git commit --amend

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

重置父级实际上做了什么?就像在幕后一样?它没有幕后操作:它重置签出父项,
HEAD
,因此当您提交时,它将是新提交的父项。使用
git update ref HEAD
,您可以实现(我完全相信)相同的效果。好的,谢谢您的解释。这就更合理了重置父级实际上做了什么?就像在幕后一样?它没有幕后操作:它重置签出父项,
HEAD
,因此当您提交时,它将是新提交的父项。使用
git update ref HEAD
,您可以实现(我完全相信)相同的效果。好的,谢谢您的解释。这会让你感觉更有意义明白吗!我以前使用过重定基址,但按时间顺序从下到上的提交总是让我感到混乱,因此我会查看顶部提交,并意识到它不是我想要的提交,因此出于某种原因,我认为它没有抓住顶部提交。谢谢!我以前使用过重定基址,但按时间顺序从下到上的提交总是让我感到混乱,因此我会查看最上面的提交,并意识到这不是我想要的提交,因此出于某种原因,我认为它没有抓住最上面的提交。