如何在 git 中重新排序最后两次提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33388210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:28:58  来源:igfitidea点击:

How to reorder last two commits in git?

git

提问by noisy

I want to reorder last two commits in git:

我想在 git 中重新排序最后两个提交:

right now I have:

现在我有:

$ git lg --oneline -4
1e0ecba (HEAD, my-branch) Fix for T255
82d45dc django_extensions
af3953b improvements according to CR
dae63ff Fullscreen support

I want to have:

我希望有:

$ git lg --oneline -4
82d45dc (HEAD, my-branch) django_extensions
1e0ecba Fix for T255
af3953b improvements according to CR
dae63ff Fullscreen support

回答by Jim Aho

I truly recommend to read this answer about how to reorder commits, you'll feel like a git rockstar afterwards, I promise.

我真的建议阅读这个关于如何重新排序提交的答案,我保证你之后会觉得自己像个 git 摇滚明星。



Apart from that, here's how you do it with a simple rebase (assuming you're standing on the branch you want to rebase):

除此之外,这里是您如何使用简单的 rebase(假设您站在要 rebase 的分支上)执行此操作的方法:

git rebase -i HEAD~2

git rebase -i HEAD~2

Next, changethe order of the commits in the prompt.

接下来,更改提示中提交的顺序。

pick f4648aee My first commit
pick 00adf09a My second commit

to

pick 00adf09a My second commit
pick f4648aee My first commit

Unbelievable that it can be that simple, if you ask me.

难以置信,如果你问我,它可以这么简单。

回答by noisy

In general you have to use git rebase --interactive- here is detail answer how to reorder any number of commits:

一般来说,您必须使用git rebase --interactive- 这是如何重新排序任意数量的提交的详细答案:

But if you want to reorder last two commitsyou can use this git alias:

但是如果你想对最后两次提交重新排序,你可以使用这个 git 别名:

Add to ~/.gitconfig:

添加到~/.gitconfig

[alias]
    reorder = "!GIT_SEQUENCE_EDITOR=\"sed -i -n 'h;1n;2p;g;p'\" git rebase -i HEAD~2"

and then:

进而:

$ git reorder
Rebasing(2/2)
Successfully rebased and updated refs/heads/my-branch.

回答by JBallin

Assuming no conflicts:

假设没有冲突:

  • git rebase -i HEAD~4
  • Re-order commits + save
  • git rebase -i HEAD~4
  • 重新排序提交 + 保存

回答by kabby

A little late to the party, but I have written the following answer from the standpoint of understanding how few git commands work. The other answers will get your work done in a shorter way with the help of interactive rebase commands. I had encountered this similar situation and this is how I solved my problem -

聚会有点晚了,但我从理解 git 命令的工作原理的角度写了以下答案。其他答案将在交互式 rebase 命令的帮助下以更短的方式完成您的工作。我遇到过类似的情况,这就是我解决问题的方法-

Suppose this is how your last 4 commits look like.

假设这是您最近 4 次提交的样子。

* ca6807e - (HEAD -> lolo) third (4 seconds ago) <Kaustubh Butte>
| 
* 61a069c - (master) fourth (18 hours ago) <Kaustubh Butte>
| 
* f3b0255 - (lol) second (2 days ago) <Kaustubh Butte>
| 
* c2f5e4f - first (2 days ago) <Kaustubh Butte>

Now according to your question you want to make the commit history as follows (Ignore the rest of the commit ids)-

现在根据您的问题,您要按如下方式制作提交历史记录(忽略其余的提交 ID)-

* 0fe6482 - (HEAD -> master) fourth (5 seconds ago) <Kaustubh Butte>
| 
* 2c3ba40 - (lol) third (4 minutes ago) <Kaustubh Butte>
| 
* f3b0255 - (lolo) second (2 days ago) <Kaustubh Butte>
| 
* c2f5e4f - first (2 days ago) <Kaustubh Butte>

To do that follow these steps -

要做到这一点,请按照以下步骤操作 -

  1. Go to the 3rd last commit

    git reset HEAD~2

    Now your HEAD will point at the 'second' commit

  2. At this point, we will switch our branch from master to lol (look at the first tree diagram)-

    git checkout lol

    Now if you do git statusyou may find some files are in the untracked section that is because you checked back to a previous commit in the history which which did not have those files in tracking mode but your remote repository has them. So you have to just ignore those files for now. But git will not let you move forward with our approach if we keep those files untracked. So we add all of them by doing git add *followed by git stashwhich will save those files in a temporary stack (ignoring them temporarily and then allowing us to pop those files from this stack later when we need).

  3. Now we need to move our 'fourth' commit on the current branch 'lol'. To do that we need to use git cherry-pickcommand. Copy the commit id of the 'fourth' commit which is ca6807ein my case. Now type git cherry-pick ca6807e. Now the tree structure will look something like this -

    * 2c3ba40 - (HEAD -> lol) third (3 seconds ago) <Kaustubh Butte>
    |   
    | * 61a069c - (master) fourth (18 hours ago) <Kaustubh Butte>
    |/  
    | 
    * f3b0255 - (lolo) second (2 days ago) <Kaustubh Butte>
    | 
    * c2f5e4f - first (2 days ago) <Kaustubh Butte>
    
  1. 转到第三次最后提交

    git reset HEAD~2

    现在您的 HEAD 将指向“第二次”提交

  2. 此时,我们将我们的分支从 master 切换到 lol(看第一个树状图)-

    git checkout lol

    现在,如果您这样做,git status您可能会发现一些文件位于未跟踪部分,这是因为您检查回历史记录中的先前提交,该提交没有处于跟踪模式的这些文件,但您的远程存储库中有这些文件。所以你现在必须忽略这些文件。但是如果我们不跟踪这些文件,git 将不会让您继续我们的方法。因此,我们通过执行以下操作添加所有文件,git add *然后git stash将这些文件保存在临时堆栈中(暂时忽略它们,然后允许我们稍后在需要时从该堆栈中弹出这些文件)。

  3. 现在我们需要在当前分支“lol”上移动我们的“第四次”提交。为此,我们需要使用git cherry-pick命令。复制ca6807e在我的情况下的“第四次”提交的提交 ID 。现在输入git cherry-pick ca6807e. 现在树结构看起来像这样 -

    * 2c3ba40 - (HEAD -> lol) third (3 seconds ago) <Kaustubh Butte>
    |   
    | * 61a069c - (master) fourth (18 hours ago) <Kaustubh Butte>
    |/  
    | 
    * f3b0255 - (lolo) second (2 days ago) <Kaustubh Butte>
    | 
    * c2f5e4f - first (2 days ago) <Kaustubh Butte>
    

Now you just need to do switch back to master branch and rebase lol branch on it.

现在你只需要切换回 master 分支并在其上 rebase lol 分支。

git checkout master
git rebase lol

And now you have successfully interchanged the last two commits. Here is how the tree structure looks like now -

现在你已经成功地交换了最后两个提交。这是树结构现在的样子 -

* 0fe6482 - (HEAD -> master) fourth (5 seconds ago) <Kaustubh Butte>
| 
* 2c3ba40 - (lol) third (4 minutes ago) <Kaustubh Butte>
| 
* f3b0255 - (lolo) second (2 days ago) <Kaustubh Butte>
| 
* c2f5e4f - first (2 days ago) <Kaustubh Butte>