如何使用 Git rebase 重新排序/合并提交?

发布于 2024-08-17 18:27:50 字数 508 浏览 7 评论 0原文

使用 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?

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对你再特殊 2024-08-24 18:27:50

首先,重新排序备份提交。

# 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

将两个提交合并在一起,忽略顶部提交消息。

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 是远程跟踪分支,它告诉您远程存储库 originmaster 分支的分支指针在哪里,或者而是你最后一次取、推或拉的时间。

First, re-ordering the backup commits.

# 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

Combine the two commits on twist, ignoring the top commit message.

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

Merge the twist branch into backup, remove the twist branch.

git checkout backup
git merge twist
git branch -d twist

Move master. There are multiple fancy ways, but this is simplest. I'm assuming that you want master to point to the edited backup position and not where it originally was.

git checkout master
git reset --hard backup

remote/origins/master is the remote tracking branch and tells you where the branch pointer for the master branch in the remote repository origin is, or rather was when you last fetched, pushed or pulled.

ぶ宁プ宁ぶ 2024-08-24 18:27:50

如何不害怕

我想向您展示,无论您搞砸得有多严重,提交都不会被破坏*,并且您始终可以回到开始的地方。

我制作了一个与您所示形状相同的人造 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 所做的就是使 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
...

Git 意味着永远不必说抱歉。

*松散的对象最终会被垃圾收集,但要等到它们至少两周后才会被收集。

如何做你想做的事。

首先让我们将两个提交合并到 master 中:

$ git checkout master
$ git rebase -i HEAD~2

Git 将启动你的编辑器。将此:更改

pick 6389f4e Moved "loaded" function out of "require".
pick 41fb646 comma

为:

pick 6389f4e Moved "loaded" function out of "require".
s 41fb646 comma

并保存。 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

为:

Moved "loaded" function out of "require".

并保存。

现在让我们重新排序“备份”中的提交:

$ git checkout backup
$ git rebase -i remotes/origin/master

当弹出编辑器时,将 this: 更改

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             <-----

为 this:

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

并保存。

现在,挑选合并的“已移动加载”提交从 master 移至当前分支(“备份”)

$git cherry-pick master

使 master 指向与“备份”相同的提交

$git checkout master
$git reset --hard backup

摆脱扭曲分支

$git branch -D twist

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:

$ 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
...

Oops, that was bad. Let's get back to where we started. First see what we did:

$git reflog
07e71d9 HEAD@{0}: HEAD~3: updating HEAD
9b41f46 HEAD@{1}: commit: Removed extraneous whitespace
...

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 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 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 checkout master
$ git rebase -i HEAD~2

Git will launch your editor. Change this:

pick 6389f4e Moved "loaded" function out of "require".
pick 41fb646 comma

to this:

pick 6389f4e Moved "loaded" function out of "require".
s 41fb646 comma

And save. Git will again launch your editor. Change this:

# 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

to this:

Moved "loaded" function out of "require".

and save.

Now let's reorder the commits in 'backup':

$ git checkout backup
$ git rebase -i remotes/origin/master

When your editor pops up, change this:

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             <-----

to this:

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

and save.

Now cherry-pick the merged "Moved loaded" commit off of master and onto the current branch ("backup")

$git cherry-pick master

Make master point to the same commit as "backup"

$git checkout master
$git reset --hard backup

Get rid of the twist branch

$git branch -D twist
So尛奶瓶 2024-08-24 18:27:50

您试图在这里实现很多目标,从提供所需内容的图表中有点不清楚,但以下指示可能会有所帮助:

  • 我会首先合并您的提交,而不是尝试移动您的分支已经在该分支上完成了,然后将最终提交应用到主分支,因为这就是您最终的目标。此时您可能必须解决一些冲突,但无论如何您都必须这样做。有关如何实现此目标的信息,请参阅接下来的两个提示。

  • 要合并两个提交,您可以执行交互式变基

  • 要从一个分支选择单个提交并将其应用到另一个分支,同时在目标分支上使用 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 using git branch -D twist when done.

Hope these tips get you on the road to your goal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文