Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

git: How to automate interactive rebase / replace it by equivalent git commands

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...