如何使用 Git rebase 重新排序/合并提交?
使用 rebase 几个小时后,该存储库看起来仍然与我需要的不同:
我想完成以下任务:
[在我开始搞变基之前,其中一些是可以的:(]
- 将顶部提交(“删除了无关的...”)移动到分支之前(“修复#226”的正上方)。
- 合并两个提交在“twist/main”分支中,“逗号”和“已移动加载...”应该是相同的提交,并且我根本不需要合并
- 新组合的“移动 ”的提交消息。 load”提交到“backup”分支,并去掉“twist”。
- 将“master”移动到现在显示“backup”的位置。
- “remote/origins/master”标签是什么意思?
我意识到这要求很高,但请包含实际的 GIT 命令。
我不介意自己阅读和尝试,但我对结果与我的预期不符感到有点困惑,我真的不想意外破坏任何提交。
After quite a few hours playing with rebase, the repo still looks different from what I need:
I would like to accomplish the following tasks:
[some of which were OK before I started messing with rebase :( ]
- Move The top commit ("Removed extraneous...") to before the branch off (Right above "fix for #226").
- Combine the two commits that are in the 'twist/main' branch. "comma" and "Moved loaded..." should be the same commit, and I don't need the commit message of "comma" at all.
- Merge the newly combined "Move loaded" commit into the 'backup' branch, and get rid of 'twist'.
- Move 'master' to where it now says 'backup'.
- What does that "remote/origins/master" tag mean?
I realize that this is asking a lot, but please include actual GIT commands.
I don't mind reading and trying on my own, but am a bit confused by the results not matching what I would've expected, and I really don't want to accidentally destroy any commits.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,重新排序
备份
提交。将两个提交合并在一起,忽略顶部提交消息。
将
twist
分支合并到backup
,删除twist分支。移动
master
。有多种奇特的方法,但这是最简单的。我假设您希望master
指向编辑后的 backup
位置,而不是原来的位置。remote/origins/master
是远程跟踪分支,它告诉您远程存储库origin
中master
分支的分支指针在哪里,或者而是你最后一次取、推或拉的时间。First, re-ordering the
backup
commits.Combine the two commits on twist, ignoring the top commit message.
Merge the
twist
branch intobackup
, remove the twist branch.Move
master
. There are multiple fancy ways, but this is simplest. I'm assuming that you wantmaster
to point to the editedbackup
position and not where it originally was.remote/origins/master
is the remote tracking branch and tells you where the branch pointer for themaster
branch in the remote repositoryorigin
is, or rather was when you last fetched, pushed or pulled.如何不害怕
我想向您展示,无论您搞砸得有多严重,提交都不会被破坏*,并且您始终可以回到开始的地方。
我制作了一个与您所示形状相同的人造 git 树:
我现在要清除“备份”分支中的最后三个提交:
哎呀,这很糟糕。让我们回到开始的地方。首先看看我们做了什么:
你可以看到,当我们重置时,git 所做的就是使 HEAD 指向旧的提交。但实际上并没有丢失任何提交——它们只是成为孤儿,而不是任何分支的一部分。让我们再次将它们作为“备份”分支的一部分:
Git 意味着永远不必说抱歉。
*松散的对象最终会被垃圾收集,但要等到它们至少两周后才会被收集。
如何做你想做的事。
首先让我们将两个提交合并到 master 中:
Git 将启动你的编辑器。将此:更改
为:
并保存。 Git 将再次启动您的编辑器。将此:更改
为:
并保存。
现在让我们重新排序“备份”中的提交:
当弹出编辑器时,将 this: 更改
为 this:
并保存。
现在,挑选合并的“已移动加载”提交从 master 移至当前分支(“备份”)
使 master 指向与“备份”相同的提交
摆脱扭曲分支
How to Not Be Afraid
I'd like to show you that no matter how bad you mess up, commits never get destroyed* and you can always get back to where you started.
I've made a faux git tree that has the same shape as you illustrated:
I am now going to wipe out the last three commits from the 'backup' branch:
Oops, that was bad. Let's get back to where we started. First see what we did:
You can see that when we reset, all git did was to make HEAD point to that old commit. But no commits were actually lost--they just became orphans, not part of any branch. Let's make them part of the "backup" branch again:
Git means never having to say you're sorry.
*Loose objects do eventually get garbage collected, but not until they are at least two weeks old.
How to Do What You Want.
First let's combine the two commits in master:
Git will launch your editor. Change this:
to this:
And save. Git will again launch your editor. Change this:
to this:
and save.
Now let's reorder the commits in 'backup':
When your editor pops up, change this:
to this:
and save.
Now cherry-pick the merged "Moved loaded" commit off of master and onto the current branch ("backup")
Make master point to the same commit as "backup"
Get rid of the twist branch
您试图在这里实现很多目标,从提供所需内容的图表中有点不清楚,但以下指示可能会有所帮助:
我会首先合并您的提交,而不是尝试移动您的分支已经在该分支上完成了,然后将最终提交应用到主分支,因为这就是您最终的目标。此时您可能必须解决一些冲突,但无论如何您都必须这样做。有关如何实现此目标的信息,请参阅接下来的两个提示。
要合并两个提交,您可以执行交互式变基。
要从一个分支选择单个提交并将其应用到另一个分支,同时在目标分支上使用
gitcherry-pick
。完成后,您可以使用 gitbranch -D twin 删除旧分支。希望这些提示可以帮助您走上实现目标的道路。
There are a number of things you are trying to achieve here and it's a tad unclear from the diagrams provided exactly what is needed but the following pointers may help:
Rather than try to move your branch, I would first combine the commits you've done on that branch, then apply the final commit to the master branch since thats eventually what you are aiming at. You may have to resolve some conflicts at this point but you would have to do that anyway. See the next two tips for info on how to achieve this.
To combine two commits you can do aninteractive rebase.
To pick a single commit from one branch and apply it to another, whilst on the target branch use
git cherry-pick
. You can delete the old branch usinggit branch -D twist
when done.Hope these tips get you on the road to your goal.