如何以非交互方式在 Git 中重新排序提交

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

how to re-order commits in Git non-interactively

gitgit-rewrite-history

提问by James Tauber

What non-interactive git command(s) achieve the change from Before to After?

哪些非交互式 git 命令实现了从之前到之后的变化?

Before:

前:

A---B---C---D

After:

后:

A---C'---B'---D'

回答by Mohamed Mansour

In your case, you can rebase interactive: git rebase -i HEAD~4Then you can just reorder your picks

在你的情况下,你可以git rebase -i HEAD~4重新设置交互:然后你可以重新排序你的选择

For example lets add three more files to our branch:

例如,让我们再向我们的分支添加三个文件:

git add A
git commit -m "A"

git add B
git commit -m "B"

git add C
git commit -m "C"

Your shortlog will be:

你的短日志将是:

$ git shortlog
 (3):
      A
      B
      C

If you want to reorder B with C:

如果你想用 C 对 B 重新排序:

$ git rebase -i HEAD~2
pick 1f9133d B
pick 33f41be C

You just re-order them to be:

您只需将它们重新排序为:

pick 33f41be C
pick 1f9133d B

After you're done writing, see the shortlog:

写完后,请参阅短日志:

$ git shortlog
 (3):
      A
      C
      B

You can do the same thing with all the commits by re-ordering. It is like what you see is what you get, which is pretty cool :)

您可以通过重新排序对所有提交执行相同的操作。就像所见即所得,这很酷:)

回答by Pa?lo Ebermann

Try this:

尝试这个:

git reset --hard A
git cherry-pick C
git cherry-pick B
git cherry-pick D

There may be a way with git rebase, but I didn't really understand it.

可能有一种方法git rebase,但我并没有真正理解它。

回答by pfalcon

See How do I run git rebase --interactive in non-interactive manner?for using git rebase --interactive in non-interactive manner.

请参阅如何以非交互式方式运行 git rebase --interactive?用于以非交互方式使用 git rebase --interactive。

Then, if you have formal criteria for reordering commits, you can script that, see for example Really flatten a git mergeto reorder commits by the original commit date.

然后,如果您有重新排序提交的正式标准,您可以编写脚本,例如查看真正展平 git merge以按原始提交日期重新排序提交。

回答by rubystallion

If you want to reorder commits in a script and don't want to deal with the commit hashes, then this seems to work as a general solution (based on Pa?lo Ebermann 's answer):

如果您想在脚本中重新排序提交并且不想处理提交哈希,那么这似乎可以作为通用解决方案(基于 Pa?lo Ebermann 的回答):

git reset --hard @~3
git cherry-pick ORIG_HEAD~1
git cherry-pick ORIG_HEAD~2
git cherry-pick ORIG_HEAD

I assume that running this sequence of commands twice in a row will restore the commit tree to what it was before, except for changing the changed commit hashes.

我假设连续两次运行此命令序列会将提交树恢复到之前的状态,除了更改更改的提交哈希。