如何使用Git rebase重新排序/组合提交?
在玩了几个小时的rebase之后,回购看起来仍然与我需要的不同: 我想完成以下任务:如何使用Git rebase重新排序/组合提交?,git,rebase,Git,Rebase,在玩了几个小时的rebase之后,回购看起来仍然与我需要的不同: 我想完成以下任务: [在我开始处理rebase之前,其中一些还行:(] 将顶部提交(“删除无关…”)移动到分支之前(在“修复#226”的正上方) 合并“twist/main”分支中的两个提交。“comma”和“Moved loaded…”应该是同一个提交,我根本不需要“comma”的提交消息 将新组合的“Move-loaded”提交合并到“backup”分支中,并去掉“twist” 将“主”移动到现在显示“备份”的位置 “rem
[在我开始处理rebase之前,其中一些还行:(]
- 将顶部提交(“删除无关…”)移动到分支之前(在“修复#226”的正上方)
- 合并“twist/main”分支中的两个提交。“comma”和“Moved loaded…”应该是同一个提交,我根本不需要“comma”的提交消息
- 将新组合的“Move-loaded”提交合并到“backup”分支中,并去掉“twist”
- 将“主”移动到现在显示“备份”的位置
- “remote/Originates/master”标签是什么意思
我不介意自己阅读和尝试,但对于结果与我预期的不匹配感到有点困惑,我真的不想意外地破坏任何提交。首先,重新排序
备份
提交
# Safety, should be a no-op if your gitk snapshot is accurate
git checkout backup
# Temporary branch
git branch backup-save backup
# Move top commit onto 'Fix for #226:
git rebase --onto origin/master HEAD^
# Go back to saved branch's parent (i.e. without the moved commit)
git reset --hard backup-save^
# Rebase onto the moved commit (HEAD@{1} is where HEAD was 1 step
# ago i.e. before the reset.)
git rebase HEAD@{1}
# Don't need the saved branch any more (although you might want
# to keep it for a bit just in case). This deletes it:
git branch -D backup-save
合并twist上的两个提交,忽略顶部的提交消息
git checkout twist
git reset --soft HEAD^
# Just re-save the commit message, alternatively to skip the
# editor step do this: git commit --amend -C HEAD
git commit --amend
将twist
分支合并到backup
,删除twist分支
git checkout backup
git merge twist
git branch -d twist
移动master
。有多种奇特的方法,但这是最简单的。我假设您希望master
指向已编辑的backup
位置,而不是原始位置
git checkout master
git reset --hard backup
remote/origins/master
是远程跟踪分支,它告诉您远程存储库origin
中master
分支的分支指针在哪里,或者更确切地说,是您上次提取、推送或拉取时的分支指针。如何不害怕
我想告诉你,不管你搞得多么糟糕,承诺永远不会被摧毁,你总能回到起点
我制作了一棵仿git树,它的形状与您所示的形状相同:
现在,我将清除“备份”分支的最后三个提交:
$ git log --oneline
9b41f46 Removed extraneous whitespace
981e2e8 Merged the "loadscripts" function
005bc62 Pick up a few very fringe cases
07e71d9 Merged "getDepsForScript" function
...
$ git reset --hard HEAD~3
HEAD is now at 07e71d9 Merged "getDepsForScript" function
$ git log --oneline
07e71d9 Merged "getDepsForScript" function
...
哎呀,那太糟糕了。让我们回到我们开始的地方。首先看看我们做了什么:
$git reflog
07e71d9 HEAD@{0}: HEAD~3: updating HEAD
9b41f46 HEAD@{1}: commit: Removed extraneous whitespace
...
您可以看到,当我们重置时,git所做的只是将头指向旧的提交。但实际上没有丢失任何提交--它们只是成为孤立的,而不是任何分支的一部分。让我们再次将它们作为“备份”分支的一部分:
$ git reset --hard 9b41f46
HEAD is now at 9b41f46 Removed extraneous whitespace
$ git log --oneline
9b41f46 Removed extraneous whitespace
981e2e8 Merged the "loadscripts" function
005bc62 Pick up a few very fringe cases
07e71d9 Merged "getDepsForScript" function
...
Git意味着永远不必说对不起
*松散的物体最终会被垃圾收集,但至少要到它们两周后才会被收集
如何做你想做的事。
首先,让我们结合master中的两个提交:
$ git checkout master
$ git rebase -i HEAD~2
Git将启动您的编辑器。更改此选项:
pick 6389f4e Moved "loaded" function out of "require".
pick 41fb646 comma
# This is a combination of two commits.
# The first commit's message is:
Moved "loaded" function out of "require".
# This is the 2nd commit message:
comma
pick ec7f71c moved "loaded" function out of "require"
pick 4a76897 Replaced maploaded event
pick 07e71d9 Merged "getDepsForScript" function
pick 005bc62 Pick up a few very fringe cases
pick 981e2e8 Merged the "loadscripts" function
pick 9b41f46 Removed extraneous whitespace <-----
为此:
pick 6389f4e Moved "loaded" function out of "require".
s 41fb646 comma
Moved "loaded" function out of "require".
pick 9b41f46 Removed extraneous whitespace <-----
pick ec7f71c moved "loaded" function out of "require"
pick 4a76897 Replaced maploaded event
pick 07e71d9 Merged "getDepsForScript" function
pick 005bc62 Pick up a few very fringe cases
pick 981e2e8 Merged the "loadscripts" function
然后保存。Git将再次启动编辑器。更改此选项:
pick 6389f4e Moved "loaded" function out of "require".
pick 41fb646 comma
# This is a combination of two commits.
# The first commit's message is:
Moved "loaded" function out of "require".
# This is the 2nd commit message:
comma
pick ec7f71c moved "loaded" function out of "require"
pick 4a76897 Replaced maploaded event
pick 07e71d9 Merged "getDepsForScript" function
pick 005bc62 Pick up a few very fringe cases
pick 981e2e8 Merged the "loadscripts" function
pick 9b41f46 Removed extraneous whitespace <-----
为此:
pick 6389f4e Moved "loaded" function out of "require".
s 41fb646 comma
Moved "loaded" function out of "require".
pick 9b41f46 Removed extraneous whitespace <-----
pick ec7f71c moved "loaded" function out of "require"
pick 4a76897 Replaced maploaded event
pick 07e71d9 Merged "getDepsForScript" function
pick 005bc62 Pick up a few very fringe cases
pick 981e2e8 Merged the "loadscripts" function
和保存
现在,让我们对“备份”中的提交重新排序:
$ git checkout backup
$ git rebase -i remotes/origin/master
当编辑器弹出时,更改以下内容:
pick 6389f4e Moved "loaded" function out of "require".
pick 41fb646 comma
# This is a combination of two commits.
# The first commit's message is:
Moved "loaded" function out of "require".
# This is the 2nd commit message:
comma
pick ec7f71c moved "loaded" function out of "require"
pick 4a76897 Replaced maploaded event
pick 07e71d9 Merged "getDepsForScript" function
pick 005bc62 Pick up a few very fringe cases
pick 981e2e8 Merged the "loadscripts" function
pick 9b41f46 Removed extraneous whitespace <-----
使主节点指向与“备份”相同的提交
除掉那个扭曲的树枝
$git branch -D twist
在这里,您正试图实现许多目标,但从提供的图表中还不清楚到底需要什么,但以下几点可能会有所帮助:
- 与其尝试移动您的分支,我将首先合并您在该分支上所做的提交,然后将最终提交应用于主分支,因为这是您最终的目标。此时您可能需要解决一些冲突,但无论如何都必须这样做。有关如何实现这一点的信息,请参阅下面两个技巧。
- 要组合两个提交,可以执行以下操作
- 要在目标分支上从一个分支选择一个提交并将其应用于另一个分支,请使用。完成后,您可以使用
删除旧分支git branch-D twist
希望这些提示能帮助您实现目标。这个问题的标题“git rebase help”相当糟糕是一系列你想要执行的不同操作。试着用一个问题的形式重新表述。你试图实现的第一个目标,以一个问题的形式,可能会让其他目标变得显而易见。我不认为这是一个不好的标题,这肯定是一个需要重新调整提交的问题。你确定你想要合并
twist
和backup
分支吗?我这样问是因为它们都有具有相同外观描述的提交。哇,你很好。1.“git checkout twist”步骤没有成功地将twist分支中的两个提交合并起来。是支持它,还是我遗漏了什么?2.在“git merge twist”中,它是c抱怨:冲突(内容):Source/Depender.js自动合并中的合并冲突失败;修复冲突,然后提交结果。
如何修复冲突?等等,我知道如何使用git add修复冲突。将在一分钟后报告,要么进一步混乱,要么都很高兴。虽然我没有完全完成这两个步骤,但它让我接近目标ve.你写的每一件事都很清楚,切中要害。非常感谢。山姆,我的荣幸。这不仅仅是一个表达方式——当我在我熟悉的问题边缘挑一个问题时,我有很多乐趣,学到了很多东西。+1.另一个恢复旧承诺的有用命令:fsck()