how to re-order commits in Git non-interactively

旧街凉风 提交于 2019-12-17 22:45:05

问题


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

Before:

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

After:

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

回答1:


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

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:

$ 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 :)




回答2:


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.




回答3:


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

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



来源:https://stackoverflow.com/questions/4981061/how-to-re-order-commits-in-git-non-interactively

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!