git:如何自动化交互式rebase /用等价的git命令替换它 [英] git: How to automate interactive rebase / replace it by equivalent git commands

查看:197
本文介绍了git:如何自动化交互式rebase /用等价的git命令替换它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要自动化一个交互式rebase或用其他命令替换它。让我解释一下我目前的情况:

在svn-> git转换中,我需要重新绑定新创建的git存储库以修复在创建期间创建的历史截止日期 SVN。

  branchNEW:包含SOMEDAY至今的历史
branchOLD:包含历史记录从过去到SOMEDAY

编辑或ascii:

  branchNEW:Y  -  Z 
branchOLD:W - X

两个分支都没有共同的提交。



现在基本想法是将branchNEW重新分支到branchOLD。不幸的是,有一些重构SOMEDAY:有些文件被移动到另一个目录。现在,rebase的结果是,每个移动的文件都存在于这两个地方。



编辑

 某些文件存在于X 
中(几乎)相同的文件也存在于Y中,仅存在于另一个路径

branchNEW:W - X - Y - Z
(转仓后)

转仓之后,HEAD现在包含X和Y的文件。我还尝试向branchOLD添加一个新的提交,它删除旧文件。在重新编译后的SVN-HEAD和git-HEAD是二进制相同的,但git log --follow不起作用。



现在解决主要问题:能够使用第二个交互式rebase来修复此问题:

  git rebase -i SHA 
old 根提交的sha-id。 现在在编辑器中,我必须将pick更改为编辑最顶层的提交。退出编辑器后,我现在必须删除错误的文件。

  git rm -f fileA fileB 
git commit - 修改
git rebase --continue

在这个git的HEAD之后, SVN,此外,git拥有完整的历史记录,并且git log --follow可用于移动的文件。



由于这一步只是一小部分未来巨大的VCS转型,我需要编写完整的流程脚本。 但如何实现上述步骤的自动化



(我知道SHA不会保持不变,但我可以获得所需的SHA从svn-id中嵌入到每个提交消息中)

解决方案

我找到了一个可能的解决方案:



git rebase --interactive 将rebase commitits列表(您可以选择,存储,编辑的列表)发送给编辑器。可以配置哪种编辑器。所以解决方案是为这个交互式底座配置一个替代的编辑器。这可以使用环境变量GIT_SEQUENCE_EDITOR完成:

  GIT_SEQUENCE_EDITOR =commandgit rebase -i SHA 

code>

命令可以是shell脚本或简单的sed:

  GIT_SEQUENCE_EDITOR =sed -i's / ^ pick ce5efdb / edit ce5efdb /; / ^ pick ce6efdb / d'git rebase -i SHA 

重要提示:rebase commitits列表作为文件传递给命令。所以命令必须接受一个文件名作为参数,并且必须将结果写入相同的文件。 sed -i正在做这件事。


I need to automate a interactive rebase or replace it by other commands. Just let me explain my current situation:

In an svn->git transition i need to rebase the newly created git repository to fix a "history cut-offs" made during SVN. Here is my manual workflow to fix the problem.

branchNEW: containing history from SOMEDAY until now
branchOLD: containing history from past to SOMEDAY

EDIT or as ascii:

branchNEW:        Y - Z
branchOLD: W - X

Both branches have no common commits.

The basic idea now is to just rebase branchNEW onto branchOLD. Unfortunately there have been some refactoring SOMEDAY: Some files were moved to another directory. The result of the rebase is now, that each moved file exists in both places.

EDIT

some file exist in X 
the (nearly) same files also exist in Y, just on another path

branchNEW: W - X - Y - Z
(after rebase)

After the rebase, HEAD now contains the files of X and of Y. I also tried to add a new commit to branchOLD which removes the old files. After the rebase SVN-HEAD and git-HEAD are binary identical, but the "git log --follow" does not work.

Now to the main problem: I am able to fix this by using a second, interactive rebase:

git rebase -i SHA

SHA is the sha-id of the old root commit in branchNEW. Now in the editor i have to change "pick" to "edit" for the topmost commit. After exiting the editor i now have to remove the wrong files

git rm -f fileA fileB
git commit --amend
git rebase --continue

After this HEAD of git is binary identical to head of SVN and in addition, git has the complete history and also "git log --follow" works for the moved files.

As this step is just a small part of a huge VCS transition in the future, i need to script the complete process. But how to automate the above steps?

(i know that SHAs won't stay the same, but i am able to get the required SHA from the svn-id which is embedded in each commit message)

解决方案

I found a possible solution:

git rebase --interactive sends the "list of rebase commits" (the list where you can pick, stash, edit, ...) to an editor. Which kind of editor can be configured. So the solution is to configure an alternative "editor" just for this interactive rebase. This can be done using the environment variable GIT_SEQUENCE_EDITOR:

GIT_SEQUENCE_EDITOR="command" git rebase -i SHA

command could be a shell script or just a simple sed:

GIT_SEQUENCE_EDITOR="sed -i 's/^pick ce5efdb /edit ce5efdb /;/^pick ce6efdb /d'" git rebase -i SHA

Important: The "list of rebase commits" is passed as file to the command. So the command must accept a filename as parameter and have to write the result to same file. "sed -i" is exactly doing this.

这篇关于git:如何自动化交互式rebase /用等价的git命令替换它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆