在使用rebase玩了好几个小时后,回购仍然看起来与我需要的不同:
我想完成以下任务:
[其中一些在我开始搞乱rebase之前没问题:(]

我意识到这很多,但请包括实际的GIT命令.
我不介意自己阅读和尝试,但我对结果不符合我的预期感到困惑,我真的不想意外破坏任何提交.
首先,重新排序backup提交.
# 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
Run Code Online (Sandbox Code Playgroud)
将两个提交组合在一起,忽略顶部提交消息.
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
Run Code Online (Sandbox Code Playgroud)
合并twist分支backup,删除扭曲分支.
git checkout backup
git merge twist
git branch -d twist
Run Code Online (Sandbox Code Playgroud)
移动master.有多种奇特的方式,但这是最简单的.我假设你想要master指向编辑的backup位置,而不是它原来的位置.
git checkout master
git reset --hard backup
Run Code Online (Sandbox Code Playgroud)
remote/origins/master是远程跟踪分支,它告诉您master远程存储库中分支的分支指针在哪里origin,或者更确切地说是上次获取,推送或拉取的时间.
怎么不害怕
我想告诉你,无论你多么糟糕,承诺永远不会被破坏*你可以随时回到你开始的地方.
我制作了一个与你说明的形状相同的人造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
...
Run Code Online (Sandbox Code Playgroud)
糟糕,这很糟糕.让我们回到我们开始的地方.首先看看我们做了什么:
$git reflog
07e71d9 HEAD@{0}: HEAD~3: updating HEAD
9b41f46 HEAD@{1}: commit: Removed extraneous whitespace
...
Run Code Online (Sandbox Code Playgroud)
你可以看到,当我们重置时,所有的git都是让HEAD指向那个旧的提交.但实际上没有任何提交丢失 - 他们只是成为孤儿,而不是任何分支的一部分.让我们再次将它们作为"备份"分支的一部分:
$ 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
...
Run Code Online (Sandbox Code Playgroud)
Git意味着永远不必说你很抱歉.
*松散的物体最终会被垃圾收集,但直到它们至少有两周的时间才被收集.
如何做你想做的事.
首先让我们在master中合并两个提交:
$ git checkout master
$ git rebase -i HEAD~2
Run Code Online (Sandbox Code Playgroud)
Git将启动你的编辑器.改变这个:
pick 6389f4e Moved "loaded" function out of "require".
pick 41fb646 comma
Run Code Online (Sandbox Code Playgroud)
对此:
pick 6389f4e Moved "loaded" function out of "require".
s 41fb646 comma
Run Code Online (Sandbox Code Playgroud)
并保存.Git将再次启动您的编辑器.改变这个:
# 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
Run Code Online (Sandbox Code Playgroud)
对此:
Moved "loaded" function out of "require".
Run Code Online (Sandbox Code Playgroud)
并保存.
现在让我们重新排序'backup'中的提交:
$ git checkout backup
$ git rebase -i remotes/origin/master
Run Code Online (Sandbox Code Playgroud)
弹出编辑器时,请更改:
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 <-----
Run Code Online (Sandbox Code Playgroud)
对此:
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
Run Code Online (Sandbox Code Playgroud)
并保存.
现在,选择合并后的"Moved loaded"提交,从master到当前分支("backup")
$git cherry-pick master
Run Code Online (Sandbox Code Playgroud)
使master指向与"backup"相同的提交
$git checkout master
$git reset --hard backup
Run Code Online (Sandbox Code Playgroud)
摆脱扭曲的分支
$git branch -D twist
Run Code Online (Sandbox Code Playgroud)