git 我应该如何将更改从一个提交移动到另一个提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15213814/
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 08:15:54  来源:igfitidea点击:

How should I move changes from one commit to another?

git

提问by Rose Perrone

I have two commits on the same branch, one right after another. I added changes to file A to the first commit, and then I made some changes to other files and then made another commit. Now I want the changes to file A to be on the second commit rather than the first. What's the most elegant way?

我在同一个分支上有两个提交,一个接一个。我将文件 A 的更改添加到第一次提交,然后对其他文件进行了一些更改,然后再次提交。现在我希望对文件 A 的更改在第二次提交而不是第一次提交。最优雅的方式是什么?

采纳答案by Ryan Stewart

If they're small commits, and commits should always be small in git, the simplest way is to git reset HEAD^^and then just do them again. Note that any solution to this involves rewriting history, and if you've already pushed these commits somewhere, you shouldn't do this unless you know what you're doing.

如果它们是小的提交,并且在 git 中提交应该总是很小,最简单的方法是 git reset HEAD^^然后再做一次。请注意,对此的任何解决方案都涉及重写历史记录,如果您已经将这些提交推送到某个地方,除非您知道自己在做什么,否则不应这样做。

回答by Mikhail Golubtsov

I wrote a script to meet this purpose. You can checkout out it here.

我写了一个脚本来满足这个目的。你可以在这里结帐。

Using the script it would be as simple as:

使用脚本将非常简单:

mv-changes HEAD~ HEAD fileA

回答by Loic

I know this is an old question but I had the exact same request and I just figured out the best way to resolve this issue: you need to use interactive rebase and choose "edit" for the two commits you want to move a change from and to.

我知道这是一个老问题,但我有完全相同的请求,我只是想出了解决此问题的最佳方法:您需要使用交互式 rebase 并为要从中移动更改的两个提交选择“编辑”和到。

For instance, let say you have 3 commits:

例如,假设您有 3 个提交:

$ git log -3 --oneline
97e9d62 Commit C, editing file2.txt
34b066b Commit B, creating file2.txt + editing file1.txt
73ff1bb Commit A, creating file1.txt

Now, if you want the Commit B to only contain the file2.txt creation but not the file1.txt edition which should be in Commit C, git rebase -iwill display this:

现在,如果您希望 Commit B 仅包含 file2.txt 创建而不包含应在 Commit C 中的 file1.txt 版本,git rebase -i将显示以下内容:

pick 73ff1bb Commit A, creating file1.txt
pick 34b066b Commit B, creating file2.txt + editing file1.txt
pick 97e9d62 Commit C, editing file2.txt

# ...

If I replace "pick" with "edit" or "e" for Commit B and Commit C and close my editor, git will stop on the second commit and let me amend my changes:

如果我将 Commit B 和 Commit C 的“pick”替换为“edit”或“e”并关闭我的编辑器,git 将在第二次提交时停止并让我修改我的更改:

pick 73ff1bb Commit A, creating file1.txt
edit 34b066b Commit B, creating file2.txt + editing file1.txt
edit 97e9d62 Commit C, editing file2.txt
Stopped at 34b066b... Commit B, creating file2.txt + editing file1.txt
You can amend the commit now, with

    git commit --amend

Once you are satisfied with your changes, run

    git rebase --continue

$ vi file1.txt    # edit of file1.txt to remove the updated line
$ git commit --amend file1.txt
$ git rebase --continue

# -- git continues rebasing and stops on the Commit C to edit it

$ vi file1.txt   # edit of file1.txt to put the removed line
$ git commit --amend file1.txt
$ git rebase --continue

And that's it.

就是这样。

Take care to save somewhere the line(s) you're removing from first commit to put them in the second one. It can be in clipboard, text file, etc. I guess it should be possible to rely on git stashtoo but if it's a simple change it's easier to keep it in the clipboard.

请注意将要从第一次提交中删除的行保存在某处,以便将它们放入第二次提交中。它可以在剪贴板、文本文件等中。我想它也应该可以依赖,git stash但如果它是一个简单的更改,那么将它保存在剪贴板中会更容易。

回答by Berik

Another solution to this problem

这个问题的另一种解决方案

  • Find the commit just before the two commits (likely this is the master branch)
  • Run git rebase -i <commit before commits to be reordered>(git rebase -i masterin most cases)
  • In the text editor, swap the order of the commits (for vim, use the sequence ddpwhile on the line that should move down)
  • Save, quit, and let git rebase do the rest
  • 找到两次提交之前的提交(可能这是主分支)
  • 运行git rebase -i <commit before commits to be reordered>git rebase -i master在大多数情况下)
  • 在文本编辑器中,交换提交的顺序(对于 vim,ddp在应该向下移动的行上使用顺序)
  • 保存,退出,剩下的交给 git rebase

回答by David Culp

In the, probably unlikely, event you have just created those commits, undo the second commit with git reset HEAD^and then add the changes to the first commit with git commit --amend.

在可能不太可能的情况下,您刚刚创建了这些提交,撤消第二个提交,git reset HEAD^然后将更改添加到第一个提交git commit --amend

In the more likely event you have moved on and the commits were at some point in the past, rebasing is your best option. Without knowing your exact situation, citing a reference that explains it well is probably best.

在更有可能的情况下,您已经继续前进并且提交是在过去的某个时间点,rebase 是您最好的选择。在不知道您的确切情况的情况下,引用可以很好地解释它的参考资料可能是最好的。

In both the first and second event, the Pro Git book's section on rewriting history explains the options well -- both when they should be used and when caution should be used.

在第一个和第二个事件中,Pro Git 书中关于重写历史的部分很好地解释了这些选项 - 何时应该使用它们以及何时应该谨慎使用。

6.4 Git Tools - Rewriting History

6.4 Git 工具——重写历史